-
Notifications
You must be signed in to change notification settings - Fork 5k
Bun.stdin: reject concurrent text/bytes/arrayBuffer/json reads instead of racing on fd 0 #35833
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
Open
robobun
wants to merge
6
commits into
main
Choose a base branch
from
farm/7b31cf56/stdin-single-owner
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+178
−4
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bb234cb
Bun.stdin: reject concurrent text/bytes/arrayBuffer/json reads instea…
robobun 7327daf
[autofix.ci] apply automated fixes
autofix-ci[bot] 58105e3
trim doc comments
robobun ac43612
only route through the cached stdin stream when it is locked
robobun 1a28f32
ci: retrigger
robobun 1c7e2b8
test: cover Bun.stdin.json() in the locked-reader matrix
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe } from "harness"; | ||
|
|
||
| // `Bun.stdin.text()/bytes()/arrayBuffer()/json()` read fd 0 via a `ReadFile` | ||
| // task that owns its own io poll. A second concurrent `ReadFile` on the same | ||
| // fd used to issue a second `EPOLL_CTL_ADD`, so one call rejected with the raw | ||
| // `EEXIST: file already exists, epoll_ctl` after already having consumed bytes | ||
| // (those bytes were dropped, so the other call resolved short). The helpers now | ||
| // reject `ERR_INVALID_STATE` up front for a second concurrent consumer instead, | ||
| // and route through the cached `ReadableStream` when `process.stdin` (or a | ||
| // manual `Bun.stdin.stream().getReader()`) already holds it. | ||
|
|
||
| const SIZE = 1024 * 1024; | ||
| const payload = Buffer.alloc(SIZE, "abcdefghij"); | ||
|
|
||
| async function run(src: string) { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", src], | ||
| env: bunEnv, | ||
| stdin: "pipe", | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| proc.stdin.write(payload); | ||
| await proc.stdin.end(); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| return { stdout, stderr, exitCode }; | ||
| } | ||
|
|
||
| test.concurrent("two concurrent Bun.stdin.text() calls: second rejects ERR_INVALID_STATE, first reads every byte", async () => { | ||
| const { stdout, stderr, exitCode } = await run(` | ||
| const wrap = p => p.then( | ||
| v => ({ state: "resolved", len: v.length }), | ||
| e => ({ state: "rejected", code: e?.code, name: e?.name }), | ||
| ); | ||
| const [a, b] = await Promise.all([wrap(Bun.stdin.text()), wrap(Bun.stdin.text())]); | ||
| process.stdout.write(JSON.stringify({ a, b })); | ||
| `); | ||
| expect(stderr).toBe(""); | ||
| expect(JSON.parse(stdout)).toEqual({ | ||
| a: { state: "resolved", len: SIZE }, | ||
| b: { state: "rejected", code: "ERR_INVALID_STATE", name: "Error" }, | ||
| }); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
|
|
||
| describe.each(["text", "arrayBuffer", "bytes"] as const)("Bun.stdin.%s()", method => { | ||
|
robobun marked this conversation as resolved.
Outdated
|
||
| test.concurrent(`rejects when process.stdin holds the reader; process.stdin receives every byte`, async () => { | ||
| const { stdout, stderr, exitCode } = await run(` | ||
| let n = 0; | ||
| process.stdin.on("data", c => (n += c.length)); | ||
| let blob = { state: "pending" }; | ||
| Bun.stdin.${method}().then( | ||
| v => { blob = { state: "resolved" }; }, | ||
| e => { blob = { state: "rejected", code: e?.code }; }, | ||
| ); | ||
| await new Promise(r => process.stdin.once("end", r)); | ||
| await Promise.resolve(); | ||
| process.stdout.write(JSON.stringify({ n, blob })); | ||
| `); | ||
| expect(stderr).toBe(""); | ||
| expect(JSON.parse(stdout)).toEqual({ | ||
| n: SIZE, | ||
| blob: { state: "rejected", code: "ERR_INVALID_STATE" }, | ||
| }); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
| }); | ||
|
|
||
| test.concurrent("sequential Bun.stdin.text() after the first read completes does not reject", async () => { | ||
| const { stdout, stderr, exitCode } = await run(` | ||
| const a = await Bun.stdin.text(); | ||
| const b = await Bun.stdin.text().then( | ||
| v => ({ state: "resolved", len: v.length }), | ||
| e => ({ state: "rejected", code: e?.code }), | ||
| ); | ||
| process.stdout.write(JSON.stringify({ aLen: a.length, b })); | ||
| `); | ||
| expect(stderr).toBe(""); | ||
| expect(JSON.parse(stdout)).toEqual({ | ||
| aLen: SIZE, | ||
| b: { state: "resolved", len: 0 }, | ||
| }); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
|
|
||
| test.concurrent("Bun.stdin.text() with no other consumer reads every byte", async () => { | ||
| const { stdout, stderr, exitCode } = await run( | ||
| `process.stdout.write(String((await Bun.stdin.text()).length));`, | ||
| ); | ||
| expect(stderr).toBe(""); | ||
| expect(stdout).toBe(String(SIZE)); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
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.