Skip to content

Make WorkerEntrypoint::construct() return a Rust WorkerEntrypoint - #8745

Draft
bartlomieju wants to merge 1 commit into
biwanczuk/rust-entrypoint-scheduled-eventfrom
biwanczuk/rust-worker-entrypoint
Draft

bartlomieju wants to merge 1 commit into
biwanczuk/rust-entrypoint-scheduled-eventfrom
biwanczuk/rust-worker-entrypoint

Conversation

@bartlomieju

@bartlomieju bartlomieju commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

We're moving WorkerEntrypoint (the C++ object that receives each event
for a Worker's exported handler) to Rust one event at a time.

Until now each ported event needed its own gate inside the C++ object
and its own pair of bridge functions. This makes the Rust object the
entrypoint itself: behind the new RUST_WORKER_ENTRYPOINT autogate,
WorkerEntrypoint::construct() returns a Rust
entrypoint::WorkerEntrypoint that owns the C++ one. Later events
become methods of the Rust object, and the C++ object shrinks until it
can be removed. The new gate replaces RUST_ENTRYPOINT_SCHEDULED from
#8734.

The C++ object is seen from Rust as an entrypoint::CxxEntrypoint: a
WorkerInterface that can also hand over the Invocation of its one
event. C++ callers reach the Rust object through RustWorkerEntrypoint,
a small WorkerInterface in entrypoint/ffi.c++.

Currently it sends runScheduled() to Rust and every other event
straight to the C++ object, through a reference it takes once when it is
created. So request() and connect() cross no FFI boundary and await
no Rust future, and the gate costs them one allocation per event.

RustWorkerEntrypoint takes the C++ object's place in the
RequestObserver's wrapper, so metrics see the same calls. The Rust
object doesn't implement worker::Interface yet: nothing in Rust calls
it, and once request() is in Rust, RustWorkerInterface can replace
RustWorkerEntrypoint.

The scheduled event takes the same steps as before, now from the Rust
object. Two checks moved with it and differ only in their messages,
which a Worker can't observe: the invocation can be taken only once ("a
WorkerEntrypoint delivers only one event", where C++ says
"runScheduled() can only be called once"), and the request must not
belong to an actor, which runScheduled() also asserts. Both fail only
on a bug in the runtime. The actor check is a call on the request, so it
is the io-context crate's binding of IoContext::IncomingRequest, next
to the other calls the scheduled event makes on it. The span
WorkerEntrypoint::runScheduled() is now emitted by
RustWorkerEntrypoint, so traces of both paths still have it.

Every test's @all-autogates variant now delivers its events through
the Rust WorkerEntrypoint, which covers forwarding of every event
type. api/tests:scheduled-test and server/tests/scheduled check the
scheduled event on both paths, and scheduled-perfetto-test checks the
spans of each. I compared the request case of bench-global-scope, which
sends a fetch through newWorkerEntrypoint(), with and without this
change: in 12 interleaved runs with all autogates on, the medians were
6.47 µs and 6.51 µs per request, within the noise. The full test suite
passes.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.53488% with 9 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (biwanczuk/rust-entrypoint-scheduled-event@56eda18). Learn more about missing BASE report.

Files with missing lines Patch % Lines
src/workerd/io/entrypoint/ffi.c++ 84.09% 6 Missing and 1 partial ⚠️
src/workerd/io/entrypoint/lib.rs 92.85% 1 Missing ⚠️
src/workerd/io/worker-entrypoint.c++ 94.44% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@                             Coverage Diff                              @@
##             biwanczuk/rust-entrypoint-scheduled-event    #8745   +/-   ##
============================================================================
  Coverage                                             ?   67.44%           
============================================================================
  Files                                                ?      836           
  Lines                                                ?   212892           
  Branches                                             ?    32486           
============================================================================
  Hits                                                 ?   143585           
  Misses                                               ?    50246           
  Partials                                             ?    19061           

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

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

LGTM. Verified locally:

  • RustWorkerEntrypoint forwards all nine WorkerInterface virtuals, including the three with default bodies (abandonAlarm, test, bench).
  • Skipping wrapWithCanceler on the Rust path is equivalent: the canceler is only set for actors, and assert_no_actor runs first.
  • Moving entrypointName in takeInvocation() rather than cloning is fine, nothing reads it after the one event, and the single-event check holds in both orders (Rust then C++, C++ then Rust).
  • scheduled-test@ / @all-autogates, both server/tests/scheduled tests, worker-entrypoint-test@all-autogates and cpp-jsg-allowlist-test pass.

@bartlomieju
bartlomieju force-pushed the biwanczuk/rust-entrypoint-scheduled-event branch from 56eda18 to 141f6c1 Compare November 23, 2026 10:28
Behind the new RUST_WORKER_ENTRYPOINT autogate, which replaces
RUST_ENTRYPOINT_SCHEDULED, WorkerEntrypoint::construct() returns a Rust
entrypoint::WorkerEntrypoint that owns the C++ WorkerEntrypoint, in the
RequestObserver's wrapper in place of the C++ object. Events that Rust
implements are delivered by the Rust object; for now that is the
scheduled event, which C++ WorkerEntrypoint::runScheduled() no longer
gates. Later events move into it one at a time, and the C++ object
shrinks until it can go.

The C++ object is seen from Rust as an entrypoint::CxxEntrypoint
(io/entrypoint-invocation.h): a WorkerInterface whose takeInvocation()
hands over the Invocation of its one event. C++ callers reach the Rust
object through RustWorkerEntrypoint (entrypoint/ffi.c++), which sends
runScheduled() to Rust and every other event straight to the C++
object, through a reference it takes once when it is made. So request()
and connect() cross no FFI boundary and await no Rust future; the cost
of the gate on them is one allocation per event. The Rust object does
not implement worker::Interface yet: nothing in Rust calls it, and once
request() is in Rust, RustWorkerInterface can replace
RustWorkerEntrypoint.

The scheduled event takes the steps it took behind the old gate, from
the Rust object. Its checks differ only in their messages: the
invocation is taken once ("a WorkerEntrypoint delivers only one event")
and the request must not be an actor's, as runScheduled() asserts. That
check is a call on the request, so it is io-context's binding of
IoContext::IncomingRequest, next to the others the scheduled event
makes. The span WorkerEntrypoint::runScheduled() is emitted by
RustWorkerEntrypoint, so both paths still have it.

Every test's @all-autogates variant now delivers its events through the
Rust WorkerEntrypoint, which covers the forwarding of each event type;
api/tests:scheduled-test and server/tests/scheduled check the scheduled
event on both paths, and scheduled-perfetto-test the spans of each.
@bartlomieju
bartlomieju force-pushed the biwanczuk/rust-entrypoint-scheduled-event branch from 141f6c1 to 9683966 Compare November 23, 2026 14:13
@bartlomieju
bartlomieju force-pushed the biwanczuk/rust-worker-entrypoint branch from eb554bc to 7f8adcc Compare November 23, 2026 14:13
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.

3 participants