Skip to content

fix(builtins): read -t 0 answers input_avail without consuming - #3954

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/3945-read-t0-readiness
Nov 19, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/3945-read-t0-readiness

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #3945.

GNU contract

read -t 0 is the non-destructive readiness check: builtins/read.def:395-398 returns input_avail(fd) (lib/sh/input_avail.c:64 — a zero-timeout select()) immediately after option parsing — rc 0 iff the descriptor is readable, rc 1 when nothing has arrived, and it never reads a byte. It also never consumes the pending record (the closed #398's face).

Root cause

Rubash's -t 0 arm (read_builtin.rs read_timeout_zero_status) consulted stdin_string_for_command_mut, whose contract is CONSUMPTION:

  • a staged FUNCTION_STDIN mirror advanced its offset to the end, so { sleep 1; printf 'x\n'; } | { read -t 0 reply; } reported ready while the producer was still sleeping (GNU: rc 1);
  • an exec-bound fd-0 File endpoint (a live procsub pipe) was slurped to EOF — exec 0< <(sleep 1; printf 'x\n'); read -t 0 x blocked 1.07 s, returned rc 0, and consumed the record;
  • a ProcessSubstitution endpoint answered ready unconditionally.

Fix

  • TimedInput::available_now (read-only — select(0) waits nothing, skipped_wait does not advance): a staged buffer with a production schedule (rubash#1141) answers on the schedule's virtual clock — bytes the producer has not emitted yet are not available however long materialization already waited.
  • Heredoc/herestring bodies stay in-process ready (GNU sources them from temp files).
  • FdReadEndpoint::timed_text_stream_ready gives read_timeout_zero_poll the same schedule verdict for Text/ProcessSubstitution endpoints; kernel-backed endpoints keep the real poll.

Verification

  • Pins tests/issue3945_read_t0_readiness.rs: fail-first verified — 5/6 red at the merge base, green with the fix.
  • Issue seed R138-B: 12/12 under WSL GNU bash 5.3.20 (script files) against the Linux rubash build.
  • Regression families (read-timeout, procsub, TMOUT compound, coproc, cli_tests read_timeout slice) green on the rebased tree (base carries 16 known-red cli_tests, unchanged).

Oracle citations: read.def:395-398, input_avail.c:64-97, read.def:398 return (input_avail (fd) ? EXECUTION_SUCCESS : EXECUTION_FAILURE);.

…3945)

GNU builtins/read.def:395-398 returns input_avail(fd) (lib/sh/
input_avail.c: a zero-timeout select) the moment -t 0 is parsed:
rc 0 iff the descriptor is readable, rc 1 when nothing has arrived,
and it never reads a byte. Rubash's -t 0 arm consulted
stdin_string_for_command_mut instead, whose contract is CONSUMPTION:
a staged FUNCTION_STDIN mirror advanced its offset to end and an
exec-bound fd-0 File endpoint was slurped to EOF, so read -t 0
blocked for the producer's whole runtime, reported ready, and ate
the pending record (the closed issue #398's face).

The fd-0 arm now judges only the in-process carriers without
consuming: a staged FUNCTION_STDIN buffer with a production
schedule (rubash#1141) answers on the schedule's virtual clock via
TimedInput::available_now (read-only - select(0) waits nothing), an
unscheduled staged buffer keeps its temp-file semantics, and
heredoc/herestring bodies stay in-process ready. FdReadEndpoint::
timed_text_stream_ready gives read_timeout_zero_poll the same
verdict for Text/ProcessSubstitution endpoints; kernel-backed
endpoints keep the real wait_readable poll.

Pins: tests/issue3945_read_t0_readiness.rs (fail-first verified:
5/6 red at 2d5b56f, green with the fix). Seed R138-B 12/12 under
WSL GNU bash 5.3.20 vs the Linux rubash build; oracle read.def
citations read.def:395-398, input_avail.c:64.
@caomengxuan666
caomengxuan666 merged commit 4fda0a5 into master Nov 19, 2026
9 of 12 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 19, 2026
…IR (#3960)

Follow-up to #3955/#3954 (the same hazard the #3958 branch scoped for its own pins): parallel census stems sharing the repo cwd collided on the pins' relative file operands. Expectations unchanged.
@caomengxuan666
caomengxuan666 deleted the fix/3945-read-t0-readiness branch November 19, 2026 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] read -t 0 returns success when no input is available read -t 0 consumes the pending input (GNU: non-destructive readiness check)

1 participant