Repository navigation
fix(sandbox): make reset() resilient to missed bridge 'exit' events - #255
Merged
Merged
Conversation
CI on Linux intermittently fails (~50% of x86-64 runs) with "a beforeEach/afterEach hook timed out" at exactly ~5001ms in integration.test.ts, then 1-2 cascading hook timeouts and "killed N dangling processes" lines. Root cause is a Linux-only Bun bug fixed upstream in oven-sh/bun#30301 (commit ff38b2b7): the pidfd that signals a child process's exit was registered with EPOLLONESHOT, and a nested event-loop tick could drop the event from the dispatch batch before user-space saw it. The kernel already disarmed the fd, so the subprocess's 'exit' event never fires. Both socat bridges are SIGTERM'd in the same synchronous run and exit within ~1ms of each other, so their pidfd events land in the same epoll_wait batch — when one is dropped, reset()'s only escape was a setTimeout(5000) fallback. bun's default test/hook timeout is also 5000ms, and the hook starts a couple ms before reset() reaches its timer, so the hook always loses the race. That Bun fix is not in any released version yet (it landed after 1.3.13). Until it ships, harden reset() so the bug degrades to a slower reset() instead of a test failure: - Replace the 5000ms bridge-exit fallback with BRIDGE_EXIT_TIMEOUT_MS (1500ms). socat exits in <100ms; 1500ms is generous and leaves ~3.5s of margin against the hook timeout. Re-check liveness at the deadline and only escalate to SIGKILL when the process is actually still alive. - Skip registering the once('exit') listener when the process has already exited (exitCode/signalCode set) — a listener registered after the event was emitted never fires. - Force-close in-flight HTTP proxy connections via closeAllConnections() before close(), so close() does not hang on a curl killed mid-request. - Track and destroy SOCKS server connections in close() for the same reason — net.Server.close()'s callback waits for fully-open sockets. - Type createHttpProxyServer's return as http.Server (it always was; the previous net.Server type hid closeAllConnections from the type). - Factor the duplicated HTTP/SOCKS bridge teardown into killBridgeProcess.
shawnm-anthropic
approved these changes
May 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
CI on Linux intermittently fails (~50% of
linux/x86-64runs over the last few days) witha beforeEach/afterEach hook timed outat exactly ~5001ms intest/sandbox/integration.test.ts, then 1–2 cascading hook timeouts andkilled N dangling processeslines, before the rest of the run recovers. The flake never reproduces on macOS.This PR hardens
SandboxManager.reset()so that the underlying trigger — a missed bridge'exit'event — degrades to a slightly slowerreset()instead of a test failure.Root cause
The flake is triggered by a Linux-only Bun bug fixed upstream in oven-sh/bun#30301 (commit
ff38b2b7, "process: drop EPOLLONESHOT for Linux pidfd exit poll").On Linux, Bun watches a child process's exit via a
pidfdregistered inepollwithEPOLLONESHOT. The kernel disarms a one-shot fd the instantepoll_waitreturns it — before user-space dispatches it. If a poll callback inside that batch re-enters the event loop (awaitForPromise()→autoTick()path), the inner tick callsepoll_waitagain and overwrites the sharedloop->ready_pollsarray while the outer dispatch is still iterating. Remaining events are silently dropped. For a one-shot pidfd, a dropped event is unrecoverable: the fd is disarmed in the kernel with no re-arm path, so the subprocess's'exit'event never fires.reset()SIGTERMs bothsocatbridges (HTTP + SOCKS, bothstdio: 'ignore', so the pidfd is each one's only exit poll) in the same synchronous run; both exit within ~1ms of each other and their pidfd events land in the sameepoll_waitbatch. When one is dropped,Promise.all(exitPromises)resolves only when the 5000mssetTimeoutfallback fires. Bun's default test/hook timeout is also 5000ms, and the hook started a couple ms earlier — so the hook timeout always wins the race.The Bun fix is correct and complete, but it has not shipped in any release yet (it landed after 1.3.13). This PR makes
reset()robust to the bug — and to any future case where a bridge'exit'event is delayed — until a Bun release that includesff38b2b7is available to pin in CI.Reproduction & verification
Bun ships its own deterministic reproducer in
test/js/bun/spawn/pidfd-exit-nested-tick.test.ts:1.3.1(current CI version)killed 19 dangling processes1.3.9killed 17 dangling processes1.3.14-canaryd352dfd7(afterff38b2b7)Running this repository's full test suite under bun 1.3.1 reproduces the CI failure pattern locally:
taskset -c 0,1taskset -c 0,1(Single-file
bun test test/sandbox/integration.test.tsnever reproduces the original flake — the trigger needs the wider event-loop pressure of the full suite.)Changes
BRIDGE_EXIT_TIMEOUT_MS = 1500. socat exits on SIGTERM in well under 100ms; 1500ms is generous and leaves ~3.5s of margin against the hook timeout. Re-check liveness at the deadline and only escalate to SIGKILL when the process actually hasn't exited..once('exit')when the process has already exited (exitCode/signalCodeset). A listener registered after the event was emitted never fires.closeAllConnections()beforeclose().server.close()'s callback waits for every active request to drain; the proxy may be mid-upstream-dial whenreset()runs (a curl killed by--max-timeleaves one). The order matters:closeAllConnections()must run beforeclose()so it actually has connections to drop.close().net.Server.close()'s callback waits for fully-open sockets, and a SOCKS handler stuck indialDirect()(30s timeout) holds the server open.createHttpProxyServer's return value ashttp.Server(it always was; the previousnet.Serverannotation hidcloseAllConnectionsfrom the type checker).killBridgeProcess().Follow-up
bun-versiononce a release shipsff38b2b7.Failed to get proxy server addressinconfigurable-proxy-ports.test.ts) is a separate_http_server.tsrace in Bun and is not addressed here.