Repository navigation
fix(builtins): read -t 0 answers input_avail without consuming - #3954
Merged
Merged
Conversation
…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.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #3945.
GNU contract
read -t 0is the non-destructive readiness check: builtins/read.def:395-398 returnsinput_avail(fd)(lib/sh/input_avail.c:64— a zero-timeoutselect()) 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 0arm (read_builtin.rsread_timeout_zero_status) consultedstdin_string_for_command_mut, whose contract is CONSUMPTION:{ sleep 1; printf 'x\n'; } | { read -t 0 reply; }reported ready while the producer was still sleeping (GNU: rc 1);Fileendpoint (a live procsub pipe) was slurped to EOF —exec 0< <(sleep 1; printf 'x\n'); read -t 0 xblocked 1.07 s, returned rc 0, and consumed the record;ProcessSubstitutionendpoint answered ready unconditionally.Fix
TimedInput::available_now(read-only — select(0) waits nothing,skipped_waitdoes 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.FdReadEndpoint::timed_text_stream_readygivesread_timeout_zero_pollthe same schedule verdict for Text/ProcessSubstitution endpoints; kernel-backed endpoints keep the real poll.Verification
tests/issue3945_read_t0_readiness.rs: fail-first verified — 5/6 red at the merge base, green with the fix.Oracle citations: read.def:395-398, input_avail.c:64-97, read.def:398
return (input_avail (fd) ? EXECUTION_SUCCESS : EXECUTION_FAILURE);.