Skip to content

fix(sandbox): make reset() resilient to missed bridge 'exit' events - #255

Merged
dylan-conway merged 1 commit into
mainfrom
fix/reset-bridge-exit-timeout
May 7, 2026
Merged

dylan-conway merged 1 commit into
mainfrom
fix/reset-bridge-exit-timeout

Conversation

@dylan-conway

Copy link
Copy Markdown
Collaborator

Summary

CI on Linux intermittently fails (~50% of linux/x86-64 runs over the last few days) with a beforeEach/afterEach hook timed out at exactly ~5001ms in test/sandbox/integration.test.ts, then 1–2 cascading hook timeouts and killed N dangling processes lines, 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 slower reset() 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 pidfd registered in epoll with EPOLLONESHOT. The kernel disarms a one-shot fd the instant epoll_wait returns it — before user-space dispatches it. If a poll callback inside that batch re-enters the event loop (a waitForPromise() → autoTick() path), the inner tick calls epoll_wait again and overwrites the shared loop->ready_polls array 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 both socat bridges (HTTP + SOCKS, both stdio: '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 same epoll_wait batch. When one is dropped, Promise.all(exitPromises) resolves only when the 5000ms setTimeout fallback 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 includes ff38b2b7 is available to pin in CI.

Reproduction & verification

Bun ships its own deterministic reproducer in test/js/bun/spawn/pidfd-exit-nested-tick.test.ts:

bun result
1.3.1 (current CI version) fail at 5000ms, killed 19 dangling processes
1.3.9 fail at 5000ms, killed 17 dangling processes
1.3.14-canary d352dfd7 (after ff38b2b7) pass in 20ms

Running this repository's full test suite under bun 1.3.1 reproduces the CI failure pattern locally:

killed 1 dangling process
(fail) Sandbox Integration Tests > (unnamed) [5002.77ms]
  ^ a beforeEach/afterEach hook timed out for this test.
(fail) Empty allowedDomains Network Blocking Integration > Network blocked with empty allowedDomains > (unnamed) [5002.93ms]
killed 2 dangling processes
(fail) Empty allowedDomains Network Blocking Integration > Network allowed with specific domains > (unnamed) [5000.33ms]
config hook-timeout flake
bun 1.3.1, no patch 5/6 runs under heavy CPU load, ~1/3 under taskset -c 0,1
bun 1.3.1, with this patch, taskset -c 0,1 0/14 runs

(Single-file bun test test/sandbox/integration.test.ts never reproduces the original flake — the trigger needs the wider event-loop pressure of the full suite.)

Changes

  • Replace the 5000ms bridge-exit fallback with 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.
  • Skip registering .once('exit') 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(). server.close()'s callback waits for every active request to drain; the proxy may be mid-upstream-dial when reset() runs (a curl killed by --max-time leaves one). The order matters: closeAllConnections() must run before close() so it actually has connections to drop.
  • Track + destroy SOCKS server connections in close(). net.Server.close()'s callback waits for fully-open sockets, and a SOCKS handler stuck in dialDirect() (30s timeout) holds the server open.
  • Type createHttpProxyServer's return value as http.Server (it always was; the previous net.Server annotation hid closeAllConnections from the type checker).
  • Factor the duplicated HTTP/SOCKS bridge teardown into killBridgeProcess().

Follow-up

  • Bump the CI bun-version once a release ships ff38b2b7.
  • The much rarer linux/arm64 flake (Failed to get proxy server address in configurable-proxy-ports.test.ts) is a separate _http_server.ts race in Bun and is not addressed here.

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.
@dylan-conway
dylan-conway merged commit f22f629 into main May 7, 2026
23 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants