Skip to content

WIP: fix fd seams #2589 #2590 #2591 - #2592

Merged
caomengxuan666 merged 5 commits into
masterfrom
fix/2589-fd-seams
Nov 2, 2026
Merged

caomengxuan666 merged 5 commits into
masterfrom
fix/2589-fd-seams

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

WIP — code-only delivery under the 2026-11-02 pace directive. Captain verifies centrally; do not merge on local results.

Refs #2589
Refs #2590
Refs #2591

Commits

GNU basis: redir.c:601-603 gates the RF_DEVFD dup emulation to builds without HAVE_DEV_FD; redir.c:725 is the plain-open path; redir.c:1193 is the dup2 path for <&N.

Captain verification required (UNVERIFIED-BY-CAPTAIN)

New pins:

  • issue2589_dev_fd_operand_origin_reopen — 4 tests, including r1c/r1d negative controls
  • issue2590_spawned_stage_chain_fold — 5 tests, including drainer negative control
  • issue2591_compound_stage_spelled_winner — 6 tests, including dup-winner and internal-spelling negative controls

Guards:

  • issue2502_pipeline_alias_delivery_regressions
  • issue2503_born_closed_stdin_regressions
  • issue2029_fd_offset_fold_regressions
  • all issue2522_* stems present at harvest
  • issue2322_close_position_regressions
  • issue2368_foreign_spawn_post_wait_trunc_regressions
  • issue2483_stream_drain

Local compile gate only: cargo check --all-targets exited 0 before the test-only master rebase. No slot/scrubbed test run was performed by W16.

A spelled /dev/fd/N operand is a fresh open on HAVE_DEV_FD builds: bash redir.c:601-603 only enables the RF_DEVFD dup emulation when HAVE_DEV_FD is absent, and redir.c:725 redir_open falls through to open(). When a pipeline stage leaves a materialized Text/ProcessSubstitution copy that still names a regular-file origin, serve the origin bytes and do not consume the stage copy. Apply the same rule to the Unix materialize path and the Windows table-operand path.

Refs #2589
GNU treats a numbered dup chain as one shared open file description: bash redir.c:1193 uses dup2 for <&N, so a spawned stage advances the source only by bytes it actually reads. Rubash drained a buffered source endpoint while materializing an own input binding and never replaced that bind-time drain with the child's measured consumption.

Keep a duplicate handle to the carrier file, record the endpoint cursor before the stream serve, and settle base + carrier position after the child exits. Settle from both the plain external dispatcher and the external pipeline-stage funnel; command-substitution forks start with no in-flight folds.

Refs #2590
GNU distinguishes a spelled /dev/fd/N input from a <&N dup inside a pipeline element: redir.c:725 redir_open performs a fresh open for the spelling on HAVE_DEV_FD builds, while redir.c:1193 dup2 shares the description for a dup. The compound-stage funnel used dup_input for both winner kinds, so brace groups and subshells continued at the donor offset.

Classify the winning fd-0 redirect with the same search order as stage_fd_alias_source. For a spelled, non-{var} Input/ReadWrite winner, try Executor::bind_fd0_fresh_alias first and retain dup_input as the fallback for endpoints that do not reopen fresh; dup winners keep the shared-description path.

Refs #2591
…eline loop

Repair of af74776 (#2589), whose r1b pin the captain's central
verification caught at 3/4: `cat /dev/fd/6 | wc -c` after an earlier
stage partially consumed fd 6 still served the stage copy's remainder
(6) instead of the origin (12).

Root cause: on Unix, a redirect-free pipeline of external commands
takes the concurrent spawn loop
(execute_external_pipeline_concurrently), and that loop never called
materialize_dev_fd_operands — its Windows twin
(spawn_wait_concurrent_external_stages) and both sequential paths
(pipeline_stages.rs, external_inner.rs) all do. Stages resolved a
spelled operand through the ambient fd pins alone, which carry
snapshot/remainder semantics. GNU's r_input is a plain open of the
path (redir.c:725) — on a HAVE_DEV_FD build the kernel resolves the
magic link fresh for a regular-file endpoint, so the operand serves
the origin from offset 0 and the donor does not move.

Port the pass into the Unix loop per stage, mirroring the Windows
twin: materialize before building the stage's process, attach the
operand deliveries after the ambient pins, and finish the records
only after every member has been waited (an earlier finish would
drop a donor entry a later stage's own operand pass still reads).
The std descriptors are carved out of the records
(DevOperandMaterialization::exclude_std_fd_records): fd 0/1 in this
loop are the OS plumbing itself (upstream pipe, capture wiring) —
real descriptors the child resolves natively — and the shell's
fd-table entry 0 is the enclosing context's binding, not the stage's
to consume.

Verification (slot + scrubbed cargo): issue2589 stem 4/4,
issue2590 stem 5/5, guards 2502 6/6, 2503 4/4, 2029 10/10,
2322 13/13, 2368 6/6, 2483 6/6; adjacent 2256 25/25, 2276 7/7,
1873 17/17, 2106 8/8.

Refs #2589
…rect application seam

Repair of 5b58cdd (#2591), whose three positive pins the captain's
central verification caught at 3/6: `{ cat; } < /dev/fd/6 | wc -c` and
the subshell twin served the donor's remainder (6) instead of a fresh
open (12).

Root cause: GNU applies a compound element's redirections exactly
once (do_redirections in the forked element). rubash applies them
twice — the stage funnel's preliminary fd-0 bind, then the body's own
application inside the subshell (with_command_input_redirects →
bind_command_fd0_input) — and the second application wins for the
mirror readers that serve the body. That inner serve,
loop_redirect_input, modeled a SPELLED `< /dev/fd/N` as a dup: it
handed out the endpoint's remaining bytes and drained the donor.
The comment on that branch even stated the wrong invariant ("a dup
of fd N, not a filesystem path"). GNU's spelled form is r_input →
redir_open (redir.c:725), a plain open the kernel resolves fresh
for regular files; only the `<&N` spelling is the dup
(r_duplicating_input → dup2, redir.c:1193). The funnel-side fresh
bind 5b58cdd added was therefore dead on arrival — the inner
application displaced it on every path (the same double application
also broke plain, non-pipeline `{ cat; } < /dev/fd/6`,
`( cat ) < /dev/fd/6` and `while read; done < /dev/fd/6`).

Fix at the seam that actually serves: loop_redirect_input's alias
branch now applies the fd_alias_reopen_path rule first (the
rubash#2256 rule, same as dev_fd_operand_bytes) — an endpoint that
re-opens fresh is served from its origin file and the donor is not
drained; endpoints that cannot re-open (pipes, staged text) keep the
shared serve. The superseded funnel fresh-bind and its
stage_fd_alias_winner_is_spelled classifier are removed: one owner
for the open-vs-dup distinction, at the application that wins.

Verification (slot + scrubbed cargo): issue2591 stem 6/6,
issue2589 stem 4/4, issue2590 stem 5/5, guards 2502 6/6, 2503 4/4,
2029 10/10, 2322 13/13, 2368 6/6, 2483 6/6; adjacent 2256 25/25,
2276 7/7, 1873 17/17, 2106 8/8. Byte-exact probes vs GNU bash 5.3.20:
r5c/r5d/full-r5 shapes, dup-winner negative control (shared serve,
donor drained), plain group/subshell/loop spelled serves — all match.

Refs #2591
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.

1 participant