Skip to content

Add stdout and stderr callbacks to WasiConfiguration - #391

Merged
martindevans merged 1 commit into
bytecodealliance:mainfrom
rlauuzo:wasi-custom-output
Oct 25, 2026
Merged

martindevans merged 1 commit into
bytecodealliance:mainfrom
rlauuzo:wasi-custom-output

Conversation

@rlauuzo

@rlauuzo rlauuzo commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

A guest's stdout and stderr could only be discarded, inherited or written to a file, so a host that wants guest output in its own logs (a Rust panic message, for instance) had to go through a temporary file. wasi_config_set_stdout_custom and wasi_config_set_stderr_custom take a callback instead, but were unbound.

WasiConfiguration.WithStandardOutputCallback and WithStandardErrorCallback bind them with a WasiOutputCallback(ReadOnlySpan<byte>). The trampoline is rooted by a GCHandle that Wasmtime frees through the finalizer when the WASI context is dropped. Like the existing stdio setters, each replaces any file or inheritance previously set for that stream.

Two behaviours come from Wasmtime rather than the binding, and are documented on the delegate:

  • The callback runs on a Wasmtime-owned thread while the thread that called into the guest waits, so it must not wait on a lock held around that call.
  • A single guest write may arrive as several calls, and if the callback throws, the exception is discarded and the stream is closed for the lifetime of the store.

Covered by tests for both streams (with a forced GC between configuration and the guest's write), a throwing callback failing the write, and a later file setter replacing the callback.

@martindevans martindevans left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@martindevans

Copy link
Copy Markdown
Member

Thanks for putting this together, just waiting for CI to finish before merging it :)

@martindevans
martindevans merged commit cb74b21 into bytecodealliance:main Oct 25, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants