wasm_instance_worker (wasm v0.16.0)
View SourceA worker owning one WebAssembly instance, in the Cloudflare-Workers shape.
It is installed with the application; use it as it is, or copy and change it. It exists because the controls a worker gives you are exactly the ones an inline call cannot, and because getting them subtly wrong is easy.
It is also tested, by wasm_worker_SUITE, so the pattern documented here is
known to work rather than merely to look right.
What the process buys
An inline wasm:call/3 runs in the caller's process. That is the fast path and
it is the right answer for trusted code, but it means:
- a module looping forever hangs you, and cannot be killed without killing you
- there is no timeout to set, because the call is synchronous in your process
max_heap_sizeprotects nothing, because the allocation is on your heap- two processes sharing the instance race, because both read-modify-write it
Owning the instance in a process fixes all four, for one message round trip (measured: about 950 ns, against 11 KB of process overhead).
Isolation policy
The central choice for a Workers-style system, and the one most likely to be got wrong quietly:
freshdestroys and re-creates the instance after every request, so no memory, global or table content survives it. This is the default, because "my worker leaked data between requests" is the failure the other option causes.reusekeeps the instance, which is faster but lets state persist. Correct only for modules that are stateless by construction, or that you trust.
fresh is affordable here: the module is compiled once and cached, so a reset
is one destroy plus one instantiate, roughly 15 us plus page allocation.
Parallelism
Comes from more workers, never from concurrent calls into one instance. Run a pool and check one out per request.
Naming a worker
The name option is a label, nothing more: info/1 gives it back, and it is
the process label (proc_lib:set_label/1) that observer and crash dumps
show. The process is not registered under it, so you always address a worker
by the pid start_link/2 returns. Register it yourself, or keep the pid in
your pool, if you need to find it by name.
Summary
Functions
Run one request.
The timeout is enforced on both sides: gen_server:call stops waiting, and the
worker is killed so the work actually stops. Giving up without killing would
leave a runaway module burning a scheduler with nobody watching.
The kill does not depend on you: the worker's guard process holds the deadline, so a worker still running when it passes is killed even if the process that called has died.
call/3 uses worker_timeout, five seconds unless you set it:
application:set_env(wasm, worker_timeout, 30000).Which is a default and not a policy. A deadline that belongs to the request
belongs in call/4, where the caller who knows it can say so.
-spec start_link(wasm:module_()) -> {ok, pid()} | {error, term()}.
Start a worker over an already-loaded module.
Opts accepts imports, limits, isolation (fresh | reuse) and name,
a label info/1 returns; the process is not registered under it. limits
is a nested map of the keys wasm_limits lists. The module's start function
runs when the worker instantiates it, under a deadline of its own: timeout
in limits (milliseconds, or infinity), or worker_timeout as for
call/3. A start that runs past it kills the worker.
-spec start_link(wasm:module_(), map()) -> {ok, pid()} | {error, term()}.
-spec stop(pid()) -> ok.