Stream: general

Topic: ✔ Running and loading modules multiple times


view this post on Zulip Ari Seyhun (May 11 2022 at 08:47):

I am building a system where there are many versions of wasm file... eg MyModule@0.1.0.wasm, MyModule@0.1.1.wasm.
Each module uses the same .wit definition.

I'm wondering which parts of the engine/linker/state should be kept separate, and which items should be defined multiple timers, each per module?

I currently have a function to create a new runtime:

import!("./domain.wit");
pub fn new() -> anyhow::Result<Self> {
    let engine = Engine::default();
    let mut linker = Linker::<Data>::new(&engine);
    wasmtime_wasi::add_to_linker(&mut linker, |ctx| &mut ctx.wasi)?;

    let wasi = WasiCtxBuilder::new()
        .inherit_stdio()
        .inherit_args()?
        .build();
    let store = Store::new(
        &engine,
        Data {
            wasi,
            domain: DomainData::default(),
        },
    );

    Domain::add_to_linker(&mut linker, move |ctx| &mut ctx.domain)?;

    Ok(Runtime {
        engine,
        linker,
        store,
    })
}

And when a user wants to run a specific module, I am loading the wasm file and creating a new instance from the linker:

let module = wasmtime::Module::from_file(&engine, "./MyModule@0.1.0.wasm");
let instance = self.linker.instantiate(&mut store, &module)?;
let domain = Domain::new(&mut store, &instance, |ctx| &mut ctx.domain)?;
self.linker.module(&mut store, "", &module)?;

The snippet above is run each time, using a shared engine, store and linker.
The code runs fine the first time, but on the 2nd time it fails on the line self.linker.module:

import of `::memory` defined twice

Should my Rust app define one linker and share it, or does the linker need to be created new each time I want to run my module?

view this post on Zulip Ari Seyhun (May 11 2022 at 08:48):

I guess Linker and Store both need to be made new for each module loaded?

view this post on Zulip bjorn3 (May 11 2022 at 09:05):

I believe so. No resources are freed until you drop a Store, even if you drop an Instance. And a Linker would contain references to the old module.

view this post on Zulip Alex Crichton (May 11 2022 at 14:54):

I believe the self.linker.module statement is defining new items within the Linker which may not be desired here? I think you probably just need self.linker.instantiate perhaps?

You'll need a new Store for each instantiation but it's intended that Linker can be shared across many instantiations.

view this post on Zulip Notification Bot (May 12 2022 at 04:47):

Ari Seyhun has marked this topic as resolved.

view this post on Zulip Ari Seyhun (May 12 2022 at 04:51):

(deleted)


Last updated: Dec 23 2024 at 12:05 UTC