bytecodealliance / wasmtime

A fast and secure runtime for WebAssembly
https://wasmtime.dev/
Apache License 2.0
15.09k stars 1.26k forks source link

wasm trap: cannot enter component instance (component model and wasi) #8943

Closed nerdachse closed 3 weeks ago

nerdachse commented 1 month ago

Hey there!

Similar to #8670 (which I read), I have a problem with calling multiple functions of a wasm component.

        let info_func = {
            let mut exports: wasmtime::component::Exports = plugin.exports(&mut store);
            let mut register = exports
                .instance("test:guest/info")
                .expect(&format!("plugin@{path:?} to have info instance"));
            match register.typed_func::<(), (PluginInfo,)>("info") {
                Ok(info) => info,
                Err(e) => {
                    eprintln!("plugin@{path:?} does not have an info function: {e}!");
                    continue;
                }
            }
        };

        let info = info_func.call(&mut store, ());
        println!("plugin@{path:?}: {info:?}");

        // Scope the borrow of &mut store to limit its lifetime
        let init_func = {
            let mut exports: wasmtime::component::Exports = plugin.exports(&mut store);
            let mut register = exports
                .instance("test:guest/register")
                .expect(&format!("plugin@{:?} to have register instance", path));
            match register.typed_func::<(), (String,)>("init") {
                Ok(init) => init,
                Err(e) => {
                    eprintln!("plugin@{:?} does not have an init function: {}!", path, e);
                    continue;
                }
            }
        };

        // Now call the init function after the borrow from exports has ended
        let result = init_func.call(&mut store, ()).expect("init to be callable");
        println!("plugin@{:?} result of init: {:?}", path, result);

gives me:

thread 'main' panicked at src/main.rs:121:53: init to be callable: wasm trap: cannot enter component instance

Now I understand the problem but in #8670 it was said that there could be another solution for this and I wonder what this could be. First attempt was to just iterate again, but apart from being non-optimal it's not enough to solve the problem.

As I plan on making a plugin system and sending events to interested plugins all the time, I wonder whether that's even feasible now.

I use wasmtime 22.0.0 and wasmtime_wasi 22.0.0 btw.

alexcrichton commented 1 month ago

You're missing a call to TypedFunc::post_return here which should be mentioned in the TypedFunc::call documentation as well. If possible I'd recommend using bindgen! if it works for your use case.