-
Notifications
You must be signed in to change notification settings - Fork 5k
usockets: make us_create_loop fallible instead of aborting on EMFILE #33850
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
Closed
Closed
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b611c2a
usockets: make us_create_loop fallible instead of aborting on EMFILE
robobun 2d472da
address review: early loop check before VM allocs; set copy.real in f…
robobun 237d14c
ci: retrigger
robobun f88fb24
web_worker: post exit code 1 when start_vm() fails
robobun bcf6dfa
test: assert specific error codes and wrap worker cleanup in try/finally
robobun 2b4acd6
rebase: adapt to per-crate thiserror enums (#33909)
robobun f29437a
condense new comments to the 3-line limit
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
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
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
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
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
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,86 @@ | ||
| // The first fetch() and the first new Worker() lazily create a per-thread | ||
| // uSockets event loop (epoll_create1 + timerfd_create + eventfd on Linux, | ||
| // kqueue on macOS). Under fd exhaustion those syscalls fail with EMFILE. The | ||
| // process must not abort: the one operation should fail and, once fds are | ||
| // freed, a retry should succeed. | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, isPosix } from "harness"; | ||
|
|
||
| const fixture = /* js */ ` | ||
| import * as fs from "node:fs"; | ||
| const held = []; | ||
| for (;;) { try { held.push(fs.openSync("/dev/null", "r")); } catch { break; } } | ||
| // First use of the lazily-created HTTP-client event loop: must reject, not abort. | ||
| await fetch("http://127.0.0.1:1/").then( | ||
| () => { console.error("UNEXPECTED: fetch resolved"); process.exit(1); }, | ||
| e => console.error("rejected:", e?.code ?? String(e)), | ||
| ); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| for (const fd of held) fs.closeSync(fd); | ||
| // With fds freed, the HTTP thread's loop should come up and a retry should | ||
| // reach a normal connect failure (ECONNREFUSED to 127.0.0.1:1), not abort. | ||
| await fetch("http://127.0.0.1:1/").then( | ||
| () => console.error("retry resolved"), | ||
| e => console.error("retry rejected:", e?.code ?? String(e)), | ||
| ); | ||
| console.error("SURVIVED"); | ||
| `; | ||
|
|
||
| const workerFixture = /* js */ ` | ||
| import * as fs from "node:fs"; | ||
| import * as os from "node:os"; | ||
| const W = os.tmpdir() + "/w-" + process.pid + ".mjs"; | ||
| fs.writeFileSync(W, "postMessage(42)\\n"); | ||
|
robobun marked this conversation as resolved.
|
||
| // Warm lazy builtin loads (debug builds read internal:fixed_queue from disk | ||
| // on the first nextTick, which new Worker()'s close-event dispatch triggers). | ||
| process.nextTick(() => {}); | ||
| const held = []; | ||
| try { | ||
| for (;;) { try { held.push(fs.openSync("/dev/null", "r")); } catch { break; } } | ||
| await new Promise(resolve => { | ||
| const w = new Worker(W); | ||
| w.onerror = ev => { console.error("worker error:", ev?.message ?? "error"); resolve(); }; | ||
| w.onmessage = () => { console.error("UNEXPECTED: worker message"); resolve(); }; | ||
| }); | ||
| } finally { | ||
| for (const fd of held) fs.closeSync(fd); | ||
| fs.unlinkSync(W); | ||
| } | ||
| console.error("SURVIVED"); | ||
| `; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| describe.skipIf(!isPosix)("lazy event-loop creation under EMFILE", () => { | ||
| test.concurrent("first fetch() rejects instead of aborting the process", async () => { | ||
| await using proc = Bun.spawn({ | ||
| cmd: ["/bin/sh", "-c", `ulimit -n 512 && exec "$1" --no-install -e "$2"`, "sh", bunExe(), fixture], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect({ stdout, stderr, exitCode }).toEqual({ | ||
| stdout: "", | ||
| stderr: expect.stringContaining("SURVIVED"), | ||
| exitCode: 0, | ||
| }); | ||
| expect(stderr).toContain("rejected: FailedToOpenSocket"); | ||
| // The retry must reach the normal connect path (loop recovered), not | ||
| // another FailedToOpenSocket. | ||
| expect(stderr).toContain("retry rejected: ConnectionRefused"); | ||
| }); | ||
|
|
||
| test.concurrent("first new Worker() fires an error event instead of aborting the process", async () => { | ||
| await using proc = Bun.spawn({ | ||
| cmd: ["/bin/sh", "-c", `ulimit -n 512 && exec "$1" --no-install -e "$2"`, "sh", bunExe(), workerFixture], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect({ stdout, stderr, exitCode }).toEqual({ | ||
| stdout: "", | ||
| stderr: expect.stringContaining("SURVIVED"), | ||
| exitCode: 0, | ||
| }); | ||
| expect(stderr).toContain("worker error: Worker initialization failed: EMFILE"); | ||
| }); | ||
| }); | ||
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.