Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dylib inside godot project breaks hot reaload #871

Open
tbillington opened this issue Aug 24, 2024 · 8 comments
Open

dylib inside godot project breaks hot reaload #871

tbillington opened this issue Aug 24, 2024 · 8 comments
Labels

Comments

@tbillington
Copy link

tbillington commented Aug 24, 2024

Hey, I'm getting errors in godot whenever I try to use hot reloading in Godot 4.3 (non-mono). I'm on 7634fe7 of gdext which is the latest as of now, with the api-4-3 feature enabled.

Screen.Recording.2024-08-24.at.12.28.19.PM.mp4

The error I get is related to ClassName it seems.

 /Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-core/src/private.rs:365 - Rust function panicked at /Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-core/src/meta/class_name.rs:182.
    Context: failed to initialize GDExtension level `Scene`
  /Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-core/src/private.rs:378 - [panic]  index out of bounds: the len is 0 but the index is 1
  /Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-core/src/private.rs:365 - Rust function panicked at /Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-core/src/registry/class.rs:207.
    Context: failed to initialize GDExtension level `Editor`
  /Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-core/src/private.rs:378 - [panic]  global lock for loaded classes poisoned; class registration or deregistration may have panicked
  /Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-core/src/private.rs:365 - Rust function panicked at /Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-core/src/registry/class.rs:207.
    Context: failed to deinitialize GDExtension level `Editor`
  /Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-core/src/private.rs:378 - [panic]  global lock for loaded classes poisoned; class registration or deregistration may have panicked
  /Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-core/src/private.rs:365 - Rust function panicked at /Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-core/src/registry/class.rs:207.
    Context: failed to deinitialize GDExtension level `Scene`
  /Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-core/src/private.rs:378 - [panic]  global lock for loaded classes poisoned; class registration or deregistration may have panicked
  /Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-core/src/private.rs:365 - Rust function panicked at /Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-core/src/registry/class.rs:207.
    Context: failed to deinitialize GDExtension level `Servers`
  /Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-core/src/private.rs:378 - [panic]  global lock for loaded classes poisoned; class registration or deregistration may have panicked
  /Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-core/src/private.rs:365 - Rust function panicked at /Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-core/src/registry/class.rs:207.
    Context: failed to deinitialize GDExtension level `Core`
  /Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-core/src/private.rs:378 - [panic]  global lock for loaded classes poisoned; class registration or deregistration may have panicked
  /Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-core/src/private.rs:365 - Rust function panicked at /Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-ffi/src/binding/single_threaded.rs:75.

My code is just vanilla book starter

// lib.rs
mod player;

use godot::prelude::*;

struct MyExtension;

#[gdextension]
unsafe impl ExtensionLibrary for MyExtension {}

// player.rs
use godot::classes::{ISprite2D, Sprite2D};
use godot::prelude::*;

#[derive(GodotClass)]
#[class(base=Sprite2D)]
struct Player {
    speed: f64,
    angular_speed: f64,
    base: Base<Sprite2D>,
}

#[godot_api]
impl ISprite2D for Player {
    fn init(base: Base<Sprite2D>) -> Self {
        godot_print!("Hello, GD world!");
        Self {
            speed: 400.0,
            angular_speed: std::f64::consts::PI,
            base,
        }
    }

    fn physics_process(&mut self, delta: f64) {
        let radians = (self.angular_speed * delta) as f32;
        self.base_mut().rotate(radians);
    }
}

Rust 1.80

> rustc --version --verbose
rustc 1.80.1 (3f5fd8dd4 2024-08-06)
binary: rustc
commit-hash: 3f5fd8dd41153bc5fdca9427e9e05be2c767ba23
commit-date: 2024-08-06
host: aarch64-apple-darwin
release: 1.80.1
LLVM version: 18.1.7
@Houtamelo
Copy link
Contributor

Do you have any classes marked with no_init? (In the #[class()] attribute)

@tbillington
Copy link
Author

tbillington commented Aug 24, 2024

The included code is all I have in the project

@zhouzhipeng
Copy link

have you tried to restart godot editor?

@tbillington
Copy link
Author

Restarting the editor allows the rust extension to function again until I do another cargo build, at which point it breaks and I receive the same error as I posted above.

@tbillington
Copy link
Author

Ok, moving the location of the .dylib files out of the godot project directory allowed hot reload to work again.

I guess this is okay for development, but for builds the .dylib must be within the project for the res:// asset reference to work (at least according to the gdext book). I don't really want to switch back and forth the paths in .gdextension when I need to make a build.

Any thoughts on what I could do here?

@lilizoey lilizoey added the bug label Aug 27, 2024
@tbillington tbillington changed the title Hot reload fails dylib inside godot project breaks hot reaload Aug 29, 2024
@Bromeon
Copy link
Member

Bromeon commented Aug 31, 2024

I guess this is okay for development, but for builds the .dylib must be within the project for the res:// asset reference to work (at least according to the gdext book).

I'm actually not sure if the book advice still true. I recently exported a project (Windows Release) without changing anything in my paths, and it seemed to work.

Could you verify this with Godot 4.3?


About the error you get:

/Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-core/src/private.rs:365 - Rust function panicked at /Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-core/src/meta/class_name.rs:182.
    Context: failed to initialize GDExtension level `Scene`
  /Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-core/src/private.rs:378 - [panic]  index out of bounds: the len is 0 but the index is 1
  /Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-core/src/private.rs:365 - Rust function panicked at /Users/choc/.cargo/git/checkouts/gdext-76630c89719e160c/7634fe7/godot-core/src/registry/class.rs:207.
    Context: failed to initialize GDExtension level `Editor`

This seems to happen here:

let entry = &cached_names[self.global_index as usize];

So classes are unloaded, but the cache still points to them. I wonder why this happens 🤔

@jgunzelman88
Copy link

I am running into the same problem. I did rename my lib and the file locations at some point after I registrered it with godot. @tbillington Did you re-name your extension? Maybe the cache is not getting updated from the original value.

@Bromeon
Copy link
Member

Bromeon commented Oct 20, 2024

Possibly related to godotengine/godot-cpp#1573 and the now solved godotengine/godot-cpp#1589. Would be interesting to see if it still occurs in last Godot nightly builds...

There's also godotengine/godot-cpp#1594.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants