Repository navigation
Conversation
worker.terminate() while a Worker-owned Bun.RedisClient has commands in
flight aborts assert-enabled builds: WebWorker::shutdown()'s socket-group
drain fires the valkey socket on_close, which builds a coded JS Error for
every in-flight/queued command via valkey_error_to_js -> Bun::createError.
With the TerminationException still pending and hasTerminationRequest()
already cleared for process.on('exit'), the first coded error in that
worker lazily initialises nodeErrorCache under a DeferTermination scope
and trips ASSERT(vm.hasTerminationRequest()) in
VMTraps::deferTerminationSlow (whole-process SIGABRT).
Guard ValkeyClient::on_close() and JSValkeyClient::on_valkey_close() on
script_execution_status(): when the VM is not Running, drop the pending
queues without building JS errors and release the socket keep-alive ref
without rejecting the connection promise or calling the onclose handler.
Same pattern as the Bun.listen/serve socket handler guards added in
#34414 / #36579.
|
Warning Review limit reached
Next review available in: 14 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (3)
Comment |
There was a problem hiding this comment.
No issues found, but this touches worker-shutdown ref-counting on the valkey client and I'd like a human to confirm the socket keep-alive ref balance and that update_poll_ref() (still run by the _defer scopeguard on the new early return in on_valkey_close) is safe on a stopped VM.
What was reviewed:
- Guard matches the established
script_execution_status() != Runningpattern fromsocket_body.rs(#36579) andipc.rs. - Stopped-VM queue drain mirrors
shutdown(None)'s finalizing branch —JSPromiseStronghandles release via Drop, no leak. on_valkey_close()still runsScopedRef::adoptbefore the early return, soconnect()'s socket keep-alive ref is released on both fail and reconnect paths.- Test is hermetic (inline RESP3 responder, no real redis), debug-only, and follows the neighboring tests' shape exactly.
Extended reasoning...
Overview
Fixes a whole-process SIGABRT when worker.terminate() lands while a Worker-owned Bun.RedisClient has commands in flight. The worker shutdown's socket-group drain reaches ValkeyClient::on_close(), which was building coded JS errors (via valkey_error_to_js → nodeErrorCache lazy init → DeferTermination) with a TerminationException pending and hasTerminationRequest() already cleared, tripping ASSERT(vm.hasTerminationRequest()) in VMTraps::deferTerminationSlow.
Two guards are added: one in ValkeyClient::on_close() (valkey.rs) that drops both queues without building JS errors when script_execution_status() != Running, then routes to on_valkey_close(); and one in JSValkeyClient::on_valkey_close() (js_valkey.rs) that early-returns after adopting the socket keep-alive ref, skipping the connection-promise reject and onclose callback. A debug-only regression test with an inline RESP3 responder is added to worker-terminate-lifetime.test.ts.
Security risks
None. This is a worker-shutdown crash fix; no user input parsing, auth, or trust-boundary changes.
Level of scrutiny
Medium-high. The change is small (~30 LOC) and follows the exact pattern already used for Bun.listen/Bun.connect socket handlers in socket_body.rs, but it sits at the intersection of worker VM termination, JSC's sticky TerminationException, and intrusive ref-counting on JSValkeyClient. The socket keep-alive ref taken by connect() must be released exactly once by either on_valkey_close() or on_valkey_reconnect(); the new stopped-VM branch routes through on_valkey_close() which adopts it before the early return, so the balance holds. The queue-drain-without-JS mirrors shutdown(None)'s finalizing branch (JSPromiseStrong releases via Drop). The one thing I did not exhaustively verify is that update_poll_ref() — which still runs via the pre-existing _defer scopeguard on the new early return — is fully safe on a stopped VM (it reads JSMap::size() and may call JsRef::upgrade()); this is pre-existing behavior since the outer SocketHandler::on_close _defer already calls it, but worth a maintainer glance.
Other factors
- PR description includes a full lldb backtrace and explicit before/after verification (aborts iteration 1 unpatched → 20/20 clean patched).
- Test alternates
autoReconnect: true/falseto exercise both the fail and reconnect branches ofon_close, is hermetic, and matches the conventions of the four neighboringterminate()-during-X regression tests in the same file. test.skipIf(!isDebug)is appropriate since release WebKit compiles the assert out, matching the sibling tests.- No prior human reviews on the PR.
|
On the two points raised: Socket keep-alive ref balance.
|
|
Closing this. The crash no longer reproduces on main, and the guard this PR adds no longer fits how teardown works. What changed on main:
Verified on a debug build with assertions (main at 4c68990, then #39543 at c5852ce), with the source change from this PR not applied:
If the assert comes back, please reopen with the new stack. |
…39543) Supersedes #39193 and keeps its three tests. The problem connect() takes one keep-alive ref on the RedisClient for its socket. The ref was released inside on_valkey_close and on_valkey_reconnect. Both are reached only through ValkeyClient::on_close(). Every branch of on_close() rejects promises before it gets there, with a ? after each call. Rejecting a promise fails while the VM's termination is pending. That is the state a terminated worker tears its sockets down in. So on_close() returned early, no releaser ran, and the client's box leaked. LSan reports it as a direct leak of 880 bytes from Box<JSValkeyClient>::new. Since #39513 the deferred close for a dial with no socket took a stand-in ref that relied on the same path. The first failed rejection also stopped the drain of the command queues. The remaining entries were never rejected. Until #39570 their promise handles and serialized bytes leaked as well, because the queues did not drop their items. What changed The socket ref is now adopted by a guard at the entry of each close event, right after the existing scoped ref: SocketHandler::on_close, SocketHandler::on_connect_error, and the half-open socket branch of ValkeyClient::close(). That branch also runs on_close() now when an exception is already pending, and returns both results. The deferred close for a dial with no socket no longer takes a stand-in ref. connect() forgets its socket ref only once it has a socket, and that task exists because it never got one, so there was nothing to give back. The two adopts inside on_valkey_close and on_valkey_reconnect are gone. reject_all_pending_commands keeps reading both queues after a rejection fails and returns the first error. on_close() itself is unchanged. Why this shape The ref belongs to the socket. It is released where the socket's close event ends, not where a callee happens to be reached. Any new ? in on_close(), fail() or reject_all_pending_commands can no longer bring the leak back. The MySQL client already does this in its on_close. #34874 and #36837 become unnecessary. Tests test/js/valkey/valkey-gc.test.ts, ASAN only. The three cases from #39193 (retry scheduled, autoReconnect off, retries exhausted) terminate a worker with commands in flight. Two more cover the offline queue: commands queued behind a non-pipelined command, and commands queued behind a dial that never completes, which is the on_connect_error entry. All five report the leaked box on main and pass with this change. Two more cover the half-open socket branch of ValkeyClient::close(), on the main thread: the client dials an IP literal whose accept queue is full, so uSockets hands connect() a real socket that never opens and delivers no close event for it. One case calls close() while the dial is pending. The other lets connectionTimeout fire during it. Each asserts the connect() rejection, the queued command's rejection, one onclose call, connected false, and a clean exit under LSan. Both report the leaked box with that branch's adopt removed and pass with it. A main-thread process.exit() variant was tried and dropped: that teardown has no termination pending, the rejections succeed, and nothing leaked before this change either. Not in this PR The queues themselves: #39570 (merged) made them VecDeque, so an early return no longer leaks the items left in them. This PR is rebased on it. The drain loop here still rejects every entry instead of stopping at the first failure. Verification Debug ASAN build on linux-x64, branch as rebased onto main at 5ebcaff (#39570): commits 99cb497, 9aaa1ec, 06ff535. The src diff is the same as before the rebase apart from the `pop_front()` and `mem::take` renames that #39570 made in `reject_all_pending_commands`. - `bun bd test test/js/valkey/valkey-gc.test.ts` with src/ at main: the five worker.terminate() cases fail, 12 pass. Each failure reports the 880 byte `Box<JSValkeyClient>` as leaked. The two close() cases pass on main as well: that branch released the ref on main too, so they pin the rewritten branch rather than show the leak. - The same file with this branch: 17 pass. - Before the rebase, on main at 4c68990: `test/js/valkey/reliability/{connection-failures,recovery,error-handling}.test.ts` 28 pass (the rest need the docker setup and skip here, CI runs them), and `test/js/web/workers/worker-terminate-lifetime.test.ts` 24 pass. Its one failure (the dns.lookup c-ares case) fails the same way with src/ at main and does not involve valkey. It is reported separately. 9aaa1ec and 06ff535 only touch the test file. <!-- robobun:evidence:begin --> --- **no test proof** · iteration 0 · Platform-specific test(s) that do not run on this machine. Deferring to CI, which covers all platforms: test/js/valkey/valkey-gc.test.ts <!-- robobun:evidence:end --> --------- Co-authored-by: robobun <117481402+robobun@users.noreply.github.com>
What
worker.terminate()while a Worker-ownedBun.RedisClienthas commands in flight aborts assert-enabled builds:5/5 whole-process SIGABRT within the first 10 iterations on release-asan main @ 074656d. Release builds compile the assert out and silently continue building JS errors / rejecting promises on a terminated VM.
Repro
No redis server required; inline RESP3 responder:
Cause
WebWorker::shutdown()clearshasTerminationRequest()(soprocess.on('exit')can run) and then drains every socket group viaRareData::close_all_socket_groups. That reaches the valkey socket'son_close, which for every in-flight / queued command callsvalkey_error_to_js->ErrorCode::fmt->Bun__createErrorWithCode->Bun::createError->globalObject->nodeErrorCache(). With the TerminationException still pending buthasTerminationRequest()now false, theLazyPropertyinit'sDeferTerminationscope tripsASSERT(vm.hasTerminationRequest())inVMTraps::deferTerminationSlow.lldb backtrace of the aborting worker thread:
Same socket-group-drain entry that #34414 / #36579 guarded for
Bun.listen/Bun.connectsocket handlers; valkey'son_closewas unguarded.Fix
Guard
ValkeyClient::on_close()onscript_execution_status(): when the VM is notRunning, drop the in-flight and offline queues without building JS errors (theirJSPromiseStronghandles release via Drop), then route throughon_valkey_close()which now also short-circuits the JS work (connection-promise reject,onclosecallback) after adoptingconnect()'s socket keep-alive ref. Mirrors theBun.listenhandler guards added in #36579.Verification
New test in
test/js/web/workers/worker-terminate-lifetime.test.tsruns the inline-responder repro (alternatingautoReconnect: false/ default so both the fail and reconnect branches ofon_closeare exercised) for 20 iterations. Debug-only (test.skipIf(!isDebug)) since release WebKit compiles the assert out.ASSERTION FAILED: vm.hasTerminationRequest().valkey-gc.test.tsand the rest ofworker-terminate-lifetime.test.tspass unchanged.no test proof · iteration 0 · 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