Repository navigation
Add await_io() and add_object() to the Rust io-context crate - #8030
Draft
bartlomieju wants to merge 2 commits into
Draft
bartlomieju wants to merge 2 commits into
bartlomieju wants to merge 2 commits into
Conversation
This was referenced Oct 27, 2026
Rust APIs need it for failures that only a bug causes, such as an IoContext that is no longer current. Throwing the error gives JavaScript a generic "internal error; reference = <id>" Error and logs the message with that ID. A new test checks the redaction.
IoContext::await_io() waits for a Rust future as part of the request, then runs a callback with its output in the IoContext. It goes through IoContext::awaitIoAndRun(), a new public variant of awaitIo() for callers that settle no jsg::Promise. Both now share the I/O half of awaitIoImpl(), split out as resumeAfterIo(), so the callback runs like awaitIo()'s does: holding a pending event, in the critical section and async context frame current when the wait began, and recorded for async tracing. If the request ends first, the future and callback are dropped without the callback running. IoContext::add_object() gives the IoContext ownership of a value, as C++ addObject() does. It returns IoOwned<T>, from which the value can only be taken out, rather than used in place as IoOwn<T> is.
bartlomieju
force-pushed
the
biwanczuk/rust-io-context-await-io
branch
from
October 28, 2026 21:58
05d31f8 to
42efd1c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Stacked on #7963; only the last two commits are new here.
This gives Rust APIs the two
IoContextoperations that portingMemoryCache.read()to Rust needs: waiting for I/O and resuming therequest afterwards, and tying a value's lifetime to the request.
IoContext::await_io()await_io(future, callback)waits for a Rust future as part of therequest, then calls
callbackwith its output in theIoContext,with a new lock. The future may complete on any thread; the callback
runs on the
IoContext's thread. If the request ends first, both aredropped without the callback running.
It goes through a new
IoContext::awaitIoAndRun(), a public variantof
awaitIo()for callers that settle nojsg::Promise. Both sharethe I/O half of
awaitIoImpl(), split out unchanged as the privateresumeAfterIo(), so the callback runs asawaitIo()'s continuationdoes:
IoContextbegan
not whichever is current when the future completes
awaitIobridge that causes theturn running the callback
awaitIoImpl()keeps its JS-settling half and callsresumeAfterIo()for the rest; its behaviour is unchanged.ExceptionOr<T>moves to the public section, sinceawaitIoAndRun()'s callback receives one.IoContext::add_object()add_object(value)gives theIoContextownership of a value, asC++
addObject()does, and returns anIoOwned<T>. Its lifetimerules match
IoOwn<T>'s, including deferring the drop to theIoContextwhen the handle is dropped while it is not current, butaccess differs: the value can only be moved out with
take(), whichreturns
Noneonce theIoContextis gone. The type's documentationcompares the two.
jsg::Error::new_internal_error()This is now public, for failures that only a bug causes, such as
calling these methods on an
IoContextthat is no longer current.Throwing it gives JavaScript a generic
internal error; reference = <id>and logs the message with that ID.Tests
io-context-test.c++runs these in requests of a real worker:runs the callback
its handle is dropped: at once if the request is current,
otherwise the next time the request runs
A
jsg-testtest checks that a thrown internal error is redacted.