Skip to content

WebAssembly workers

A WebAssembly worker is one WebAssembly component targeting the virtufin:worker WIT world. It is created with MIME type application/wasm (a .wasm file, by URL or inline like a DLL) and runs in the Runtime's WasmEngine: one virtufin-wasm-host subprocess per worker, on wasmtime. It has the same lifecycle, outputs, secrets, variables and lifecycle events as every worker.

The WIT package lives in worker-devkit (wit/virtufin-worker/world.wit, the worker contract); the host compiles against a byte-identical copy (src/Virtufin.WorkManager.Engine.Wasm.Host/wit/virtufin-worker/). Authoring: Write a WebAssembly worker.

The contract: virtufin:worker@1.0.0

Version 1.0.0 is synchronous: every call returns before the host goes on, so it builds with today's WASI 0.2 toolchains. The host runs WASI 0.2 and WASI 0.3 components alike. An async process comes later as a new package version; the host keeps running the older versions until these docs announce their removal.

Export (virtufin:worker/guest) Called
start(ctx: start-context) -> result<_, error> once per activation, before the first event; ctx has the worker id, config, secrets and platform variables (each a list<tuple<string, string>>, sorted by name)
process(event: cloud-event) -> result<list<cloud-event>, error> per input event; the outputs are published in order, each to its publishtopic extension, else its type
stop() once, after the host stopped taking the worker's publishes and after the last process
Import (virtufin:worker/host) Does what it does for every engine
publish(event) -> result<_, error> publish now through the host's outbox and ownership gate, routed like an output; refused (error::refused) while stopping or after the lease is lost
report-status(state, detail) -> result<_, error> the worker's status (IWorkerContext.ReportStatusAsync)
gateway-call(service, method, request-json) -> result<string, error> a backend method through the api gateway (Gateway.InvokeJson, proto field names, a JSON object in and out); the guest's only way to the network
log(level, message) a line in the worker's log at that level

error is failed(string), refused(string) or invalid(string). A guest's start or process returning failed fails the call as a worker's exception does: a failed event is dead-lettered (dlq.<topic>, dlqerror the message). refused and invalid come back with the host's message prefixed.

Secrets reach the guest only in start. The host never logs them.

The cloud-event record

A CloudEvent crosses the boundary as a typed record, converted by the host from and to the platform's protobuf CloudEvent without loss; the guest needs no protobuf library.

Field CloudEvents attribute Notes
id, source, type id, source, type on an output, empty means unset: the Runtime gives a fresh id, urn:unknown, unknown (as for the other subprocess engines)
subject: option<string> subject
time: option<string> time RFC 3339, nanoseconds kept; an output's time that is not RFC 3339 fails the event
data-content-type: option<string> datacontenttype
data: option<list<u8>> data text data as its UTF-8 bytes; see below
extensions: list<tuple<string, attribute-value>> every other attribute sorted by name on the way in

attribute-value has a case per CloudEvents type, so an echoed attribute keeps its kind:

attribute-value protobuf
boolean(bool) ce_boolean
integer(s32) ce_integer
string(string) ce_string
binary(list<u8>) ce_bytes
uri(string) ce_uri
uri-ref(string) ce_uri_ref
timestamp(string) ce_timestamp (RFC 3339 text)

Data. In: text_data becomes its UTF-8 bytes, binary_data its bytes. Out: the data goes back as text_data when the content type is text (absent, which CloudEvents reads as JSON; text/*; application/json or +json; application/xml or +xml) and the bytes are UTF-8, else as binary_data. So an echoed event is the event that came in. proto_data comes in as its packed bytes. The config, secrets and variables the Runtime stamps on every input are string extensions.

Capabilities

Offered Not offered (refused at load, naming the import)
virtufin:worker/host wasi:filesystem (no preopened directory either)
wasi:clocks, wasi:random wasi:sockets
stdout and stderr: each line in the worker's log, at info and error wasi:http
their plumbing: wasi:io, wasi:cli (no environment, no arguments, an empty stdin, exit traps) anything else

Each of WASI 0.2 and WASI 0.3. Rust's wasm32-wasip2 standard library imports the wasi:cli plumbing in every component, which is why it is offered (empty); it imports wasi:filesystem only when the worker uses std::fs.

Limits

Every limit is a Runtime setting (configuration):

Setting Default What happens past it
Wasm:StartTimeoutMs 30000 start is interrupted; the start fails
Wasm:ProcessTimeoutMs 30000 process is interrupted; the event fails (dead-lettered)
Wasm:StopTimeoutMs 5000 (and at most the engine's stop budget) stop is interrupted; the worker counts as stopped. Also the grace of a start or process still running when the host stops the worker (below)
Wasm:MemoryLimitMiB 256 the growth traps; the call fails

A deadline is wall time, host calls included, checked by epoch interruption every 10 ms.

Stopping during a call. The contract has no stopping signal: a call is synchronous and the guest cannot see that the host stops the worker. So when the host stops it while start or process runs (the engine's stopping frame, workmanager#134), the call gets Wasm:StopTimeoutMs to return by itself, then the host interrupts it (checked every 100 ms). The event fails with "the host is stopping the worker"; the Runtime leaves it to the next activation (the worker no longer owns it, so it is not dead-lettered). The instance is dropped, so the worker's stop is not called on it and the stop is not held up by the call. A call that returns within the grace is not affected, and the guest's own stop is bounded only by its deadline. After a trap or an interrupt the host does not trust the instance: it drops it and, before the next event, builds a fresh one and calls start again with the same context. A worker keeps nothing in memory across a failed event: keep what must survive in state (or rebuild it in start). Other workers on the pod are not affected: each runs in its own process.

Design notes

The engine follows openspec change wasm-engine (workmanager#116). Where the implementation differs from its design.md, and why:

  • Typed extension values. design.md had extensions: list<tuple<string, string>> with typed values as their canonical text. The spec requires the conversion to be lossless, and the conformance vectors typed-attributes-survive-an-echo and non-string-publishtopic-falls-back-to-type need an echoed integer to stay an integer, so the value is the attribute-value variant.
  • data is optional (option<list<u8>>), so empty data and no data stay apart; and an output's data goes back as text when it is text (above), not always as binary_data, so an echo is exact.
  • The wasi:cli plumbing is linked (empty environment, no arguments, empty stdin, exit), beyond design.md's clocks, random and stdout/stderr: Rust's wasm32-wasip2 std imports it in every component, so without it no Rust worker would load. Filesystem, sockets and HTTP stay refused.
  • A new host call frame, gateway (20), with GatewayCall/GatewayResponse in proto/internal/engine.proto: no frame for a gateway call existed. It runs GatewayJsonCall, which the native engine's gateway_call now shares.
  • Wasm:MemoryLimitMiB, not Wasm:MemoryLimitMb: a binary amount is named in MiB (common#32).
  • glibc host binaries. The image's wasmhost stage cross-compiles for x86_64-unknown-linux-gnu/aarch64-unknown-linux-gnu with Debian's cross gcc, not musl: wasmtime's build compiles C (cc-rs), and Debian has no musl cross compilers. The binary needs libc and libgcc_s, which the chiseled base has.
  • The local host binary. The Engines.Local loader runs the same host from the PATH (or Wasm:HostPath); it is built from this repo (cargo install --path src/Virtufin.WorkManager.Engine.Wasm.Host), not taken from a worker-devkit release.

Prior art the host builds on: wasmtime's component::bindgen! (async imports and exports), wasmtime-wasi's per-interface add_to_linker functions (how wasmtime_wasi::p2::add_to_linker_* and p3::add_to_linker compose theirs, used here selectively), epoch interruption and StoreLimits. The guest crate's export! macro is wit-bindgen's pub_export_macro pattern, as SDK crates (spin-sdk, wasi) export their worlds.