-
-
Notifications
You must be signed in to change notification settings - Fork 140
fix(fs): openSync dropped O_NONBLOCK, and readSync reported errors as EOF #6403
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
proggeramlug
merged 4 commits into
PerryTS:main
from
proggeramlug:fix/fs-open-flags-read-errors
Jul 14, 2026
+172
−8
Merged
Changes from all 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
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,49 @@ | ||
| // `openSync` must pass the flags it does not model through to open(2), and a | ||
| // failing `readSync` must surface the syscall error instead of reporting EOF. | ||
| // | ||
| // Perry translated only O_ACCMODE/O_CREAT/O_EXCL/O_TRUNC/O_APPEND into Rust's | ||
| // OpenOptions and dropped the rest, so O_NONBLOCK never reached open(2): the | ||
| // descriptor stayed blocking and a `readSync` with no data available hung | ||
| // forever where Node throws EAGAIN. `readSync` also mapped every error to 0, | ||
| // making a failure indistinguishable from a clean end-of-file. | ||
|
|
||
| import { openSync, readSync, closeSync, constants } from "fs"; | ||
|
|
||
| // A FIFO has no writer, so a non-blocking read has nothing to deliver: Node | ||
| // answers EAGAIN. (A blocking descriptor would park here forever.) | ||
| import { mkdtempSync, rmSync } from "fs"; | ||
| import { execFileSync } from "child_process"; | ||
| import { tmpdir } from "os"; | ||
| import { join } from "path"; | ||
|
|
||
| const dir = mkdtempSync(join(tmpdir(), "perry-nonblock-")); | ||
|
|
||
| // The fifo and the descriptor must be released even when an assertion below | ||
| // throws, or a failing run leaves a temp directory (and an open fd) behind on | ||
| // every retry. | ||
| try { | ||
| const fifo = join(dir, "fifo"); | ||
| execFileSync("mkfifo", [fifo]); | ||
|
|
||
| const fd = openSync(fifo, constants.O_RDONLY | constants.O_NONBLOCK); | ||
| try { | ||
| const buf = Buffer.alloc(16); | ||
| let code = null; | ||
| try { | ||
| readSync(fd, buf, 0, 16, null); | ||
| throw new Error("readSync resolved instead of raising EAGAIN"); | ||
| } catch (err) { | ||
| code = err.code; | ||
| } | ||
|
|
||
| if (code !== "EAGAIN") { | ||
| throw new Error(`readSync on an empty non-blocking FIFO reported ${code}, expected EAGAIN`); | ||
| } | ||
| } finally { | ||
| closeSync(fd); | ||
| } | ||
| } finally { | ||
| rmSync(dir, { recursive: true, force: true }); | ||
| } | ||
|
|
||
| console.log("nonblocking-read ok"); | ||
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,62 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" | ||
| PERRY="${PERRY_BIN:-${PERRY:-$ROOT/target/release/perry}}" | ||
| RUNTIME_DIR="${PERRY_RUNTIME_DIR:-$ROOT/target/release}" | ||
| FIXTURE="$ROOT/tests/issue_fs_nonblocking_read.js" | ||
| WORKDIR="${TMPDIR:-/tmp}/perry-fs-nonblocking-read-$$" | ||
| BIN="$WORKDIR/perry-fs-nonblocking-read" | ||
| COMPILE_LOG="$WORKDIR/compile.log" | ||
| STDOUT_LOG="$WORKDIR/stdout.log" | ||
| STDERR_LOG="$WORKDIR/stderr.log" | ||
|
|
||
| mkdir -p "$WORKDIR" | ||
| trap 'rm -rf "$WORKDIR"' EXIT | ||
|
|
||
| if [[ ! -x "$PERRY" ]]; then | ||
| PERRY="$ROOT/target/debug/perry" | ||
| fi | ||
| if [[ ! -x "$PERRY" ]]; then | ||
| echo "SKIP: perry binary not found (build with cargo build --release -p perry)" | ||
| exit 0 | ||
| fi | ||
| if ! command -v mkfifo >/dev/null 2>&1; then | ||
| echo "SKIP: mkfifo not available" | ||
| exit 0 | ||
| fi | ||
|
|
||
| env PERRY_ALLOW_UNIMPLEMENTED=1 PERRY_RUNTIME_DIR="$RUNTIME_DIR" "$PERRY" compile --no-cache --no-auto-optimize "$FIXTURE" -o "$BIN" \ | ||
| >"$COMPILE_LOG" 2>&1 || { | ||
| cat "$COMPILE_LOG" >&2 | ||
| exit 1 | ||
| } | ||
|
|
||
| # A blocking descriptor parks in readSync forever — cap the run so the | ||
| # regression shows up as a failure rather than a hung suite. | ||
| set +e | ||
| if command -v timeout >/dev/null 2>&1; then | ||
| timeout 20 "$BIN" >"$STDOUT_LOG" 2>"$STDERR_LOG" </dev/null | ||
| elif command -v gtimeout >/dev/null 2>&1; then | ||
| gtimeout 20 "$BIN" >"$STDOUT_LOG" 2>"$STDERR_LOG" </dev/null | ||
| else | ||
| "$BIN" >"$STDOUT_LOG" 2>"$STDERR_LOG" </dev/null | ||
| fi | ||
| run_rc=$? | ||
| set -e | ||
|
|
||
| if [[ "$run_rc" -eq 124 ]]; then | ||
| echo "readSync on a non-blocking fd hung (O_NONBLOCK was dropped on open)" >&2 | ||
| exit 1 | ||
| fi | ||
| if [[ "$run_rc" -ne 0 ]]; then | ||
| echo "Perry non-blocking read fixture failed with exit code $run_rc" >&2 | ||
| cat "$STDOUT_LOG" "$STDERR_LOG" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| grep -qx "nonblocking-read ok" "$STDOUT_LOG" || { | ||
| echo "unexpected output:" >&2 | ||
| cat "$STDOUT_LOG" >&2 | ||
| exit 1 | ||
| } |
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.
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Preserve the intended test failure message.
Currently, if
readSyncsuccessfully resolves instead of throwing, the script intentionally throwsnew Error("readSync resolved..."). However, thecatchblock immediately intercepts this error. Because standard error objects don't have a.codeproperty,codeis assignedundefined. The script then throws the generic second error (readSync on an empty non-blocking FIFO reported undefined, expected EAGAIN), and the original, more descriptive failure message is completely lost.To avoid catching the test's own failure error, move the explicit throw outside the
catchblock.💚 Proposed fix to properly report test failures
let code = null; try { readSync(fd, buf, 0, 16, null); - throw new Error("readSync resolved instead of raising EAGAIN"); } catch (err) { code = err.code; } + if (code === null) { + throw new Error("readSync resolved instead of raising EAGAIN"); + } if (code !== "EAGAIN") { throw new Error(`readSync on an empty non-blocking FIFO reported ${code}, expected EAGAIN`); }📝 Committable suggestion
🤖 Prompt for AI Agents