Repository navigation
Move TestController to Rust and deliver test() through the entrypoint crate - #8736
Conversation
8373c12 to
9e1e37b
Compare
9e1e37b to
38ea0fa
Compare
a8830dc to
e733315
Compare
38ea0fa to
56eda18
Compare
141f6c1 to
9683966
Compare
e733315 to
4d7854d
Compare
a5576be to
71d1d12
Compare
4d7854d to
07f69d5
Compare
There was a problem hiding this comment.
Superseded by the review of
67b417a6.
This review, of 07f69d53
💬 Approved with comments — The test event behaves as it did in C++ and is well tested. The only finding is an INFO about a module doc comment that contradicts the code.
Findings: 1 info (1 is an inline comment on the diff)
Not opened by the correctness review: 2 changed files it was meant to read
src/workerd/io/io-context/BUILD.bazelsrc/workerd/io/io-context/test-bridge.h
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
📋 Code Quality: 💬 1 info
Behaviour matches the C++ path. One module doc comment contradicts the code.
Recommendation: 💬 Optional Nits
- [INFO] The
test-api/lib.rsmodule doc saystestis not a member ofExportedHandler, but this pull request keeps it there as an untypedV8Ref<v8::Function>. (src/workerd/api/test-api/lib.rs:9)
View full review
The module doc in src/workerd/api/test-api/lib.rs:9-11 says test "isn't a member of C++'s ExportedHandler". It still is: global-scope.h:546 holds it as jsg::LenientOptional<jsg::V8Ref<v8::Function>> test, and it is read when the export is loaded, which test-controller-test.js checks. The comment on global-scope.h describes this correctly.
The error paths, the tracing and the handle lifetimes in getTestHandles behave the same as the C++ path did.
✅ All other checks passed
🔒 Security: ✅ LGTM
No security problems. Only test runners deliver the test event. getTestHandler() keeps both existing checks, and staticName() checks span names from Rust before turning them into a kj::StringPtr.
📖 AGENTS.md: ✅ LGTM
The changes to src/workerd/io/entrypoint/AGENTS.md match the code.
⚡ Performance: ✅ LGTM
Only test runners use this event path, and no hot path is affected.
📝 Documentation: ✅ LGTM
No issues found.
Reviewed commit: 07f69d53
39545b5 to
67b417a
Compare
There was a problem hiding this comment.
Superseded by the review of
77f2d9f0.
This review, of 67b417a6
💬 Approved with comments — The test event still behaves as it did in C++ and the earlier doc note is fixed. The only finding is an INFO about two comment lines longer than 100 columns.
Findings: 2 info (2 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.
📋 Code Quality: 💬 2 info
The earlier module-doc note is fixed; two new header comments have lines longer than 100 columns.
Recommendation: 💬 Optional Nits
- [INFO] A comment line in
getTestHandles()'s doc is 152 columns, over the 100-column limit. (src/workerd/io/entrypoint-invocation.h:55) - [INFO] A comment line in
incoming_request_run_in_wait_until()'s doc is 122 columns, over the 100-column limit. (src/workerd/io/io-context/ffi.h:67)
View full review
The module doc in src/workerd/api/test-api/lib.rs now correctly describes ExportedHandler::test and getTestHandler().
In two new header comments, a line runs past the 100-column limit:
src/workerd/io/entrypoint-invocation.h:55(152 columns)src/workerd/io/io-context/ffi.h:67(122 columns)
Running just format on both files fixes this.
The rest of the change behaves as the C++ path did:
- Handler lookup keeps both checks.
- The prelude runs before Rust holds the lock.
- JavaScript errors, KJ errors and panics are handled correctly.
- The event is reported, delivered, run and finished in the same order as before.
✅ All other checks passed
🔒 Security: ✅ LGTM
No security problems. Only test runners deliver the test event, so production requests cannot reach this path.
📖 AGENTS.md: ✅ LGTM
The changes to src/workerd/io/entrypoint/AGENTS.md match the code.
⚡ Performance: ✅ LGTM
Only test runners use this event path, so no hot path is affected.
📝 Documentation: ✅ LGTM
No issues found.
Reviewed commit: 67b417a6
The test event's handler is called from Rust (next commit), as
WorkerEntrypoint::test() calls it in C++:
context.addWaitUntil(context.run([...](Worker::Lock& lock,
IoContext& context) {
TRACE_EVENT("workerd", "WorkerEntrypoint::test() run");
auto traceScope = context.makeRequestTraceScope(lock);
...
}));
io_context binds that as incoming_request_run_in_wait_until(), next to
its other IoContext::IncomingRequest calls. In IoContext::run(), under
the Perfetto span that the prelude names and in the request's trace
scope, it runs a C++ RunPrelude, then a Rust function on the handles the
prelude returned, then waits for the future the function returns. The
prelude is the C++ work that the lambda does before the call, such as
finding the exported handler, which can run JavaScript and throw: it
runs before Rust holds the lock, so no C++ JSG runs under a Rust-held
lock, and an exception it throws fails the run as from C++ code run()
runs. The prelude also names the span, with a C++ string literal, so the
lambda captures only owning values.
The function fails with an io_context::RunError: a JavaScript error is
thrown, so that run() logs it as uncaught, as for C++ code it runs, and
a KJ exception reaches run() unchanged, unless a JavaScript exception or
a termination is pending: that one must keep propagating, so the run
fails with it, as for a JavaScript error, and the KJ exception is
dropped. A panic in the function terminates execution and fails the task
with a KJ exception, as for io_context_await_io_and_run(); one in its
future fails the task through kj-rs, as for any Rust future that C++
awaits.
The run's outcome is handled in io-context/ffi.c++, with the reviewed
throwPendingToRun() that await_io_and_run() already uses, so the C++ JSG
allowlist doesn't change.
It also binds the tracer call the test event makes,
incoming_request_set_custom_event_info(), shaped like
incoming_request_set_scheduled_event_info().
io-context-test covers the prelude's handles reaching the function in
the request's IoContext, a prelude that throws a JavaScript exception
(the function is dropped unrun), the function throwing and failing with
a KJ exception, with the exception that run() logs as uncaught, failing
with a KJ exception while a JavaScript exception is pending, and
panicking, its future panicking, and its being dropped unrun when the
request is destroyed before the isolate lock is taken.
… crate TestController, the first argument of a test() handler, is a Rust #[jsg_resource] in the new crate src/workerd/api/test-api, whose run() function calls a test() handler on its export with a new controller, env and ctx, and converts what the handler returns as C++ JSG converts a jsg::Promise<void>. Its TypeScript declaration is written by hand, next to ExecutionContext's, where the generated one was: Rust RTTI reaches the type generator only through module registration, so a Rust resource can't join the global scope's types. The generated types are unchanged. WorkerEntrypoint::test() hands the event to entrypoint::runTest(), still wrapped with the canceler. test.rs reports it to the tracer as a custom event, delivers it as "test", and, in one waitUntil task, calls the handler's test() with test_api::run() and waits for the promise with await_js(). It then finishes the request with finishScheduled() and resolves with whether the outcome is OK. Delivered::run_test_in_wait_until() runs the function with io_context::incoming_request_run_in_wait_until(), whose C++ prelude finds the handler and its test(): the shim takes the entrypoint name, version info and props out of the Invocation, as the C++ lambda captured them, into a prelude that calls getTestHandles(), in io beside Invocation, and returns test(), the handler, env and ctx. The prelude runs in IoContext::run() before Rust holds the lock, so no C++ JSG runs under a Rust-held lock, and what a class entrypoint's constructor throws propagates as from the C++ lambda: run() logs it as uncaught, as before. The crate's shims use no C++ JSG, and the allowlist is unchanged. The test event behaves as it did in C++. ExportedHandler keeps its test member, read from the export when it is loaded, as its other handlers are, but untyped, as a function: a jsg::Function signature can't name the Rust TestController. ServiceWorkerGlobalScope::test() becomes getTestHandler(), which makes the same two JSG_REQUIRE_NONNULL checks, for Service Workers syntax and for an export without a test() function, and returns the function; the prelude calls it, so both failures are the exceptions C++ threw, from the same function. The only difference is where the log lines of a test whose promise rejects come from: Rust handles the rejection, so they name io-context/ffi.c++ and rust/jsg/lib.rs rather than worker.c++ and value.h. Only --verbose shows them. The Perfetto spans keep their names and extents. "WorkerEntrypoint::test() run" is the run's span. "WorkerEntrypoint::test() waitForFinished()" covers the whole wait, as the C++ coroutine's TRACE_EVENT did across its co_await: it is a perfetto::AsyncSlice, begun before finish_scheduled() and ended when the wait does, on a track of its own, since a thread-track slice can't stay open across an .await. test-event-perfetto-test checks that each name is in the trace; "WorkerEntrypoint::test()" is a prefix of the other two, so it must not be followed by a space. There is no autogate: only test runners deliver the test event. The test-api crate's unit tests cover the controller and the conversion of results. test-controller-test checks the controller's shape and how test() is called, with and without immutable_api_prototypes, including that test() is read once, when the export is loaded, so replacing it later doesn't change what runs. server-test checks how failures are reported, now including a class entrypoint whose constructor throws. server/tests/test-event runs workerd test on tests that pass, fail asynchronously, or use Service Workers syntax, and checks the verdicts, the async trace and how each failure is logged. These tests pass against the C++ path as well.
67b417a to
77f2d9f
Compare
There was a problem hiding this comment.
✅ Approved — Both earlier comment-length notes are fixed and the files changed since the last review raise no new issues. The test event path still matches the C++ behaviour.
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
Only test runners deliver the test event, and the change adds no new entry point and no new input a caller controls. The handler-lookup checks are unchanged, and the lookup still runs before Rust takes the lock.
📖 AGENTS.md: ✅ LGTM
The changes to src/workerd/io/entrypoint/AGENTS.md match the code.
📋 Code Quality: ✅ LGTM
The doc comments in src/workerd/io/entrypoint-invocation.h and src/workerd/io/io-context/ffi.h now wrap within 100 columns.
The code in the files changed since the last review still behaves as the C++ path did:
getTestHandler()still makes bothJSG_REQUIRE_NONNULLchecks. BecauseExportedHandler::testis an untypedLenientOptional<V8Ref<v8::Function>>, atestthat is not a function still leaves it empty.getTestHandles()returns the receiver, env and ctx in the lock'sHandleScope. ctx is undefined when the handler has none.- A KJ error while a JavaScript exception or termination is pending fails the run, so the pending exception keeps propagating.
WorkerEntrypoint::test()keeps the same event order and thewaitForFinishedspan.
⚡ Performance: ✅ LGTM
Only test runners use this event path, so no hot path is affected.
📝 Documentation: ✅ LGTM
No issues found.
Reviewed commit: 77f2d9f0
The test event is the first event whose handler call runs in Rust, not
just its delivery.
TestController, the first argument of atest()handler, becomes a Rust
#[jsg_resource]in the newtest-apicrate,and
WorkerEntrypoint::test()hands the event to theentrypointcrate, which reports it to the tracer, delivers it, calls
test()in awaitUntil task and finishes the request with
finishScheduled(). Thereis no autogate: only test runners deliver the test event, so no
production request reaches this path. The first commit adds what the
event needs from
io_context,incoming_request_run_in_wait_until(),which is
context.addWaitUntil(context.run(...)). It runs a C++prelude, which finds the handler and its
test(), before Rust holds thelock, so no C++ JSG runs under a Rust-held lock and a throwing class
constructor fails the run as in C++. A JavaScript error from the Rust
function is logged as uncaught, a KJ exception reaches
run()unchangedunless a JavaScript exception is pending, which then wins, and the
crate's shims use no C++ JSG.
The event behaves as it did in C++, and production Workers see no
change.
ExportedHandlerkeeps itstestmember, read when the exportis loaded, but untyped, since a
jsg::Functionsignature can't name theRust
TestController. The oldServiceWorkerGlobalScope::test()becomes
getTestHandler(), which makes the same twoJSG_REQUIRE_NONNULLchecks, so Service Workers syntax and an exportwithout
test()fail with the exceptions C++ threw. The one differenceis that, for a test whose promise rejects, the
--verboselog linescome from
io-context/ffi.c++andrust/jsg/lib.rsrather thanworker.c++andvalue.h, since Rust handles the rejection. ThePerfetto spans keep their names and extents:
WorkerEntrypoint::test() waitForFinished()covers the whole wait, as in C++, on a track of itsown.
io-context-testcovers the run's prelude, failures and panics,test-api's unit tests cover the controller, andtest-controller-test,server-testandserver/tests/test-eventcover how
test()is called and how failures are reported. Those threealso pass against the C++ path on main.