- Notifications
You must be signed in to change notification settings - Fork18.7k
runtime: add a runtime.wasiOnIdle function#76775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:master
Are you sure you want to change the base?
Conversation
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View thisfailed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
gopherbot commentedDec 12, 2025
This PR (HEAD:2a51287) has been imported to Gerrit for code review. Please visit Gerrit athttps://go-review.googlesource.com/c/go/+/729680. Important tips:
|
gopherbot commentedDec 12, 2025
Message from Gopher Robot: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visitgolang.org/cl/729680. |
gopherbot commentedDec 12, 2025
Message from Gopher Robot: Patch Set 1: Congratulations on opening your first change. Thank you for your contribution! Next steps: Most changes in the Go project go through a few rounds of revision. This can be During May-July and Nov-Jan the Go project is in a code freeze, during which Please don’t reply on this GitHub thread. Visitgolang.org/cl/729680. |
2a51287 to250d1b3Compareruntime.wasiOnIdle function250d1b3 toc7fc6e1Comparegopherbot commentedDec 12, 2025
This PR (HEAD:c7fc6e1) has been imported to Gerrit for code review. Please visit Gerrit athttps://go-review.googlesource.com/c/go/+/729680. Important tips:
|
5ba5015 to4f250f8CompareThis small patch provides a mechanism for bridging the Go schedulerwith WASIp3's concurrency model.Note that I'm fairly new to Go and very open to feedback andalternative approaches. Please consider this as much an RFC as it isa PR.BackgroundBoth the js and wasip1 target OSes define a runtime.beforeIdlefunction, called by the scheduler if and when no goroutines arerunnable. In the case of js, the Go scheduler yields to the JS eventloop to await any async events it might produce. In the case ofwasip1 (and all other OSes), beforeIdle does nothing since thatplatform has neither an event loop nor async events.However, WASIp3 (due to be released early next year) _does_ supportconcurrency and asynchronous I/O [1] in which case it's useful for theGo scheduler to yield to the host once all goroutines have gone idle,just like it does for JS.MotivationThis patch is intended as a baby step towards full GOOS=wasip3support. Unlike GOOS=wasip1, where blocking I/O operations block_all_ goroutines, GOOS=wasip3 can support asynchronous I/O operationswhich cooperate with the Go scheduler, only blocking the goroutinedoing the call and allowing any others to continue running.Internally, each such operation may either complete immediatelywithout blocking or return a waitable handle representing a pendingevent. We can associate a channel with that waitable, to be writtento once the host delivers the corresponding event. The callinggoroutine reads from that channel before returning a value.In order to support the above, each exported function needs to be ableto wait for all goroutines to reach an idle state, collect anyaccumulated waitable handles, and return control to the host until oneor more events are ready. runtime.wasiOnIdle provides that capabilityby accepting a callback to be run by runtime.beforeIdle.Once GOOS=wasip3 has been fully implemented, the above can be handledinternally by the compiler and runtime. As a first step, though, I'vecreated a bindings generator [2] which generates import and exportglue code from the IDL in which the WASIp3 interfaces are defined.That glue code handles bridging Go's scheduler to the WASIp3 hostevent loop. It's able to do this using standard goroutines andchannels, with no special integration with the Go scheduler _except_for runtime.wasiOnIdle, hence this patch.Note that wasiOnIdle is private since it's not intended for generaluse; the glue code mentioned above uses go:linkname to access it.This use of go:linkname is a temporary measure while we experimentwith WASIp3 support outside of the runtime. The eventual goal is toencapsulate the host<->scheduler interaction entirely within the Goruntime.Concurrent imports and exportsWASIp3 is based on the WebAssembly Component Model [3], which includesan IDL (WebAssembly Interface Types, or WIT) and an ABI for expressinghigh-level types, functions, and interfaces which can be used torepresent both traditional OS features (e.g. filesystem and networkaccess) and high-level features such as HTTP request handlers anddatabase connections. WIT can also be used to represent custom,application specific APIs and then build components which eitherimplement or consume those APIs, analogous to how shared librarieswork on native OSes.Consequently, GOOS=wasip3 will ideally support creating both"executable"-style applications with a single func main entrypoint andalso "library"-style components with one or more custom entrypointsand imports. Fortunately, Go already has go:wasmexport andgo:wasmimport directives to support this. The wit-bindgen-go projectmentioned above builds upon those directives to support exporting andimporting _concurrent_ functions which may suspend and resume asnecessary (e.g. due to I/O) prior to producing a result.Hypothetically, this support could be integrated into the compiler ifthere's interest.[1]https://github.com/WebAssembly/component-model/blob/main/design/mvp/Concurrency.md[2]https://github.com/bytecodealliance/wit-bindgen/tree/main/crates/go[3]https://github.com/WebAssembly/component-model
4f250f8 toe83d890Comparegopherbot commentedDec 12, 2025
This PR (HEAD:4f250f8) has been imported to Gerrit for code review. Please visit Gerrit athttps://go-review.googlesource.com/c/go/+/729680. Important tips:
|
gopherbot commentedDec 12, 2025
This PR (HEAD:e83d890) has been imported to Gerrit for code review. Please visit Gerrit athttps://go-review.googlesource.com/c/go/+/729680. Important tips:
|
Uh oh!
There was an error while loading.Please reload this page.
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 js and wasip1 target OSes define a runtime.beforeIdle
function, called by the scheduler if and when no goroutines are
runnable. In the case of js, the Go scheduler yields to the JS event
loop to await any async events it might produce. In the case of
wasip1 (and all other OSes), beforeIdle does 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 [1] 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=wasip3
support. Unlike GOOS=wasip1, where blocking I/O operations block
all goroutines, GOOS=wasip3 can 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 a waitable handle representing a pending
event. We can associate a channel with that waitable, 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 waitable handles, and return control to the host until one
or more events are ready. runtime.wasiOnIdle provides that capability
by accepting a callback to be run by runtime.beforeIdle.
Once GOOS=wasip3 has been fully implemented, the above can be handled
internally by the compiler and runtime. As a first step, though, I've
created a bindings generator [2] 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 schedulerexcept
for runtime.wasiOnIdle, hence this patch.
Note that wasiOnIdle is private since it's not intended for general
use; the glue code mentioned above uses go:linkname to access it.
This use of go:linkname is 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 [3], 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=wasip3 will ideally support creating both
"executable"-style applications with a single func main entrypoint and
also "library"-style components with one or more custom entrypoints
and imports. Fortunately, Go already has go:wasmexport and
go:wasmimport directives to support this. The wit-bindgen-go project
mentioned above builds upon those directives to support exporting and
importingconcurrent 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.
[1]https://github.com/WebAssembly/component-model/blob/main/design/mvp/Concurrency.md
[2]https://github.com/bytecodealliance/wit-bindgen/tree/main/crates/go
[3]https://github.com/WebAssembly/component-model