Repository navigation
WIP: fix(fd): in-process serves resolve a dup source at its list position (#2314) - #2330
Merged
Merged
Conversation
GNU applies a command's redirect list as one ordered walk: do_redirections (redir.c:237) loops the list in order (redir.c:252 `for (temp = list; temp; temp = temp->next)`), so a dup copies its source descriptor as bound AT THE DUP'S OWN POSITION. Later entries that rebind the source cannot change what an earlier dup copied, and a cyclic pair (`3<&4 4<&3`) is not a loop — the second dup sees the first's result, the first saw the pre-list ambient state. The #2305 serve resolved a dup's source with a whole-list last-wins scan (position discarded) and served the terminal file/heredoc by rev-find, so on `read -u 3 a 3<&4 4<inner.txt` fd 3 read inner instead of fd 4's pre-list file, and the cyclic pair chased 3->4->3 until the fd was reported invalid. Thread positions through the serve instead: - own_input_binding_scan(cmd, fd, before) bounds the ordered walk to the entries before `before` and returns the winner's list index; own_input_binding keeps its signature as the position-blind wrapper for kind-only callers. - The dup chases (read/mapfile serve and the availability gate) start at the dup's own index and resolve each source against the entries before the current bound — positions strictly descend, seen-set keyed on (fd, bound). - List-sourced winners are served entry-exactly: read_redirected_fd_at / mapfile_redirected_fd_input_at bound the rev-find to the winning index; read_heredoc_fd_input_at / mapfile_heredoc_fd_input_at pick the heredoc entry by the ordinal correlation materialize_async_stdin_body uses (same-fd, same-stringness document entries before the winner). Entry cores extracted from the existing serves; the unpositioned serves are unchanged for the top-level fd and the ambient fallthrough. - redirect_in convention: it mirrors a list entry whose position is unrecoverable, so it is consulted only when the bounded scan yields no binding at all and reports position None; a dup sourced from it resolves its source against the full list, after which positions strictly descend. Probes (GNU bash 5.3.20, script files): headline, cyclic both directions, source rebound to file and to heredoc, heredoc source, two chained-dup shapes, close-after-dup all byte-match GNU; unrebound-dup and dup-of-closed-source controls unchanged. Pins: tests/issue2314_dup_source_position_regressions.rs (12). Controls green: issue2305 (16), issue2292 (18), issue2235 (11). Stacked on PR #2313 (the defect exists only on top of the #2305 serve); lands after it. Refs #2314
caomengxuan666
changed the base branch from
fix/2305-heredoc-correlation
to
master
October 31, 2026 05:13
caomengxuan666
marked this pull request as ready for review
October 31, 2026 08:25
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.
Refs #2314 — residual D2 of the #2305 cluster: on the in-process serve path (
read -u/mapfile -u), a dup whose source fd is rebound by a later entry of the same redirect list resolved against the source's final binding instead of its at-position binding. Cyclic dups (3<&4 4<&3) diverged the same way (reported as invalid fds).Stacking: based on PR #2313's head (master d758fd0 + the #2305 serve commit). The defect exists only on top of #2305 — bare master does not contain the serve — so this PR is stacked on #2313 and lands after it; once #2313 lands, the diff reduces to the single #2314 commit.
GNU oracle
do_redirections(redir.c:237) is one ordered walk — sequential loop at redir.c:252 (for (temp = list; temp; temp = temp->next)). Every dup copies the descriptor as bound at its own position; a cyclic pair is not a loop (second dup sees the first's result; the first saw the pre-list ambient state).Fix (mechanism, no per-shape guards)
own_input_binding_scan(cmd, fd, before)— the [bug] Heredoc binding on a nested group is shadowed by the outer group's binding for the same fd #2305 resolver's ordered walk, bounded to entries beforebefore, returning the winner's list index.own_input_bindingkeeps its signature as the position-blind wrapper (kind-only callers untouched).(fd, bound).read_redirected_fd_at/mapfile_redirected_fd_input_atbound the rev-find to the winning index;read_heredoc_fd_input_at/mapfile_heredoc_fd_input_atpick the heredoc entry by the ordinal correlationmaterialize_async_stdin_bodyuses. Entry cores extracted from the existing serves; unpositioned serves unchanged for the top-level fd and the ambient fallthrough.None; a dup sourced from it resolves its source against the full list, after which positions strictly descend.Verification
4: Bad file descriptor). Pre-fix, 10 of 12 diverged.tests/issue2314_dup_source_position_regressions.rs: 12 passed (scrubbed-fd runner).RUSTFLAGS="-C debuginfo=0"(canonical debuginfo=2 lib compiles are OOM-killed on this box); full regression + lib suites and canonical-profile verification are the collector's / CI's.