Skip to content

Keep a dynamic worker's env channels in the server's channel table - #8920

Draft
bartlomieju wants to merge 1 commit into
biwanczuk/rust-io-channels-cratefrom
biwanczuk/rust-io-channels-table
Draft

bartlomieju wants to merge 1 commit into
biwanczuk/rust-io-channels-cratefrom
biwanczuk/rust-io-channels-table

Conversation

@bartlomieju

Copy link
Copy Markdown
Collaborator

The motivation for this change is to make the Rust server the only owner
of a worker's channel numbers. A dynamic worker loaded through a Worker
Loader had two channel tables: the server's Rust table couldn't hold a
C++ channel, so the factory kept the channels the worker's source
supplied (its global outbound and the capabilities in its env) in
three C++ tables, looked in them before asking the Rust table, and told
the Rust side how many there were so that it could number ctx.exports
after them. The server's SubrequestChannel and ActorClassChannel can
now be a C++ channel that the server only stores, held as an
io-channels type, and hand the same object back to C++, so a facet's
class check and channel tokens behave as before. The factory adds a
dynamic worker's channels to the server's table as it rewrites the
env, and the three C++ tables, their special cases in four
IoChannelFactory methods and the channel counts are gone.

One difference is observable. A dynamic worker is unlinked when its
loader drops its stub or the server shuts down, and a request of it
still in flight then, such as one that reached it through a
ctx.exports stub it handed out, used to lose its env channels and
fail with "invalid subrequest channel number" on its next subrequest
through one. It now keeps them until it finishes, as a config worker's
requests already keep theirs; a Worker can reach this only after its
loader has let it go, and the subrequest now succeeds instead of
failing. With the two builds alternated on one bazelflare executor, the
src/bench/subrequest benchmark shows no case slower beyond the noise
of comparing a build with itself, about 5%. The existing server, io,
channel-interop and Worker Loader tests cover the change.

@codecov-commenter

codecov-commenter commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.70950% with 22 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (biwanczuk/rust-io-channels-crate@88560fa). Learn more about missing BASE report.

Files with missing lines Patch % Lines
src/workerd/server/server/channels.rs 80.72% 16 Missing ⚠️
src/workerd/server/factory/worker-factory.c++ 85.71% 1 Missing and 5 partials ⚠️
Additional details and impacted files
@@                         Coverage Diff                         @@
##             biwanczuk/rust-io-channels-crate    #8920   +/-   ##
===================================================================
  Coverage                                    ?   67.77%           
===================================================================
  Files                                       ?      852           
  Lines                                       ?   216434           
  Branches                                    ?    32563           
===================================================================
  Hits                                        ?   146688           
  Misses                                      ?    50523           
  Partials                                    ?    19223           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

A dynamic worker had two channel tables. The Rust server's table
couldn't hold a C++ channel, so the factory kept the channels its
source supplied (its global outbound and the capabilities in its env)
in three C++ tables on CompiledWorker::Impl, and RustIoChannelFactory
checked them before it asked the Rust table. The Rust side was told
how many there were, so that it could number ctx.exports after them.

The server's SubrequestChannel and ActorClassChannel are now either one
of its own channels or a C++ channel it only stores, and the server's
table has an RPC table too. indexDynamicChannels() hands each channel to
a DynamicChannels builder as it rewrites the env, and the builder gives
it its number, so the server numbers every channel of a worker. A C++
channel goes back to C++ as itself rather than a handle around it, so a
facet whose class is a C++ one still fails with the facet error, and
only the server's own channels are ever wrapped in a handle; the handle
constructors assert that. A channel that is a handle around one of the
server's own is unwrapped as it comes in, as config workers store
theirs. The io-channels newtypes are now Unpin, which a bridge requires
of a Rust type that holds a channel and that C++ borrows mutably, such
as the builder.

This deletes the three C++ tables, envSubrequestChannel(), the env
cases in startSubrequest(), getSubrequestChannelResolved(),
getActorClassResolved() and getRpcChannel(), and the channel counts in
WorkerInfo with the offsets LinkedChannels kept for them. One
difference: worker_unlink() no longer drops a dynamic worker's env
channels itself. They go with the server's table when it unlinks the
worker, so a request still in flight keeps them until it finishes, as
a config worker's channels already were.

The subrequest benchmark, run with the order of the two builds
alternated on one bazelflare executor, shows no case slower beyond
the noise of comparing a build with itself (about 5%).
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