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

rand 0.6+ panic when using wasm-bindgen #810

Closed
dandry opened this issue Jun 1, 2019 · 8 comments
Closed

rand 0.6+ panic when using wasm-bindgen #810

dandry opened this issue Jun 1, 2019 · 8 comments

Comments

@dandry
Copy link

dandry commented Jun 1, 2019

I'm familiar with the #616 issue, however I still experience this problem while using rand 0.6.5 and wasm-bindgen 0.2.45.

The source code:

extern crate rand;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn test() {
   let mut rng = rand::thread_rng();
}

Cargo.toml

[package]
name = "mgr-rust-wasm"
version = "0.1.0"
authors = ["Dandry<[email protected]>"]
edition = "2018"

[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = ["console_error_panic_hook"]

[dependencies]
wasm-bindgen = "0.2.45"
rand = "0.6.5"

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.1", optional = true }

# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. It is slower than the default
# allocator, however.
#
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.2", optional = true }

[dev-dependencies]
wasm-bindgen-test = "0.2.45"

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"

Executing the test() function in JS results in panic:

Uncaught RuntimeError: unreachable
at __rust_start_panic (wasm-function[171]:1)
at rust_panic (wasm-function[99]:31)
at std::panicking::rust_panic_with_hook::h5d8808384a53a826 (wasm-function[32]:303)
at std::panicking::continue_panic_fmt::h821bed92a14cf5d5 (wasm-function[53]:114)
at std::panicking::begin_panic_fmt::h83a0a8c60964976b (wasm-function[71]:94)
at rand::rngs::thread::THREAD_RNG_KEY::__init::{{closure}}::h0b4a20f36b746721 (wasm-function[74]:80)
at rand::rngs::thread::THREAD_RNG_KEY::__init::h91a64631d4c9d99e (wasm-function[40]:217)
at <std::thread::local::LocalKey>::try_with::ha7869471de61cfad (wasm-function[30]:53)
at <std::thread::local::LocalKey>::with::h8ab2ea0937eaef49 (wasm-function[81]:19)
at rand::rngs::thread::thread_rng::h18a8bb12b6ca765b (wasm-function[153]:6)

EDIT: Maybe it's worth mentioning that I'm using Manjaro Linux.

@dandry
Copy link
Author

dandry commented Jun 1, 2019

It turns out that my Cargo.toml configuration was wrong and I missed use rand::Rng; in my source code.
The following is correct in Cargo.toml:

rand = { version = "0.6.5", features = ["wasm-bindgen"] }

Sorry for wasting your time. This issue can be closed.

@dhardy
Copy link
Member

dhardy commented Jun 1, 2019

Yes, we require specification of a feature flag for WASM.

Not sure why the error message is so terrible, but the code for Rand 0.7 is already totally different so there isn't much incentive to investigate further.

@dhardy dhardy closed this as completed Jun 1, 2019
@yixing-liu
Copy link

yixing-liu commented Jul 7, 2019

I had the same error message with version "0.6.5" and "0.7". I added features =["wasm-bindgen"] .
This is my error message:

bootstrap.js:5 Error importing `index.js`: RuntimeError: unreachable
    at __rust_start_panic (wasm-function[171]:1)
    at rust_panic (wasm-function[101]:31)
    at std::panicking::rust_panic_with_hook::h12b7239ed4348eae (wasm-function[24]:303)
    at std::panicking::continue_panic_fmt::hfbe042bfacb6a5d7 (wasm-function[51]:114)
    at std::panicking::begin_panic_fmt::hb49e9d7f7ff6a73d (wasm-function[70]:94)
    at rand::rngs::thread::THREAD_RNG_KEY::__init::{{closure}}::h8d38765b7916ab20 (wasm-function[69]:90)
    at rand::rngs::thread::THREAD_RNG_KEY::__init::h217fcb2d71e66af8 (wasm-function[49]:100)
    at std::thread::local::LocalKey<T>::try_with::h673d3027270a955d (wasm-function[14]:54)
    at std::thread::local::LocalKey<T>::with::h1a224dc51f4b1570 (wasm-function[79]:19)
    at rand::rngs::thread::thread_rng::h659b4c6634f4c979 (wasm-function[108]:10)

I am using WSL Ubuntu LTS 18. I don't think this should matter, though.

@dhardy
Copy link
Member

dhardy commented Jul 8, 2019

@yixing-liu minimal example please, and also test whether the same happens when using the getrandom crate directly.

@josephlr
Copy link
Member

Note that if you're trying to reproduce this only using getrandom v0.2 the feature has been renamed to "js". So you'll need to have:

[dependencies]
getrandom = { version = "0.2", features = ["js"] }

@clay53
Copy link

clay53 commented Oct 19, 2020

I just want to note that the error message is still terrible. I can open a separate issue if that'd be appreciated.

@dhardy
Copy link
Member

dhardy commented Oct 19, 2020

Regarding Rand v0.6? Sorry, we have limited (human) resources, and investigating poor error messages for (very) outdated versions is not a good use of what we have. Unless you wish to make a PR yourself, in which case please go ahead.

@clay53
Copy link

clay53 commented Oct 19, 2020

Oh sorry, v0.7

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

No branches or pull requests

5 participants