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?
I guess Linker
and Store
both need to be made new for each module loaded?
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.
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.
Ari Seyhun has marked this topic as resolved.
(deleted)
Last updated: Nov 22 2024 at 17:03 UTC