Repository navigation
Add IoContext::then(), catch() and then_or_catch() to the Rust io-context crate - #8733
Conversation
…text crate C++ registers a promise continuation that must run in the IoContext that created it as promise.then(js, ioctx.addFunctor(func)). Creating the wrapper requires that IoContext to be current; the wrapper throws if the continuation runs while another IoContext, or none, is current, and passes the IoContext to a functor that takes one. Rust JSG's reactions capture nothing and get their state through a data value, so there is no closure for an addFunctor() to wrap. Instead, ctx.then(), ctx.catch() and ctx.then_or_catch() register the reactions themselves. They require the IoContext to be current, hold the data with its id, check that it is current again before calling the callbacks, and pass it to them before the data, as addFunctor() passes it after jsg::Lock&. What passes through without a callback, a rejection for then() and a fulfillment for catch(), does so in any IoContext, as in C++. The checking reactions are closures that capture only the callbacks, which capture nothing, so they meet Rust JSG's requirements for a reaction as they are, and Rust JSG is unchanged. A callback that captures something fails to compile at the call. Two differences from addFunctor(). The data is held by the reaction, which GC traces, rather than by the IoContext, so it may hold jsg::Rcs and TracedGlobals, and a cycle through them can be collected; it is dropped once a reaction has run, which may be in another request's IoContext. And the error a reaction fails with outside its IoContext is the one IoContext throws for an I/O object of another request, also when no IoContext is current, without the " (I/O type: ...)" suffix the C++ one has. With handle_cross_request_promise_resolution, which is on by default, V8 runs a reaction that another request queues in the IoContext that created the promise, or never once it is gone, so the check fires for promises created outside the registering request. The tests cover both that and the behavior without the flag, the registration check, where the data is dropped, and that the data is traced. The comment on C++ addFunctor() and src/workerd/io/AGENTS.md point Rust code at the new methods. workerd bench's BenchController, which is moving to Rust, is the first user.
There was a problem hiding this comment.
Superseded by the review of
8843de83.
This review, of eba946f5
💬 Approved with comments — I found no functional issues. The only findings are doc-comment nits: one line in the then() doc needs re-wrapping, and two places that list the new methods leave out catch().
Findings: 1 info · 2 suggestions (3 are inline comments on the diff)
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
📖 AGENTS.md: 💬 1 suggestion
The new I/O guidance does not mention catch().
Recommendation: 💬 Optional Nits
- [SUGGESTION] The Rust counterpart guidance leaves out
IoContext::catch(). (src/workerd/io/AGENTS.md:48)
View full review
src/workerd/io/AGENTS.md:48liststhen()/then_or_catch()as the Rust counterpart but notcatch().
📋 Code Quality: 💬 1 info · 1 suggestion
The implementation is correct and well tested; one doc paragraph needs re-wrapping.
Recommendation: 💬 Optional Nits
- [INFO] The
ReactionDatadoc namesthenandthen_or_catchas its users but leaves outcatch, which uses it too. (src/workerd/io/io-context/lib.rs:1213) - [SUGGESTION] The
then()doc paragraph has one line that was not re-wrapped and runs far past 100 columns. (src/workerd/io/io-context/lib.rs:374)
View full review
src/workerd/io/io-context/lib.rs:374: in thethen()doc, the sentence about whendatais dropped was not re-wrapped and runs well past the ~100-column width of the lines around it.
The zero-sized F: Copy bound, the forwarding of trace and needs_drop_detach in the wrapper, the context check at both registration and run time, and the documented drop-context behavior all hold.
✅ All other checks passed
🔒 Security: ✅ LGTM
No security issues found. The new methods require the registering IoContext to be current, and they check it again before running a callback.
⚡ Performance: ✅ LGTM
Each call allocates one small wrapper and runs one context check when the reaction runs. The trace hook only forwards to the wrapped data. There are no loops and no blocking I/O.
📝 Documentation: ✅ LGTM
The doc comments match the code, apart from the nits noted in the other sections.
Reviewed commit: eba946f5
The comment on C++ addFunctor(), src/workerd/io/AGENTS.md and the doc of ReactionData named only then() and then_or_catch(). Also rewrap a line of the then() doc that ran past the width of the rest.
There was a problem hiding this comment.
Superseded by the review of
672b5710.
This review, of 8843de83
✅ Approved — The earlier doc nits are fixed and this commit raises no new issues.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
No security issues found.
📖 AGENTS.md: ✅ LGTM
src/workerd/io/AGENTS.md now lists catch() alongside then() and then_or_catch().
📋 Code Quality: ✅ LGTM
Both earlier notes are fixed:
- The sentence in the
then()doc about whendatais dropped is now re-wrapped. - The
ReactionDatadoc now namesIoContext::catch.
then(), catch() and then_or_catch() each check that their IoContext is current twice: when the reaction is registered and again when it runs. The zero-size bound on callbacks is checked at compile time, and ReactionData passes trace and needs_drop_detach through to the data it wraps.
⚡ Performance: ✅ LGTM
Each call allocates one small wrapper, and each reaction runs one context check when it fires. The trace hook only passes through to the wrapped data.
📝 Documentation: ✅ LGTM
The doc comments match the code.
Reviewed commit: 8843de83
…text-then # Conflicts: # src/workerd/io/io-context/lib.rs # src/workerd/io/io-context/test-bridge.h # src/workerd/io/io-context/test_bridge.rs
There was a problem hiding this comment.
Superseded by the review of
5b1577e6.
This review, of 672b5710
✅ Approved — No findings after the merge from main. The new IoContext methods still check the current context both when a reaction is registered and when it runs, and the test bridge still matches across C++ and Rust.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
No security issues found. A reaction that runs under another IoContext, or under none, fails without running its callback.
📖 AGENTS.md: ✅ LGTM
src/workerd/io/AGENTS.md lists then(), catch() and then_or_catch() correctly.
📋 Code Quality: ✅ LGTM
After the merge from main:
then(),catch()andthen_or_catch()still check that theirIoContextis current when the reaction is registered, andReactionData::require_currentchecks it again when the reaction runs.ReactionDatastill passestraceandneeds_drop_detachthrough to the data it wraps.- The compile-time zero-size checks on the callbacks are still in place.
record_drop_context,reactionDataDropContextIdandregister_reactionmatch acrosstest-bridge.h,test-bridge-state.h,test-bridge.c++,test_bridge.rsandio-context-test.c++.
⚡ Performance: ✅ LGTM
Each call allocates one small wrapper, and each reaction runs one context check when it fires. The trace hook only passes through to the wrapped data.
📝 Documentation: ✅ LGTM
The doc comments match the code.
Reviewed commit: 672b5710
guybedford
left a comment
There was a problem hiding this comment.
Looks good to me. One doc correction and two nits inline.
Who created the promise never decides that. A reaction on a promise another request created goes through the cross-context follow callback, whose wait resolves in the registering IoContext, whether or not handle_cross_request_promise_resolution is on. Only settlement matters, and only without the flag. Cover the flag-on case: a promise created and settled in one request, with the reaction registered in another, runs it in the registering IoContext. registerReaction() takes the request to create the promise in.
data may be dropped in another request's IoContext, or in none, so an I/O object that an addFunctor() functor would capture belongs in an IoOwned inside it, which drops the object in its own IoContext.
The reaction data of JSG's promise.rs is also named ReactionData, and this one ends up nested inside it. InContext says what it adds: the IoContext the reactions run in.
There was a problem hiding this comment.
✅ Approved — No findings. The changes since the last review are a doc correction, a doc addition, a rename and a new test, and all of them match the code.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
A reaction that runs under another IoContext, or under none, fails without running its callback. The test bridge additions are test-only.
📖 AGENTS.md: ✅ LGTM
src/workerd/io/AGENTS.md lists then(), catch() and then_or_catch() correctly.
📋 Code Quality: ✅ LGTM
- The
InContextrename is applied consistently. - The registration check and the check when a reaction runs are both still in place.
tracestill passes through to the wrapped data.- The compile-time zero-size checks on the callbacks are still there.
- The test bridge declarations match across
test-bridge.h,test-bridge-state.h,test-bridge.c++,test_bridge.rsandio-context-test.c++. - The
MODULE.bazel.lockchange only updates the capnproto pin and its digest.
⚡ Performance: ✅ LGTM
Each call allocates one small wrapper, and each reaction does one context check when it fires. The trace hook only passes through to the wrapped data.
📝 Documentation: ✅ LGTM
The corrected doc on cross-request behaviour and the new IoOwned guidance match the code and the new test.
Reviewed commit: 5b1577e6
The motivation for this change is Rust ports that register promise
reactions which must run in the request that registered them. C++ does
this with
promise.then(js, ioctx.addFunctor(func)). Rust JSG'sreactions capture nothing and carry their state as a separate data
value, so there is no closure for an
addFunctor()to wrap. Thesethree methods are the Rust counterpart: they register the reactions
themselves.
workerd bench'sBenchController, which is moving toRust, is the first user.
Like
addFunctor(), the methods require theIoContextto be currentwhen they're called. A reaction fails without running its callback if
it runs while another
IoContext, or none, is current. Each callbackgets the
IoContext, asaddFunctor()passes it to functors that takeone. The methods are built on the existing Rust JSG promise API, which
is unchanged.
There are two deliberate differences from
addFunctor(), and neitheris visible to Workers, since nothing in production calls these methods
yet. First, the reaction holds the data and the GC traces it, rather
than the
IoContextowning it. That lets the data holdjsg::Rcs andTracedGlobals and lets cycles through them be collected, which anIoContext-owned value can't do. Second, the error a reaction failswith lacks the C++ " (I/O type: ...)" suffix, because there is no C++
functor type to name. The comment on C++
addFunctor()andsrc/workerd/io/AGENTS.mdnow point Rust code at the new methods.The tests cover each method running in its own
IoContextand failingin another, the check at registration, which
IoContextthe data isdropped in, that the data is traced, and behavior both with and without
handle_cross_request_promise_resolution.