go icon indicating copy to clipboard operation
go copied to clipboard

runtime: add a `runtime.wasiOnIdle` function

Open dicej opened this issue 2 weeks ago • 1 comments

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, 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 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 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, 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 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.

dicej avatar Dec 10 '25 00:12 dicej

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 this failed 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.

google-cla[bot] avatar Dec 10 '25 00:12 google-cla[bot]

This PR (HEAD: 2a5128718663e0c1f8f5133025542df1f926e00f) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/729680.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

gopherbot avatar Dec 12 '25 15:12 gopherbot

Message from Gopher Robot:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/729680. After addressing review feedback, remember to publish your drafts!

gopherbot avatar Dec 12 '25 15:12 gopherbot

Message from Gopher Robot:

Patch Set 1:

Congratulations on opening your first change. Thank you for your contribution!

Next steps: A maintainer will review your change and provide feedback. See https://go.dev/doc/contribute#review for more info and tips to get your patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be surprising to people new to the project. The careful, iterative review process is our way of helping mentor contributors and ensuring that their contributions have a lasting impact.

During May-July and Nov-Jan the Go project is in a code freeze, during which little code gets reviewed or merged. If a reviewer responds with a comment like R=go1.11 or adds a tag like "wait-release", it means that this CL will be reviewed as part of the next development cycle. See https://go.dev/s/release for more details.


Please don’t reply on this GitHub thread. Visit golang.org/cl/729680. After addressing review feedback, remember to publish your drafts!

gopherbot avatar Dec 12 '25 15:12 gopherbot

This PR (HEAD: c7fc6e1a234cb2040f401d754ac613e089d7afd2) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/729680.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

gopherbot avatar Dec 12 '25 16:12 gopherbot

This PR (HEAD: 4f250f80976ea056a398bca4637eb0f93f805442) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/729680.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

gopherbot avatar Dec 12 '25 16:12 gopherbot

This PR (HEAD: e83d89075e0354b01bb43c6689b70c1245a47a0f) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/729680.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

gopherbot avatar Dec 12 '25 16:12 gopherbot