Repository navigation
WIP: fix(fd): mirror governance over File entries (#2170); child /proc fd view must not consume parent FileFd (#2171) - #2180
Merged
Conversation
…ntry (rubash#2170)
After `exec 0<file`, a compound command's mirror rebinding
(`{ ...; } < /dev/null`, a heredoc body, a here-string) was broken
at the transport layer: bind_command_fd0_input's mirror branch
staged the FUNCTION_STDIN text but left the fd-0 fd-table entry in
place, so the File entry kept governing every consumer in the body
— the builtin read's fd-table arm read the old file, a spawned
child inherited the file, and the fd-alias facts stated the file —
while the staged channel claimed the mirror's source. The mirror
only ever governed over the ambient InheritedProcessStdin entry.
GNU installs a command's own redirection on fd 0 before it runs
(execute_cmd.c dispatch + redir.c:767-955
do_redirection_internal), superseding the enclosing scope's
binding, and restores it at scope end. Every descriptor branch of
the bind already replaces the fd-table entry and lets
CommandFd0InputGuard restore it; the mirror branch now does the
same: it removes the fd-0 entry for the scope's duration (the
guard's snapshot restores it verbatim). With the slot displaced,
reads take the FUNCTION_STDIN arm, children take the staged
payload, and the #2131 fact pass states the mirror's own fact —
its supersede check already treated an absent slot as
mirror-governed, so the fact layer needs no change. The
flat-subshell region feed (ast_exec) stages the same channel for a
region's readers and gets the same displacement; the region's
cloned fd table is restored wholesale at the boundary.
The rule is about entry kinds in mirror scope generally — no
/dev/null or fd-0 special cases beyond the mirror's own scope.
Pins: tests/issue2170_mirror_governance_over_file_regressions.rs
(8 tests; 6 red on master, 2 guard pins green both ways; GNU bash
5.3.20 script-file probes p2170_a..k byte-match after the fix).
Refs #2170
…ndle (rubash#2171) After `exec 0<dir`, a child that merely inspected the descriptor (`ls -l /proc/self/fd/0`) invalidated the parent's fd-0 FileFd: the next builtin read reported `Bad file descriptor` where GNU reports the descriptor's own `Is a directory` (read.def:911). Same death for any File endpoint on any fd (an exec-bound regular file on fd 3 died identically). Root cause, in the unix branch of materialize_dev_fd_operands: the attach record (DevOperandMaterialization) owns what it records — its Drop closes every recorded handle once the child exits, like GNU closing the parent's pipe end once the forked child owns its copy. Two of its three arms record fresh, owned handles (a capture-pipe read end, a just-opened procsub file). The File arm recorded the endpoint's OWN handle — the parent's live descriptor — so the record's drop closed the parent's FileFd under it; the fd-table entry survived but pointed at a dead handle. Fix: the File arm attaches a duplicate (crate::fd::duplicate_handle). A dup shares the open file description, offset included, so the child's /proc view and the #1952 shared-offset read face are unchanged (verified: a child reading through the operand behaves byte-identically to GNU before and after); the record now closes only its dup, and the parent's descriptor survives any number of inspecting children. Platform shape: the defect and the fix are unix-branch only. The Windows materialization never hands a child the parent handle — it rewrites the operand to the endpoint's path or a temp file, and its deferred consume step seeks (consume_all_text) rather than closes. The read-side EISDIR shape is #2101/#2167's and is not touched. Collision note: this is the cluster's only hunk in src/executor/dev_fd_operands.rs, which open PR #2156 also edits (its hunks are the drain/finish regions; this one is the unix materialize loop's File arm). The borrowed-handle invariant lives only in this file, so the hunk is minimal and flagged in the PR body for merge sequencing. Pins: tests/issue2171_proc_fd_operand_survival_regressions.rs (4 tests; 3 red on master, 1 guard green both ways). Refs #2171
caomengxuan666
marked this pull request as ready for review
October 30, 2026 07:38
This was referenced Oct 30, 2026
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.
Two fd-transport fixes discovered by the fd-stdin cluster (PR #2167), one commit each. Draft for CI adjudication — the main session closes the issues after verification.
Refs #2170
Refs #2171
#2170 — mirror governance over File entries
After
exec 0<file, a compound's text-mirror rebinding ({ ...; } < /dev/null, heredoc, here-string) was shadowed at the transport layer:bind_command_fd0_input's mirror branch staged FUNCTION_STDIN but left the fd-0 fd-table entry in place, so the File entry kept governing the body's readers (builtin read read the old file, children inherited it, facts stated it) while the staged channel claimed the mirror's source. GNU installs the command's own redirection on fd 0 (execute_cmd.c + redir.c:767-955do_redirection_internal), superseding the enclosing binding for the scope.Fix, in the mirror-governance domain (no fact-layer patch, no
/dev/null/fd-0 special cases): the mirror branch now displaces the fd-0 entry for the scope's duration — every descriptor branch of the bind already replaces the entry and letsCommandFd0InputGuardrestore it verbatim; the #2131 fact pass already treated an absent slot as mirror-governed, so facts follow with no fact-layer change. The flat-subshell region feed stages the same channel and gets the same displacement (its cloned fd table is restored wholesale at the boundary).Pins:
tests/issue2170_mirror_governance_over_file_regressions.rs— 8 tests, 6 red on master → green; GNU 5.3.20 script-file probes (11 shapes: /dev/null, heredoc + facts, external child, restore, subshell region, loop, stream-file guard, inherited guard, pipeline guard, function guard) byte-match after the fix.#2171 — child /proc/self/fd view must not consume the parent's FileFd
After
exec 0<dir, a child inspecting/proc/self/fd/0(ls -l) killed the parent's fd-0 FileFd: laterreadreported EBADF where GNU reports EISDIR. Root cause (unix branch of the operand materialization): the File arm recorded the parent endpoint's OWN handle inunix_fd_attach, whose record Drop closes every recorded handle after the child exits — a contract that assumes the record owns what it attaches (the other two arms record fresh, owned handles). Fix: the File arm attaches a duplicate (crate::fd::duplicate_handle— dup shares the open file description and its offset, so the child's view is unchanged) and the record closes only its dup.Collision note for the main session: this is a one-hunk change inside
src/executor/dev_fd_operands.rs, which open PR #2156 also touches (#2156's hunks are the drain/finish regions; this hunk is the unix materialize loop's File arm). The borrowed-handle invariant lives only in this file — no mechanism-true fix exists outside it — so the hunk is kept minimal and flagged here for merge sequencing.Pins:
tests/issue2171_proc_fd_operand_survival_regressions.rs— 4 tests, 3 red on master → green (directory EISDIR survival, numbered-fd survival, repeated views), 1 guard green both ways. The Windows materialization never hands the child the parent handle (path/temp based; its consume step seeks, never closes), so the defect and fix are unix-branch only; the read-side EISDIR shape itself is #2101/#2167's and is not touched.