Repository navigation
JSSink: skip JS onClose/onPull when the VM has a pending termination - #36331
Conversation
When a Worker running Bun.serve() with a streamed ReadableStream body is terminated mid-stream, worker shutdown stops the server and tears down the in-flight HTTPResponseSink. That teardown fires the sink controller's JS onClose/onPull hook via AsyncContextFrame::call while the VM's sticky TerminationException is already pending, and Interpreter::executeCallImpl's scope.assertNoException() aborts the whole process on assertion-enabled builds. Guard the three generated JS-entry points (detach(), __onClose, __onReady) with vm.hasPendingTerminationException(), matching the established pattern in Worker.cpp and JSDOMPromiseDeferred.cpp. The non-JS cleanup in detach() (nulling the native pointer, running the destroy hook, notifying controllerDetached) still runs; only the JS callback is skipped, which only exists to transition the direct ReadableStream's JS-visible state and is moot once the VM is shutting down.
WalkthroughGenerated JS sink teardown and callbacks now avoid re-entering JavaScript when worker termination is pending. A debug-only regression test covers terminating workers while ChangesWorker termination guards
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@test/js/web/workers/worker-terminate-lifetime.test.ts`:
- Line 212: Update the cleanup flow containing rd.cancel() to await the
cancellation promise while still intentionally swallowing any rejection. Ensure
the fixture does not continue until reader cancellation and response-body
cleanup have settled.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 8351650f-b3f4-4c67-a53d-cb90583442e0
📒 Files selected for processing (2)
src/codegen/generate-jssink.tstest/js/web/workers/worker-terminate-lifetime.test.ts
|
Found 1 issue this PR may fix:
🤖 Generated with Claude Code |
|
Re #34690: that one is the |
…dy thunks
Belt alongside the generated C++ hasPendingTerminationException() guard:
short-circuits before the call_check_slow scope is even constructed,
matching the idiom in WebSocketServerContext.rs / socket/Handlers.rs.
JSSink::detach is intentionally not guarded here: it must still run
signal.clear(), unprotect(), and the C++ detach()'s native cleanup
(controllerDetached / onSinkDestroyed); the C++ detach() already skips
only the JS onClose when terminating.
Also apply review nits to the test: use the file's ${rounds} constant
(4 under debug) and assert stderr is empty like every sibling.
There was a problem hiding this comment.
LGTM — targeted termination-exception guards following the established pattern, with a regression test that fails on main.
What was reviewed:
- Verified the
vm.hasPendingTerminationException()guard placement indetach()runs after all native cleanup (m_sinkPtr nulling,Bun__onSinkDestroyed,controllerDetached, WriteBarrier clears) so only the JS onClose is skipped;__onClosestill clearsm_onClosebefore the guard so the call-once invariant holds. - Confirmed the pattern matches
NodeTimerObject.cpp:80/JSDOMExceptionHandling.cpp:149(C++) andsocket_body.rs:2039(Rusthas_exception()before entering JS on the sameassertNoExceptionfailure mode). - Checked that
JSSink::detach(Rust) is intentionally left un-guarded sosignal.clear()+unprotect()+ C++ native cleanup still run; the C++detach()guard covers that path's JS re-entry. - All four prior review comments (mine on
${rounds}andtoBe(""), CodeRabbit'sawait rd.cancel(), comment-cop's condensed comments) are applied in the current diff.
Extended reasoning...
Overview
Three files touched: generate-jssink.ts adds vm.hasPendingTerminationException() early-returns to the generated detach(), __onReady, and __onClose (× 6 sink classes) before AsyncContextFrame::call; Sink.rs adds global.has_exception() early-returns to the SinkSignal close/ready vtable thunks before call_check_slow; and a new debug-gated regression test in worker-terminate-lifetime.test.ts.
Security risks
None. This is a debug-assertion fix — release builds compile the tripped ASSERT out and were never affected. The guards only skip JS callbacks on a dying VM; no new code paths, no untrusted input handling.
Level of scrutiny
Medium. Worker termination + JSC exception scopes are subtle, but the change is a mechanical application of a pattern already used at 5+ sites in the codebase (NodeTimerObject.cpp, JSDOMExceptionHandling.cpp, Worker.cpp, socket_body.rs). The key correctness question — does skipping the JS onClose leak native state? — is answered by guard placement: in detach() all native teardown (m_sinkPtr = nullptr, Bun__onSinkDestroyed, ${name}__controllerDetached, clearing m_onPull/m_onClose/m_weakReadableStream) happens before the guard. In __onClose the m_onClose.clear() also precedes the guard. Only the JS-visible ReadableStream state transition is skipped, which is moot on a terminating VM.
Other factors
- The regression test was verified in the PR to abort on main (first iteration, 8/8) and pass with the fix; it now uses
${rounds},expect(stderr).toBe(""), and awaitsrd.cancel()per prior feedback, matching the file's conventions. serve-direct-readable-stream.test.ts(13 tests) was reported passing, covering the non-terminating onClose path.- The Rust-side
has_exception()guard is broader than the C++hasPendingTerminationException()(any pending exception vs. termination only), but that matches thesocket_body.rs:2039precedent and is defensively correct — enteringexecuteCallImplwith any pending exception trips the same assertion. - The PR author correctly declined to claim this fixes #34690 (different call site).
- No bugs found by the multi-agent review; all four prior inline comments resolved and applied.
…te assert Restructure the regression into a two-cell describe (http + https) sharing one spawn helper so both HTTPResponseSink__onClose and HTTPSResponseSink__onClose are exercised. Both cells abort on main with the same ExceptionScope::assertNoException() SIGABRT and pass with the codegen guard. The type:"direct" body cell was tried but hits a separate pre-existing assert (VMTraps::deferTerminationSlow's hasTerminationRequest() check, rooted at web_worker.rs shutdown clearing the request but not the exception) on both main and this branch; tracked separately.
There was a problem hiding this comment.
No issues found — all three earlier nits (loop count, stderr assertion, stale cell-count comment) are applied in the current diff. Deferring to a human for sign-off since this touches generated JSC bindings and worker-VM termination paths across all six sink classes.
What was reviewed:
- Verified the
vm.hasPendingTerminationException()guard matches the pattern inNodeTimerObject.cpp:80,Worker.cpp:709,JSDOMPromiseDeferred.cpp:236. - Verified the Rust
global.has_exception()early-return matchessocket/Handlers.rs:287andWebSocketServerContext.rs:87(same rationale comment). - Checked
detach()guard placement: native cleanup (m_sinkPtr,Bun__onSinkDestroyed,controllerDetached, WriteBarrier clears) still runs; only the JS onClose call is skipped. - Checked
__onClose:m_onClose.clear()still runs before the guard, so the call-once invariant holds.
Extended reasoning...
Overview
Three files: src/codegen/generate-jssink.ts (adds vm.hasPendingTerminationException() early-return in the generated detach(), __onReady, __onClose for all six sink classes), src/runtime/webcore/Sink.rs (adds global.has_exception() early-return in the SinkSignal close/ready vtable thunks), and a new debug-gated describe in test/js/web/workers/worker-terminate-lifetime.test.ts covering HTTPResponseSink and HTTPSResponseSink.
Security risks
None. The change only adds early-returns on a condition that is false outside worker termination; no new inputs are parsed and no trust boundaries are crossed.
Level of scrutiny
High. This is generated JSC bindings code on the worker-VM teardown path — REVIEW.md's most-blocked category. The guards are additive and verbatim copies of existing precedent (I confirmed NodeTimerObject.cpp, Worker.cpp, JSDOMPromiseDeferred.cpp on the C++ side and socket/Handlers.rs / WebSocketServerContext.rs on the Rust side use the identical idiom for the identical reason), and the guard placement in detach() is correct (all native cleanup runs first; only the JS callback is skipped). But the layering choice — Rust-side has_exception() (any exception) plus C++-side hasPendingTerminationException() (termination-only) as a double guard on the same path — and the decision to leave the type:"direct" / BunStreamSource.cpp sibling shapes for a follow-up are the kind of calls a maintainer who owns this area should confirm.
Other factors
I left three inline nits on earlier revisions (hardcoded 8 → ${rounds}, .not.toContain → .toBe(""), stale "Three cells" comment) and all were applied; the CodeRabbit await rd.cancel() suggestion and the comment-cop paragraph-length flags were also addressed. The bug-hunting system found nothing on the current revision. The test follows file-local conventions (concurrent subprocess cells, bunEnv, port: 0, pipes drained concurrently, skipIf(!isDebug)).
There was a problem hiding this comment.
No issues found, but this touches worker VM termination + JSC exception-scope handling across all 6 generated sink classes, so worth a human look.
Checked: the hasPendingTerminationException() guard matches NodeTimerObject.cpp:80 and the Rust has_exception() early-return matches WebSocketServerContext.rs:87 / socket/Handlers.rs:287. In detach(), native cleanup (m_sinkPtr null, Bun__onSinkDestroyed, controllerDetached) runs before the guard, so only the JS onClose is skipped — no leak. All prior review feedback (rounds interpolation, stderr toBe, stale cell count, awaited cancel) is applied in the current diff.
Extended reasoning...
Overview
Three files: generate-jssink.ts adds a vm.hasPendingTerminationException() guard at three JS re-entry points (detach(), __onReady, __onClose) shared by all 6 sink classes; Sink.rs adds a matching global.has_exception() early-return in the two SinkSignal vtable thunks; worker-terminate-lifetime.test.ts adds a debug-only regression describe with http/https cells.
Security risks
None. This is a defensive short-circuit on a VM-shutdown path; no user input parsing, no auth, no data exposure. The only behavior change is not calling a JS callback when the VM already has a pending (termination) exception — which would have tripped an assert or returned immediately anyway.
Level of scrutiny
Medium-high. The pattern is mechanical and verified against three cited in-tree precedents (NodeTimerObject.cpp, WebSocketServerContext.rs, socket/Handlers.rs), and the test reproduces the SIGABRT deterministically. But worker VM termination × JSC exception scopes × generated code that fans out to 6 sink classes is exactly the kind of lifecycle path where a maintainer should confirm the layering choice — in particular that the Rust-side broader has_exception() (vs. the C++ side's termination-specific check) is intentional defense-in-depth rather than an accidental over-broad skip, and that the PR's explicit deferral of the type:"direct" / VMTraps::deferTerminationSlow sibling is the right scoping call.
Other factors
All four prior review threads (CodeRabbit's awaited cancel, comment-cop's verbose comments, my ${rounds} / toBe("") / stale cell-count nits) are resolved and reflected in the current diff. The bug hunter found nothing this run. The detach() guard placement was checked specifically: the native-side cleanup (nulling m_sinkPtr, Bun__onSinkDestroyed, ${name}__controllerDetached) happens before the new early return, so skipping the JS onClose does not leak the sink pointer or the destroy callback.
|
CI status: build 85025 had one non-flaky red, |
What
A Worker running
Bun.serve()with a streamedReadableStreambody, terminated mid-stream, SIGABRTs the whole process on assertion-enabled builds:Stack:
assertNoException←Interpreter::executeCallImpl←executeBoundCall←JSC::call←AsyncContextFrame::call←HTTPResponseSink__onClose, aborting thread "Worker". The same holds forHTTPSResponseSink__onCloseover a TLS listener.Repro
Under
bun bdthis aborts on the first iteration 8/8. Release builds survive (theASSERTis compiled out).Cause
worker.terminate()sets the stickyTerminationExceptionon the worker VM. Worker shutdown (web_worker.rsclose_all_socket_groups) then stops everyBun.serve(), which aborts in-flight responses:RequestContext→HTTPServerWritable::abort→signal.close(None)→SinkSignalclose thunk → generatedHTTP(S)ResponseSink__onClose, which re-enters JS viaAsyncContextFrame::call→JSC::profiledCall→executeCallImpl.executeCallImplopens withscope.assertNoException(), and the already-pending termination exception trips it.None of
__onClose,__onReady, or the controller'sdetach()(all generated bygenerate-jssink.ts, shared by all 6 sink classes) checked for a pending exception before calling into JS.Fix
generate-jssink.ts: guarddetach(),__onClose, and__onReadywithvm.hasPendingTerminationException()beforeAsyncContextFrame::call, matching the pattern inWorker.cpp/NodeTimerObject.cpp/JSDOMPromiseDeferred.cpp.detach()'s native cleanup (nullingm_sinkPtr,Bun__onSinkDestroyed,controllerDetached) still runs; only the JS onClose is skipped, which only exists to transition the directReadableStream's JS-visible state and is moot once the VM is shutting down.Sink.rs: add aglobal.has_exception()early-return in theSinkSignalclose/readyvtable thunks, mirroring the idiom inWebSocketServerContext.rs/socket/Handlers.rs. Short-circuits before thecall_check_slowscope is constructed. The RustJSSink::detachis left alone because it must still clear the signal, unprotect the cell, and run the C++ native cleanup; the C++detach()guard handles that path.The
type:"direct"body cell was also tried but hits a separate pre-existing assert on main (VMTraps::deferTerminationSlow'shasTerminationRequest()check, rooted atweb_worker.rsshutdown clearing the request flag but not the sticky exception beforeclose_all_socket_groups); that is tracked separately.BunStreamSource.cpp'sreadDirectStreamCloseImplhas the same shape (pre-call no termination check at thesinkController.end()/cancel()calls) but is unreproduced; also left for a separate change.Verification
bun bd test test/js/web/workers/worker-terminate-lifetime.test.ts -t "does not trip assertNoException": both cells (httpHTTPResponseSink, httpsHTTPSResponseSink) fail on main (ASSERTION FAILED: !exception(), exit 134, first iteration every run), pass with fix (~13s concurrent).test/js/bun/http/serve-direct-readable-stream.test.ts: 13 pass (direct-stream onClose path unchanged for the non-terminating case).no test proof · iteration 1 · Platform-specific test(s) that do not run on this machine. Deferring to CI, which covers all platforms: test/js/web/workers/worker-terminate-lifetime.test.ts