DioxusLabs / dioxus

Fullstack app framework for web, desktop, mobile, and more.
https://dioxuslabs.com
Apache License 2.0
20.32k stars 779 forks source link

Fermi Error at Optlevel 2-3 #536

Closed JoschuaL closed 2 years ago

JoschuaL commented 2 years ago

Problem

When compiling a simple programm using dioxus with fermi and the web feature enabled, using trunk, this error message appears and the application does not work properly as soon as the page is opened in a browser

Error Message panicked at 'called `Result::unwrap()` on an `Err` value: Any { .. }', /home/misha/.cargo/registry/src/github.com-1ecc6299db9ec823/fermi-0.2.1/src/root.rs:47:43 Stack: getImports/imports.wbg.__wbg_new_693216e109162396@http://localhost:8080/frontend-a85016710e7cf20c.js:338:21 console_error_panic_hook::hook::hccdc65123d386add@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[223]:0x1fd63 core::ops::function::Fn::call::h03b5ba35aba6f2f3@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[1013]:0x3ab25 std::panicking::rust_panic_with_hook::h84feca33bd4bd229@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[418]:0x30cb3 std::panicking::begin_panic_handler::{{closure}}::hd2eacd3bb9ff1eab@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[493]:0x34113 std::sys_common::backtrace::__rust_end_short_backtrace::h976699518d897fb1@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[845]:0x3a2c6 rust_begin_unwind@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[687]:0x38a98 core::panicking::panic_fmt::hc171d095bc4a492d@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[689]:0x38b19 core::result::unwrap_failed::h68ab818eb89182b6@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[515]:0x34cdf fermi::root::AtomRoot::register::hf7d7317a0f91fa2a@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[210]:0x1dde5 fermi::hooks::read::use_read::hb21da33d9e04cfca@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[488]:0x33dfd frontend::Component::ha848ed4cf3cb4e81@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[848]:0x3a2f8 as dioxus_core::nodes::AnyProps>::render::he92fd34f9b06d3e8@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[678]:0x38836 dioxus_core::scopes::ScopeArena::run_scope::hc59c6e8afeb6c864@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[180]:0x1893d dioxus_core::diff::DiffState::create_node::h5273c7db041d8d99@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[138]:0x8b2b dioxus_core::virtual_dom::VirtualDom::rebuild::hc9371f0c64a5a9bc@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[182]:0x190c6 as core::future::future::Future>::poll::h2c32271d32ce7fa9@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[133]:0x3d3a wasm_bindgen_futures::task::singlethread::Task::run::h300c65dbd2dfe601@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[388]:0x2f27f wasm_bindgen_futures::queue::Queue::new::{{closure}}::h580e790085673073@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[227]:0x20616 +Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::h412ac59d3b7c064a@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[795]:0x39ded __wbg_adapter_24@http://localhost:8080/frontend-a85016710e7cf20c.js:246:10 real@http://localhost:8080/frontend-a85016710e7cf20c.js:190:20 promise callback*getImports/imports.wbg.__wbg_then_18da6e5453572fc8@http://localhost:8080/frontend-a85016710e7cf20c.js:904:33 js_sys::Promise::then::h845cd8a514441641@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[906]:0x3a66b wasm_bindgen_futures::queue::Queue::schedule_task::h6319dc014492e3a9@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[329]:0x2ae7a wasm_bindgen_futures::task::singlethread::Task::spawn::h25873dcb0a7f64b7@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[562]:0x361cf dioxus_web::launch::h47bece2a21662e15@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[444]:0x32132 frontend::main::h25b739b51f73a32c@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[1010]:0x3ab0e std::sys_common::backtrace::__rust_begin_short_backtrace::h7ec7bb65dbbfea46@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[1011]:0x3ab16 std::rt::lang_start::{{closure}}::h5ab029a62639367a@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[716]:0x390b9 std::rt::lang_start_internal::he9e674c0ac64f681@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[447]:0x3233f main@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[715]:0x39098 @http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[921]:0x3a742 finalizeInit@http://localhost:8080/frontend-a85016710e7cf20c.js:947:10 init@http://localhost:8080/frontend-a85016710e7cf20c.js:976:12 async*@http://localhost:8080/:12:54 frontend-a85016710e7cf20c.js:351:13 __wbg_error_09919627ac0992f5 http://localhost:8080/frontend-a85016710e7cf20c.js:351 hccdc65123d386add http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:130678 h03b5ba35aba6f2f3 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:240421 h84feca33bd4bd229 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:199859 hd2eacd3bb9ff1eab http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:213267 h976699518d897fb1 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:238278 rust_begin_unwind http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:232088 hc171d095bc4a492d http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:232217 h68ab818eb89182b6 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:216287 hf7d7317a0f91fa2a http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:122341 hb21da33d9e04cfca http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:212477 ha848ed4cf3cb4e81 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:238328 as dioxus_core::nodes::AnyProps>::render::he92fd34f9b06d3e8 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:231478 hc59c6e8afeb6c864 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:100669 h5273c7db041d8d99 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:35627 hc9371f0c64a5a9bc http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:102598 as core::future::future::Future>::poll::h2c32271d32ce7fa9 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:15674 h300c65dbd2dfe601 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:193151 h580e790085673073 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:132630 +Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::h412ac59d3b7c064a http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:237037 __wbg_adapter_24 http://localhost:8080/frontend-a85016710e7cf20c.js:246 real http://localhost:8080/frontend-a85016710e7cf20c.js:190 (Async: promise callback) __wbg_then_18da6e5453572fc8 http://localhost:8080/frontend-a85016710e7cf20c.js:904 h845cd8a514441641 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:239211 h6319dc014492e3a9 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:175738 h25873dcb0a7f64b7 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:221647 h47bece2a21662e15 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:205106 h25b739b51f73a32c http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:240398 h7ec7bb65dbbfea46 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:240406 h5ab029a62639367a http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:233657 he9e674c0ac64f681 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:205631 main http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:233624 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:239426 finalizeInit http://localhost:8080/frontend-a85016710e7cf20c.js:947 init http://localhost:8080/frontend-a85016710e7cf20c.js:976 http://localhost:8080/:12

This only happens when rusts opt-level is set to 2 or 3 (either manually in cargo.toml or by doing a release build, which defaults to 3) On all other opt-levels, no error message appears and the application behaves as intended

Steps To Reproduce

I uploaded a minnimum Setup to trigger the error in this repo

Alternatively, the code in the main.rs is

use dioxus::prelude::*;

fn main() {
    dioxus::web::launch(app);
}

fn app(cx: Scope) -> Element {
    let atom = use_read(&cx, ATOM);

    cx.render(rsx! {
        Component{},
    })
}

pub struct S {
    pub x: String,
}

pub static ATOM: Atom<Vec<S>> = |_| Vec::new();
pub static OTHER_ATOM: Atom<Vec<String>> = |_| Vec::new();

pub fn Component(cx: Scope) -> Element {
    let other_atom = use_read(&cx, OTHER_ATOM);
    None
}

And otherwise its just a minimal dioxus application for the web.

Of note, the error can be avoided by removing either of the use_read use_read expressions, changing the type in other atom to a numeric type (i64), the type in ATOM to some simple type like string, removing the field x of struct S or changing its type to a numeric type.

So overall very peculiar.

It could also be that this is in fact not a bug in dioxus but a bug in the rust compiler/optimizer/wasm toolchain, though i think that that is less likely than a dioxus bug.

Expected behavior

The behavior for any opt-level is the same, the application works without an error message

Screenshots

Environment:

jkelleyrtp commented 2 years ago

It looks like Rust merges the two Atoms together when they're unused. This causes issues with down casting and TypeIds.

Instead of panicking here, we should combine the typeid of the pointer to use as the hash, so these collisions can't occur even in apps that don't use various atoms.

If you change the contents of either vet or actually use them in your app, Rust won't merge them together.

Edit: I put up a branch that fixes this issue by making the AtomId hold the typeid to prevent this bug.

Thank you so much for the repro - it made it super easy to fix.

If you want to ride on master until this fix is merged, then you'll need to update your Cargo.toml and project.

dioxus = {git = "https://github.com/dioxuslabs/dioxus.git"}
dioxus-web = {git = "https://github.com/dioxuslabs/dioxus.git"}
fermi = { git = "https://github.com/dioxuslabs/dioxus.git"}
JoschuaL commented 2 years ago

Let me know when the fix lands in something i could try out, because this also happens in a non-trivial context where im pretty sure im using all of the atoms. But there might also one im just overlooking, or the code using it might be unused or stuff like that.

JoschuaL commented 2 years ago

sorry, pressed the wrong thing there