Skip to content

JSSink: skip JS onClose/onPull when the VM has a pending termination - #36331

Merged
Jarred-Sumner merged 7 commits into
mainfrom
farm/dc5f9aab/sink-onclose-worker-terminate
Jul 29, 2026
Merged

Jarred-Sumner merged 7 commits into
mainfrom
farm/dc5f9aab/sink-onclose-worker-terminate

Conversation

@robobun

@robobun robobun commented Jul 29, 2026 •

Copy link
Copy Markdown
Collaborator

What

A Worker running Bun.serve() with a streamed ReadableStream body, terminated mid-stream, SIGABRTs the whole process on assertion-enabled builds:

ASSERTION FAILED: !exception()
ExceptionScope.h(61) : void JSC::ExceptionScope::assertNoException()

Stack: assertNoException ← Interpreter::executeCallImpl ← executeBoundCall ← JSC::call ← AsyncContextFrame::call ← HTTPResponseSink__onClose, aborting thread "Worker". The same holds for HTTPSResponseSink__onClose over a TLS listener.

Repro

const { Worker } = require("node:worker_threads");
const src = 'const server = Bun.serve({ port: 0, fetch() { let i = 0;' +
  '  return new Response(new ReadableStream({ async pull(c) {' +
  '    c.enqueue(new Uint8Array(8192).fill(i++)); if (i > 20000) c.close(); await Bun.sleep(0); } })); } });' +
  'require("node:worker_threads").parentPort.postMessage(server.port);';
for (let i = 0; i < 8; i++) {
  const w = new Worker(src, { eval: true });
  const port = await new Promise(r => w.once("message", r));
  const res = await fetch("http://127.0.0.1:" + port + "/");
  const rd = res.body.getReader();
  await rd.read();
  const done = new Promise(r => w.once("exit", r));
  w.terminate();
  await done;
  await rd.cancel().catch(() => {});
}
console.log("survived");

Under bun bd this aborts on the first iteration 8/8. Release builds survive (the ASSERT is compiled out).

Cause

worker.terminate() sets the sticky TerminationException on the worker VM. Worker shutdown (web_worker.rs close_all_socket_groups) then stops every Bun.serve(), which aborts in-flight responses: RequestContext → HTTPServerWritable::abort → signal.close(None) → SinkSignal close thunk → generated HTTP(S)ResponseSink__onClose, which re-enters JS via AsyncContextFrame::call → JSC::profiledCall → executeCallImpl. executeCallImpl opens with scope.assertNoException(), and the already-pending termination exception trips it.

None of __onClose, __onReady, or the controller's detach() (all generated by generate-jssink.ts, shared by all 6 sink classes) checked for a pending exception before calling into JS.

Fix

  • generate-jssink.ts: guard detach(), __onClose, and __onReady with vm.hasPendingTerminationException() before AsyncContextFrame::call, matching the pattern in Worker.cpp / NodeTimerObject.cpp / JSDOMPromiseDeferred.cpp. detach()'s native cleanup (nulling m_sinkPtr, Bun__onSinkDestroyed, controllerDetached) still runs; only the JS onClose is skipped, which only exists to transition the direct ReadableStream's JS-visible state and is moot once the VM is shutting down.
  • Sink.rs: add a global.has_exception() early-return in the SinkSignal close / ready vtable thunks, mirroring the idiom in WebSocketServerContext.rs / socket/Handlers.rs. Short-circuits before the call_check_slow scope is constructed. The Rust JSSink::detach is 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's hasTerminationRequest() check, rooted at web_worker.rs shutdown clearing the request flag but not the sticky exception before close_all_socket_groups); that is tracked separately. BunStreamSource.cpp's readDirectStreamCloseImpl has the same shape (pre-call no termination check at the sinkController.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 (http HTTPResponseSink, https HTTPSResponseSink) 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

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

robobun commented Jul 29, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 5:05 AM PT - Jul 29th, 2026

@robobun, your commit 3206f00 is building: #85038

@coderabbitai

coderabbitai Bot commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Generated JS sink teardown and callbacks now avoid re-entering JavaScript when worker termination is pending. A debug-only regression test covers terminating workers while Bun.serve() streams a response.

Changes

Worker termination guards

Layer / File(s) Summary
Generated sink termination guards
src/codegen/generate-jssink.ts
Generated detach, onReady, and onClose paths check for pending VM termination exceptions before creating throw scopes or invoking JS hooks.
Streaming worker termination regression test
test/js/web/workers/worker-terminate-lifetime.test.ts
Adds a debug-only test that terminates workers during streamed responses and verifies clean exit without assertion failures.

Possibly related PRs

  • oven-sh/bun#35976: Adjusts preservation and rethrowing of worker termination exceptions in VM evaluation paths.
  • oven-sh/bun#36097: Stops Bun.serve servers during worker shutdown.

Suggested reviewers: jarred-sumner

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title matches the main change: skipping JS sink callbacks during pending termination, though it says onPull instead of onReady.
Description check ✅ Passed The description covers what changed and how it was verified, with clear repro, fix, and test details.

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between 59242d6 and 9d6ea3e.

📒 Files selected for processing (2)
  • src/codegen/generate-jssink.ts
  • test/js/web/workers/worker-terminate-lifetime.test.ts

Comment thread src/codegen/generate-jssink.ts Resolved
Comment thread src/codegen/generate-jssink.ts Resolved
Comment thread src/codegen/generate-jssink.ts Resolved
@github-actions

Copy link
Copy Markdown
Contributor

Found 1 issue this PR may fix:

  1. ASAN CI: ExceptionScope::assertNoException during worker terminate (worker-transfer-terminate-stress, separate from #34095) #34690 - Same ExceptionScope::assertNoException assertion failure during worker terminate; this PR guards the JSSink onClose/onReady/onPull callbacks that re-enter JS with a pending TerminationException

If this is helpful, copy the block below into the PR description to auto-close this issue on merge.

Fixes #34690

🤖 Generated with Claude Code

@robobun

robobun commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator Author

Re #34690: that one is the worker-transfer-terminate-stress test (MessagePort transfer + terminate, no Bun.serve or sink involved), and its backtrace is unsymbolized. Same assertNoException() line, different call site. This PR only guards the sink controller's detach()/__onClose/__onReady, so I'm not claiming it fixes #34690.

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

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 in detach() runs after all native cleanup (m_sinkPtr nulling, Bun__onSinkDestroyed, controllerDetached, WriteBarrier clears) so only the JS onClose is skipped; __onClose still clears m_onClose before the guard so the call-once invariant holds.
  • Confirmed the pattern matches NodeTimerObject.cpp:80 / JSDOMExceptionHandling.cpp:149 (C++) and socket_body.rs:2039 (Rust has_exception() before entering JS on the same assertNoException failure mode).
  • Checked that JSSink::detach (Rust) is intentionally left un-guarded so signal.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} and toBe(""), CodeRabbit's await 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 awaits rd.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 the socket_body.rs:2039 precedent and is defensively correct — entering executeCallImpl with 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.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 in NodeTimerObject.cpp:80, Worker.cpp:709, JSDOMPromiseDeferred.cpp:236.
  • Verified the Rust global.has_exception() early-return matches socket/Handlers.rs:287 and WebSocketServerContext.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)).

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@robobun

robobun commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator Author

CI status: build 85025 had one non-flaky red, worker-transfer-terminate-stress.test.ts SIGABRT on x64-asan, which is the known intermittent #34690 (MessagePort transfer + terminate, unrelated to the sink path this PR touches; passes locally 3/3 on both main and this branch). After one retrigger, build 85038 shows only [flaky] failures that passed alone on retry (spawn-streaming-stdout timing, node-net-server unix socket, proxy-stress-protocol ECONNRESET, bun-install-registry hoisting on win-aarch64, and several parallel-batch timeouts). test/js/web/workers/worker-terminate-lifetime.test.ts (the new test) is green on every lane. Diff is ready for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants