Skip to content

Bevy does not start on WASM because of the rand crates. #1376

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

Closed
codec-abc opened this issue Feb 1, 2021 · 13 comments
Closed

Bevy does not start on WASM because of the rand crates. #1376

codec-abc opened this issue Feb 1, 2021 · 13 comments
Labels
C-Bug An unexpected or incorrect behavior C-Dependencies A change to the crates that Bevy depends on O-Web Specific to web (WASM) builds
Milestone

Comments

@codec-abc
Copy link

codec-abc commented Feb 1, 2021

Bevy version

"0.4.0"

Operating system & version

Windows 10 and building for WASM.

What you did

I created a basic project to play with Seed (a Rust wasm framework) and Bevy to see how the 2 could interoperate. As I ran into other issues with Bevy I tried to setup a project for reproducing the bug. When I removed the Seed dependency another bug showed up (this one).

What you expected to happen

In this repository I set up the project is working fine as it is. But it contains Seed as a dependency that isn't needed. I expected to run just the same when removing the Seed dependency. Instead I got a crash with this error:

package.js:367 hello world
package_bg.wasm:0x13306b4 Uncaught (in promise) RuntimeError: unreachable
    at rust_start_panic (http://127.0.0.1:8000/static/package_bg.wasm:wasm-function[96256]:0x13306b4)
    at rust_panic (http://127.0.0.1:8000/static/package_bg.wasm:wasm-function[88735]:0x12fb685)
    at std::panicking::rust_panic_with_hook::hc5713da015ebaa19 (http://127.0.0.1:8000/static/package_bg.wasm:wasm-function[19582]:0xc80a7a)
    at std::panicking::begin_panic_handler::{{closure}}::hc5eba7f0030e8f4f (http://127.0.0.1:8000/static/package_bg.wasm:wasm-function[84877]:0x12d12fc)
    at std::sys_common::backtrace::rust_end_short_backtrace::he811f0bd07938b42 (http://127.0.0.1:8000/static/package_bg.wasm:wasm-function[85867]:0x12dd102)
    at rust_begin_unwind (http://127.0.0.1:8000/static/package_bg.wasm:wasm-function[76786]:0x1266ab9)
    at std::panicking::begin_panic_fmt::h308260c2e5b91752 (http://127.0.0.1:8000/static/package_bg.wasm:wasm-function[84876]:0x12d12c3)
    at rand::rngs::thread::THREAD_RNG_KEY::init::{{closure}}::h0d5a160d272afa5c (http://127.0.0.1:8000/static/package_bg.wasm:wasm-function[22564]:0xd1ee8e)
    at core::result::Result<T,E>::unwrap_or_else::h0acb8f77032d9da3 (http://127.0.0.1:8000/static/package_bg.wasm:wasm-function[5362]:0x7a5162)
    at rand::rngs::thread::THREAD_RNG_KEY::init::hb4d7b1557044de65 (http://127.0.0.1:8000/static/package_bg.wasm:wasm-function[9958]:0x9c051e)

To sum up, if you use the repo linked earlier, it will run fine as it is. When you edit the Cargo.toml to remove the Seed dependency it still build fine but crash at runtime while it shouldn't

Additional information

The call stack and some messages here and here on the Bevy discord seems to indicate that is a buggy version of the rand crate (or one of its dependencies). This would explain why it works with Seed. Since it uses rand it might trigger another version to be used which doesn't have this bug.

What could improve the situation is to pin exact version of the random crates that are known to work with WASM

PS: Thanks to Alice and DJMcNab on the Discord server for helping newcomers.

@mockersf
Copy link
Member

mockersf commented Feb 1, 2021

👋 welcome!

The one difference between rand in bevy and in seed is a feature:
https://github.com/seed-rs/seed/blob/2fc928781c6abf4af47fd617be7ab263d25c84d8/Cargo.toml#L32

As feature are additive, enabling it for rand through seed also enables it in bevy dependencies.

Could you try to not import seed, but import rand with the same feature? It would not be a real fix, but would help to identify the cause.

rand = { version = "0.8.0", features = ["small_rng"] }

@codec-abc
Copy link
Author

Thanks for feedback. I tried the rand trick but it didn't work. I run a diff between the output of cargo tree for the 2 versions and one interesting thing is that rand_core and rand_pcr show up in the version with Seed but not with the one. I tried to add many dependencies but I wonder if the linker does not remove them somehow because I did not find a single configuration that works by adding dependencies.

@rparrett
Copy link
Contributor

rparrett commented Feb 1, 2021

Swapping out the seed dependency for uuid = {version="=0.8.1"} makes things work. I'm not sure why this is necessary exactly, but https://github.com/mrk-its/bevy_webgl2_app_template also does this. Not sure if that's expected or an example of a workaround for the same bug.

@rparrett
Copy link
Contributor

rparrett commented Feb 1, 2021

Here's a more complete stack trace in case it's useful

package_bg.wasm:0x133dcdd Uncaught (in promise) RuntimeError: unreachable
    at __rust_start_panic (http://localhost:9000/static/package_bg.wasm:wasm-function[96258]:0x133dcdd)
    at rust_panic (http://localhost:9000/static/package_bg.wasm:wasm-function[88826]:0x13096a6)
    at std::panicking::rust_panic_with_hook::h123718ba3bf480af (http://localhost:9000/static/package_bg.wasm:wasm-function[19192]:0xc70187)
    at std::panicking::begin_panic_handler::{{closure}}::hf393a82e58397bd2 (http://localhost:9000/static/package_bg.wasm:wasm-function[30862]:0xe7f73b)
    at std::sys_common::backtrace::__rust_end_short_backtrace::h2ff2cfc953878925 (http://localhost:9000/static/package_bg.wasm:wasm-function[85847]:0x12e9e85)
    at rust_begin_unwind (http://localhost:9000/static/package_bg.wasm:wasm-function[77271]:0x127a28e)
    at std::panicking::begin_panic_fmt::h96153d9dc0e882ed (http://localhost:9000/static/package_bg.wasm:wasm-function[84857]:0x12de077)
    at rand::rngs::thread::THREAD_RNG_KEY::__init::{{closure}}::h8ae4330291bfd8f8 (http://localhost:9000/static/package_bg.wasm:wasm-function[22415]:0xd1bf6a)
    at core::result::Result<T,E>::unwrap_or_else::h633a94d6b0dafd1c (http://localhost:9000/static/package_bg.wasm:wasm-function[5509]:0x7c3c1e)
    at rand::rngs::thread::THREAD_RNG_KEY::__init::h5b5b278c06a41cb3 (http://localhost:9000/static/package_bg.wasm:wasm-function[9984]:0x9cda71)
    at core::ops::function::FnOnce::call_once::ha51a275f0968dcf3 (http://localhost:9000/static/package_bg.wasm:wasm-function[78877]:0x12901cd)
    at std::thread::local::lazy::LazyKeyInner<T>::initialize::h0288a6bbce1ccea8 (http://localhost:9000/static/package_bg.wasm:wasm-function[15958]:0xbaaded)
    at std::thread::local::statik::Key<T>::get::h7d594a2cc1db358b (http://localhost:9000/static/package_bg.wasm:wasm-function[25295]:0xda45c1)
    at rand::rngs::thread::THREAD_RNG_KEY::__getit::h8a02932e64c3636a (http://localhost:9000/static/package_bg.wasm:wasm-function[95021]:0x133980e)
    at std::thread::local::LocalKey<T>::try_with::h2e6a6b9b9ee15f76 (http://localhost:9000/static/package_bg.wasm:wasm-function[19692]:0xc8bde0)
    at std::thread::local::LocalKey<T>::with::hb2a688111ead90af (http://localhost:9000/static/package_bg.wasm:wasm-function[36450]:0xf34793)
    at rand::rngs::thread::thread_rng::h7315cb71fd2fc4e5 (http://localhost:9000/static/package_bg.wasm:wasm-function[41818]:0xfcb679)
    at rand::random::h6c400ee9fc5b67e0 (http://localhost:9000/static/package_bg.wasm:wasm-function[58430]:0x1144724)
    at bevy_ecs::system::system::SystemId::new::h6e9dc5eb5c413125 (http://localhost:9000/static/package_bg.wasm:wasm-function[73622]:0x12457fe)
    at <bevy_ecs::schedule::stage::RunOnce as core::default::Default>::default::hb788ef4d44199b5b (http://localhost:9000/static/package_bg.wasm:wasm-function[28038]:0xe16f85)
    at bevy_app::app_builder::AppBuilder::add_default_stages::hc6079b3d539ebee0 (http://localhost:9000/static/package_bg.wasm:wasm-function[2675]:0x5bac41)
    at <bevy_app::app_builder::AppBuilder as core::default::Default>::default::hdc7bc2b4c376e2ad (http://localhost:9000/static/package_bg.wasm:wasm-function[25915]:0xdbfaeb)
    at bevy_app::app::App::build::hd77e41c462b67f70 (http://localhost:9000/static/package_bg.wasm:wasm-function[95960]:0x133d713)
    at bevy_wasm::start::h49c318fd183fea71 (http://localhost:9000/static/package_bg.wasm:wasm-function[689]:0x2f48db)
    at start (http://localhost:9000/static/package_bg.wasm:wasm-function[83331]:0x12cb254)
    at init (http://localhost:9000/static/package.js:1108:10)

@codec-abc
Copy link
Author

@rparrett Are you sure about the uuid thing? I tried adding it and it still crash on my side.

@rparrett
Copy link
Contributor

rparrett commented Feb 1, 2021

No, I may have goofed, or maybe there's something intermittent going on. I can't seem to even reproduce the panic anymore.

By the way, did you mean to link to your other bevy-repro repo in the issue text? If not, I'm even more lost.

@codec-abc
Copy link
Author

codec-abc commented Feb 1, 2021

I am so sorry, I indeed linked the wrong repository. I now edited the first message to avoid confusion.

Edit: The linked repo was the one where I found the issue but contains a bit more code than necessary that's why I set up another one for this issue. The "good" news is that the problem is the same and for example the call stack you printed earlier is also correct.

About reproducing it, I disabled caching thanks to the Chrome dev tools and sometimes to make sure I just delete the static directory in out to make sure the build recerate fresh new files.

@rparrett
Copy link
Contributor

rparrett commented Feb 2, 2021

I just went through the procedure again, adding cargo clean into the mix and being extra careful.

Adding uuid = {version="=0.8.1"} does seem to help.

In bevy 0.4, bevy_scene, bevy_reflect, and bevy_utils otherwise specify uuid = "0.8" which picks up 0.8.2

From uuid 0.8.2's changelog:

Use getrandom instead of rand (447)
Update getrandom to 0.2.0 (501)

So I think there could be something to this, but I haven't quite connected all the dots.

@codec-abc
Copy link
Author

@rparrett Thanks for giving this another go. I will do another round later today because I want to make sure we can reproduce it and pin down to a particular crate.

Regarding the (maybe) intermittent nature of the bug: If this the case (I will triple check this), doesn't it look a bug in cargo and/or rustc? Because, to me it means that the same input files (cargo.toml and some rust source code) would behave differently at runtime depending on earlier commands which isn't something I want.

@codec-abc
Copy link
Author

So toggling on and off uuid = {version="=0.8.1"}

and building with

 rm -f -r target && rm -f Cargo.lock && cargo clean && wasm-pack -v build --target web --out-name package --out-dir out/static --dev

I was able to confirm that indeed this make the program run normally or crash depending if uuid in version 0.8.1 is used.

Thanks for the help @rparrett. At this point we have at least pinpointed the problem and found a workaround.

@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior O-Web Specific to web (WASM) builds C-Dependencies A change to the crates that Bevy depends on labels Feb 17, 2021
@alice-i-cecile
Copy link
Member

This is really unfortunate, but I don't think there's anything we can do at this point. Going to close as unactionable :(

This is another argument in favor of #3992.

@alice-i-cecile alice-i-cecile added the S-Blocked This cannot move forward until something else changes label Apr 29, 2022
@alice-i-cecile
Copy link
Member

Reopening... We should just remove rand, as @DJMcNab points out.

@alice-i-cecile alice-i-cecile removed the S-Blocked This cannot move forward until something else changes label Apr 29, 2022
@alice-i-cecile alice-i-cecile added this to the Bevy 0.8 milestone Apr 29, 2022
@alice-i-cecile
Copy link
Member

Closed, as #3992 is merged. If you have encountered a new related problem, please open a new issue :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-Bug An unexpected or incorrect behavior C-Dependencies A change to the crates that Bevy depends on O-Web Specific to web (WASM) builds
Projects
None yet
Development

No branches or pull requests

4 participants