runtime: add a runtime.wasiOnIdle function
#76775
Open
+9
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This small patch provides a mechanism for bridging the Go scheduler with WASIp3's concurrency model.
Note that I'm fairly new to Go and very open to feedback and alternative approaches. Please consider this as much an RFC as it is a PR.
Background
Both the
jsandwasip1target OSes define aruntime.beforeIdlefunction, called by the scheduler if and when no goroutines are runnable. In the case ofjs, the Go scheduler yields to the JS event loop to await any async events it might produce. In the case ofwasip1(and all other OSes),beforeIdledoes nothing since that platform has neither an event loop nor async events.However, WASIp3 (due to be released early next year) does support concurrency and asynchronous
I/O, in which case it's useful for the Go scheduler to yield to the host once all goroutines have gone idle, just like it does for JS.
Motivation
This patch is intended as a baby step towards full
GOOS=wasip3support. UnlikeGOOS=wasip1, where blocking I/O operations block all goroutines,GOOS=wasip3can support asynchronous I/O operations which cooperate with the Go scheduler, only blocking the goroutine doing the call and allowing any others to continue running. Internally, each such operation may either complete immediately without blocking or return awaitablehandle representing a pending event. We can associate a channel with thatwaitable, to be written to once the host delivers the corresponding event. The calling goroutine reads from that channel before returning a value.In order to support the above, each exported function needs to be able to wait for all goroutines to reach an idle state, collect any accumulated
waitablehandles, and return control to the host until one or more events are ready.runtime.wasiOnIdleprovides that capability by accepting a callback to be run byruntime.beforeIdle.Once
GOOS=wasip3has been fully implemented, the above can be handled internally by the compiler and runtime. As a first step, though, I've created a bindingsgenerator which generates import and export glue code from the IDL in which the WASIp3 interfaces are defined. That glue code handles bridging Go's scheduler to the WASIp3 host event loop. It's able to do this using standard goroutines and channels, with no special integration with the Go scheduler except for
runtime.wasiOnIdle, hence this patch.Note that
wasiOnIdleis private since it's not intended for general use; the glue code mentioned above usesgo:linknameto access it. This use ofgo:linknameis a temporary measure while we experiment with WASIp3 support outside of the runtime. The eventual goal is to encapsulate the host<->scheduler interaction entirely within the Go runtime.Concurrent imports and exports
WASIp3 is based on the WebAssembly Component
Model, which includes an IDL (WebAssembly Interface Types, or WIT) and an ABI for expressing high-level types, functions, and interfaces which can be used to represent both traditional OS features (e.g. filesystem and network access) and high-level features such as HTTP request handlers and database connections. WIT can also be used to represent custom, application specific APIs and then build components which either implement or consume those APIs, analogous to how shared libraries work on native OSes.
Consequently,
GOOS=wasip3will ideally support creating both "executable"-style applications with a singlefunc mainentrypoint and also "library"-style components with one or more custom entrypoints and imports. Fortunately, Go already hasgo:wasmexportandgo:wasmimportdirectives to support this. Thewit-bindgen-goproject mentioned above builds upon those directives to support exporting and importing concurrent functions which may suspend and resume as necessary (e.g. due to I/O) prior to producing a result. Hypothetically, this support could be integrated into the compiler if there's interest.