Skip to content

Add IoContext::then(), catch() and then_or_catch() to the Rust io-context crate - #8733

Merged
bartlomieju merged 7 commits into
mainfrom
biwanczuk/rust-io-context-then
Nov 21, 2026
Merged

bartlomieju merged 7 commits into
mainfrom
biwanczuk/rust-io-context-then

Conversation

@bartlomieju

@bartlomieju bartlomieju commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

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's
reactions capture nothing and carry their state as a separate data
value, so there is no closure for an addFunctor() to wrap. These
three methods are the Rust counterpart: they register the reactions
themselves. workerd bench's BenchController, which is moving to
Rust, is the first user.

Like addFunctor(), the methods require the IoContext to be current
when they're called. A reaction fails without running its callback if
it runs while another IoContext, or none, is current. Each callback
gets the IoContext, as addFunctor() passes it to functors that take
one. The methods are built on the existing Rust JSG promise API, which
is unchanged.

There are two deliberate differences from addFunctor(), and neither
is 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 IoContext owning it. That lets the data hold jsg::Rcs and
TracedGlobals and lets cycles through them be collected, which an
IoContext-owned value can't do. Second, the error a reaction fails
with lacks the C++ " (I/O type: ...)" suffix, because there is no C++
functor type to name. The comment on C++ addFunctor() and
src/workerd/io/AGENTS.md now point Rust code at the new methods.

The tests cover each method running in its own IoContext and failing
in another, the check at registration, which IoContext the data is
dropped in, that the data is traced, and behavior both with and without
handle_cross_request_promise_resolution.

…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.
@bartlomieju
bartlomieju requested review from a team as code owners November 20, 2026 10:29

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:48 lists then() / then_or_catch() as the Rust counterpart but not catch().

📋 Code Quality: 💬 1 info · 1 suggestion

The implementation is correct and well tested; one doc paragraph needs re-wrapping.

Recommendation: 💬 Optional Nits

  • [INFO] The ReactionData doc names then and then_or_catch as its users but leaves out catch, 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 the then() doc, the sentence about when data is 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

Comment thread src/workerd/io/io-context/lib.rs Resolved
Comment thread src/workerd/io/io-context/lib.rs Resolved
Comment thread src/workerd/io/AGENTS.md Resolved
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.

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 when data is dropped is now re-wrapped.
  • The ReactionData doc now names IoContext::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

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() and then_or_catch() still check that their IoContext is current when the reaction is registered, and ReactionData::require_current checks it again when the reaction runs.
  • ReactionData still passes trace and needs_drop_detach through to the data it wraps.
  • The compile-time zero-size checks on the callbacks are still in place.
  • record_drop_context, reactionDataDropContextId and register_reaction match across test-bridge.h, test-bridge-state.h, test-bridge.c++, test_bridge.rs and io-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 guybedford left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. One doc correction and two nits inline.

Comment thread src/workerd/io/io-context/lib.rs Resolved
Comment thread src/workerd/io/io-context/lib.rs Resolved
Comment thread src/workerd/io/io-context/lib.rs Resolved
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.

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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 InContext rename is applied consistently.
  • The registration check and the check when a reaction runs are both still in place.
  • trace still 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.rs and io-context-test.c++.
  • The MODULE.bazel.lock change 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

@bartlomieju
bartlomieju merged commit e4311cb into main Nov 21, 2026
24 checks passed
@bartlomieju
bartlomieju deleted the biwanczuk/rust-io-context-then branch November 21, 2026 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants