-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix(stdin): retry transient nonblocking reads #713
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
ALV0612
wants to merge
4
commits into
openai:main
Choose a base branch
from
ALV0612:fix/issue-247-retry-nonblocking-stdin
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.
+142
−3
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,77 @@ | ||
| import test from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
|
|
||
| import { readStdinIfPiped } from "../plugins/codex/scripts/lib/fs.mjs"; | ||
|
|
||
| function scriptedRead(steps, events = []) { | ||
| return (fd, buffer, offset, length, position) => { | ||
| events.push(["read", fd, offset, length, position]); | ||
| const step = steps.shift(); | ||
| if (step instanceof Error) throw step; | ||
| if (step == null) return 0; | ||
| const bytes = Buffer.from(step); | ||
| bytes.copy(buffer, offset); | ||
| return bytes.length; | ||
| }; | ||
| } | ||
|
|
||
| function transient(code = "EAGAIN") { | ||
| return Object.assign(new Error("resource temporarily unavailable"), { code }); | ||
| } | ||
|
|
||
| test("readStdinIfPiped preserves bytes consumed before a transient read failure", () => { | ||
| const events = []; | ||
| const input = readStdinIfPiped({ | ||
| stdin: { isTTY: false, _handle: { setBlocking: (value) => events.push(["blocking", value]) } }, | ||
| readSync: scriptedRead(["first-", transient(), "second\n", null], events), | ||
| waitForRetry: (delay) => events.push(["wait", delay]), | ||
| chunkSize: 32 | ||
| }); | ||
| assert.equal(input, "first-second\n"); | ||
| assert.deepEqual(events[0], ["blocking", true]); | ||
| assert.deepEqual(events.filter((event) => event[0] === "wait"), [["wait", 10]]); | ||
| }); | ||
|
|
||
| test("readStdinIfPiped treats EWOULDBLOCK as transient and bounds backoff", () => { | ||
| const error = transient("EWOULDBLOCK"); | ||
| const waits = []; | ||
| assert.throws(() => readStdinIfPiped({ | ||
| stdin: { isTTY: false }, | ||
| readSync() { throw error; }, | ||
| waitForRetry: (delay) => waits.push(delay), | ||
| maxAttempts: 5, | ||
| initialRetryDelayMs: 10, | ||
| maxRetryDelayMs: 25 | ||
| }), (actual) => actual === error); | ||
| assert.deepEqual(waits, [10, 20, 25, 25]); | ||
| }); | ||
|
|
||
| test("readStdinIfPiped surfaces non-transient read errors without retrying", () => { | ||
| const error = Object.assign(new Error("bad descriptor"), { code: "EBADF" }); | ||
| let waits = 0; | ||
| assert.throws(() => readStdinIfPiped({ | ||
| stdin: { isTTY: false }, | ||
| readSync() { throw error; }, | ||
| waitForRetry() { waits += 1; } | ||
| }), (actual) => actual === error); | ||
| assert.equal(waits, 0); | ||
| }); | ||
|
|
||
| test("readStdinIfPiped ignores unsupported blocking mode and reads all chunks", () => { | ||
| const input = readStdinIfPiped({ | ||
| stdin: { isTTY: false, _handle: { setBlocking() { throw new Error("unsupported"); } } }, | ||
| readSync: scriptedRead(["ordinary ", "pipe", null]), | ||
| waitForRetry() { throw new Error("unexpected retry"); } | ||
| }); | ||
| assert.equal(input, "ordinary pipe"); | ||
| }); | ||
|
|
||
| test("readStdinIfPiped returns empty input for a TTY without touching fd 0", () => { | ||
| let reads = 0; | ||
| const input = readStdinIfPiped({ | ||
| stdin: { isTTY: true }, | ||
| readSync() { reads += 1; return 0; } | ||
| }); | ||
| assert.equal(input, ""); | ||
| assert.equal(reads, 0); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
setBlocking(true)is unavailable and a producer leaves its pipe open without supplying the next chunk for about 1.13 seconds, the eighthEAGAINis thrown even though it is neither EOF nor an invalid descriptor. For example, a delayed or intermittently streaming piped prompt will fail instead of being read in full; the normal blocking-read behavior has no equivalent timeout. Continue waiting until data or EOF arrives (or expose an explicit caller timeout) rather than treating a fixed retry count as a read failure.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in af3e94b. Transient EAGAIN/EWOULDBLOCK no longer has an attempt ceiling while the pipe remains open; the loop waits with capped backoff until data or EOF, and still throws non-transient errors immediately. Added a 20-consecutive-transient regression before eventual progress.