Repository navigation
process: drop EPOLLONESHOT for Linux pidfd exit poll #30301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fc6a2ad
process: drop EPOLLONESHOT for Linux pidfd exit poll
robobun 75e5954
[autofix.ci] apply automated fixes
autofix-ci[bot] 32c09f0
test: await the condition directly; let the test timeout be the failsafe
robobun 9bea577
test: use PATH-resolved 'true' instead of /bin/true
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // On Linux, a subprocess's pidfd is the only exit-notification poll when | ||
| // stdio is ignored. It was registered with EPOLLONESHOT, so the kernel | ||
| // disarms the fd the instant epoll_wait returns it — before user-space | ||
| // has dispatched it. If the event is then dropped in user-space before | ||
| // `onUpdate` reaches it, the fd is left permanently disarmed with no | ||
| // re-arm path, and the subprocess's `'exit'` event arrives only when the | ||
| // next unrelated timer happens to wake the loop. | ||
| // | ||
| // The drop happens when a poll callback re-enters `us_loop_run_bun_tick` | ||
| // (e.g. `expect(p).resolves` → `waitForPromise` → `autoTick`), which | ||
| // overwrites the shared `loop->ready_polls` / `num_ready_polls` / | ||
| // `current_ready_poll` while the outer dispatch is still mid-iteration. | ||
| // The outer loop resumes with the inner tick's indices and silently skips | ||
| // its own remaining events. | ||
| // | ||
| // Observed in the wild as a 5s `afterAll` timeout in | ||
| // anthropic-experimental/sandbox-runtime on GH Actions ubuntu-24.04 x86 | ||
| // runners (two socat bridges SIGTERM'd together; one's event lost). | ||
| // | ||
| // Fix: register the pidfd level-triggered (no EPOLLONESHOT). A pidfd stays | ||
| // readable from process exit until close, so a dropped ready_polls slot is | ||
| // harmless — the next epoll_wait returns it again. | ||
| import { test, expect } from "bun:test"; | ||
| import { isLinux } from "harness"; | ||
|
|
||
| // pidfd path is Linux-only; macOS/FreeBSD use EVFILT_PROC which is keyed | ||
| // on pid and auto-removed by the kernel when the process is reaped. | ||
| test.skipIf(!isLinux)( | ||
| "subprocess pidfd exit survives nested event-loop tick dropping its ready_polls slot", | ||
| async () => { | ||
| // Spawn a batch of short-lived children with stdio ignored so the pidfd | ||
| // is each one's only poll. They all exit ~together, so a single | ||
| // epoll_wait returns most of their pidfd events in one batch. | ||
| const N = 20; | ||
| const exits: Array<Promise<void>> = []; | ||
| let nested = false; | ||
|
|
||
| for (let i = 0; i < N; i++) { | ||
| const { promise, resolve } = Promise.withResolvers<void>(); | ||
| exits.push(promise); | ||
| Bun.spawn({ | ||
| cmd: ["/bin/true"], | ||
|
Check warning on line 42 in test/js/bun/spawn/pidfd-exit-nested-tick.test.ts
|
||
| stdin: "ignore", | ||
| stdout: "ignore", | ||
| stderr: "ignore", | ||
| onExit() { | ||
| // First onExit to run forces a synchronous nested tick of the | ||
| // main uws loop. Bun.sleep(1) resolves via the timer queue, which | ||
| // only drains inside autoTick() AFTER us_loop_run_bun_tick — so | ||
| // waitForPromise must enter autoTick → epoll_wait to resolve it. | ||
| // That overwrites the outer dispatch's ready_polls state; any | ||
| // sibling pidfd events queued after this one in the outer batch | ||
| // are dropped. With EPOLLONESHOT those pidfds are now disarmed in | ||
| // the kernel with no re-arm path. | ||
| if (!nested) { | ||
| nested = true; | ||
| expect(Bun.sleep(1)).resolves.toBe(undefined); | ||
| } | ||
| resolve(); | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| // Every child must report exit promptly. With the EPOLLONESHOT bug, | ||
| // the children whose events were dropped never fire onExit and this | ||
| // await hangs until the 4s fallback — well over the 1000ms threshold. | ||
| const t0 = Date.now(); | ||
| const ms = await Promise.race([ | ||
| Promise.all(exits).then(() => Date.now() - t0), | ||
| new Promise<number>(r => setTimeout(() => r(Date.now() - t0), 4000)), | ||
| ]); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| expect(ms).toBeLessThan(1000); | ||
| }, | ||
| ); | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.