Skip to content

WIP: complementary pipe end for write-mode /proc/self/fd/N opens (appendable endpoint kind) - #2470

Merged
caomengxuan666 merged 7 commits into
masterfrom
design/2385-appendable-endpoint
Nov 1, 2026
Merged

caomengxuan666 merged 7 commits into
masterfrom
design/2385-appendable-endpoint

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #2385

WIP — design + engine implementation for the complementary pipe end. Design record (endpoint kind, producers/consumers, lifetime/close semantics, per-kind complementary-open table, external-stage story) lives in the cluster workdir REPORT.md and in the commit messages.

What

Opening a pipe read end's /proc/self/fd/N (or /dev/fd/N) in WRITE mode must materialize the pipe's other end. GNU reaches it through redir.c:725 redir_open's plain open() of the magic link (called from redir.c:237 do_redirections); on a HAVE_DEV_FD build the spelling is not in the special table (redir.c:602), so the descriptor's stream is what the open attaches to. builtins/common.c:320 sh_chkwrite is the adjacent asymmetry that stays: a DUP of a read end (>&3) still fails EBADF.

Mechanism (endpoint kinds, no path-name cases)

  • New endpoint kind FdWriteEndpoint::PipeChannel(PipeChannelEnd) — a handle onto the SAME TextInput queue the read endpoint serves; writes append at the tail, the read cursor never moves. Walk twin OutputTarget::PipeChannel.
  • Kernel pipes pair through the fd layer: fd::complementary_write_file opens /proc/self/fd/{handle} O_WRONLY on unix and wraps as the existing File endpoint; the Windows twin declines (no complementary-open API for pipe handles there) — documented platform boundary. Materialized channels pair on all platforms.
  • One resolver, Executor::fd_alias_complementary_write_endpoint (+ pure predicate twin), consulted only where a write-mode spelling open is resolved and no write endpoint exists. Regular files keep the fresh-open arm; socket ends keep the ENXIO arm; /dev/null-class ends keep theirs — complementary resolution is pipe-keyed throughout.
  • Three diversions found and fixed en route: the ordered walk's Closed seed let the dup shortcut serve a spelling target; the exec walk resolved spellings through the dup helper and copied read ends for >/<>; the external target chains had no branch for a spelled complementary target, so a child's bytes leaked to the shell's real stdout with status 0.

Stop-gate

Not triggered: every touched type is pub(crate), docs/api-ledger.md contains no fd-model symbols, no niubash-side change is needed.

Verification (scrubbed-fd harness, fds >2 closed)

…ash#2385)

A write-mode open of a pipe read end's fd alias spelling must
materialize the pipe's other end. GNU reaches it through redir.c
redir_open's plain open() of the magic link (redir.c:725, called
from do_redirections, redir.c:237): on a HAVE_DEV_FD build the
/dev/fd entry is in the special table only when the macro is
absent (redir.c:602), so the kernel attaches the open to the
descriptor's stream. A materialized stream has no kernel pipe,
so the fd model gains the appendable byte-channel endpoint kind:
FdWriteEndpoint::PipeChannel(PipeChannelEnd) — a handle onto the
SAME TextInput queue the read endpoint serves; writes append at
the tail, the read cursor never moves (GNU's pipe write has no
offset). For kernel pipes the fd layer gains the real operation:
fd::complementary_write_file opens /proc/self/fd/{handle}
O_WRONLY on unix (the fifo attach GNU's open performs; the
reader is already held, so it cannot block); the Windows twin
declines — a pipe handle there has no complementary-open API.
materialize_for_child maps a channel end to a sink temp file
(the FullDevice precedent): the queue cannot cross the spawn
boundary, and kernel-pipe complementary ends are plain File
endpoints that do reach children.
…385)

Executor::fd_alias_complementary_write_endpoint is the alias
layer's third question, after fd_alias_reopen_path (fresh open)
and fd_alias_spelling_open_error (ENXIO): consulted only where a
WRITE-MODE spelling open is being resolved and no write endpoint
exists to share. By the source's READ endpoint kind (endpoint-
keyed, never number-keyed — a dup of stdin carries
InheritedProcessStdin, so its alias resolves to stdin's pipe
exactly like /dev/stdin's): Text/ProcessSubstitution pair with
PipeChannel over the same queue; a handle-backed FIFO (streamed
procsub pipe, inherited pipe fd, coproc stdout, pipe stdin)
pairs with the kernel's own complementary end wrapped as File;
everything else declines to the existing arms. A pure predicate
twin serves judgment faces that must decline dup service without
performing the open. write_fd_endpoint, write_output_fd_redirect,
the fd-2 diagnostic writer, the fd classification face, and
write_dev_stdio_redirect_output all write through the new kind;
the append-dup handle helper sinks it like the other symbolic
endpoints (the bound faces carry the exact shape).
…h#2385)

The walk-level twin of the channel end: command_output_fd_state
seeds it from an installed PipeChannel, open_command_output_target
resolves it through the complementary resolver after the
ENXIO/fresh-open arms, and both flush faces (write_to_fd,
write_stage_stream_to_target) append the bytes to the queue —
the latter is also how an external stage's drained complementary
write lands. Root-cause find: the walk seeded OutputTarget::Closed
for a read-only source fd, so apply_ordered_output_redirects' dup
shortcut cloned Closed for a SPELLING target and the flush failed
EBADF; the shortcut now declines when the target is a path
spelling whose source has no write endpoint but a complementary
one (an open is not a dup — cf. the #1871 dead-fd decline beside
it). No noclobber gate rides the new arm: noclobber_open
(redir.c:676) refuses regular files only, and > and >> are the
same write on a pipe (no offset for either to act on).
The compound binder and the conditional binder consult the
resolver before the dup fallbacks, so exec 4> /dev/fd/3 persists
the pipe's write end and <> keeps ONE description: the read side
copied plus the complementary write side installed (materialized
and kernel parents bind through the same resolver; the earlier
Output-arm fallback stays as a safety net). set_fd_readwrite_file
— the recorded face behind the exec path — applies the same
upgrade after its dup pair, since a plain dup of a read end
silently yields a read-only slot. Root-cause find: execute_stdio_
only_exec_redirect resolved path spellings through
redirect_target_fd_and_move, the helper that ALSO accepts path
spellings — so exec's > and <> arms copied the read end wholesale
into the slot. The exec Output arm now materializes the
complementary end for a non-dup spelling before the dup apply,
and the exec Input/ReadWrite arm does it for a ReadWrite spelling
by copying the read side and installing the resolver's write
side (GNU's real O_RDWR attach, redir.c:725). Spellings with no
complementary end keep the old dup path untouched.
…sh#2385)

External stages: apply_external_complementary_target joins all
four target chains (stdout/stderr x write/append). Root-cause
find: /dev/fd/3 on an external command fired NO branch — the
target parsed to Some(3), the source had no write endpoint, the
literal arm requires the target to parse as NO fd — so the child
inherited the shell's real stdout and the bytes leaked out of the
script with status 0. A kernel File complementary end is handed
to the child directly (the real pipe, what GNU's child inherits
through dup2); an in-memory PipeChannel end rides Stdio::piped()
plus the post-external drain, which appends the drained bytes to
the queue — inside the script's stream model, never the real fd.
The coproc stderr plan/spec and the external plan's dup-source
and ambient-seed judgments classify the new kind with the other
symbolic endpoints (absorbed/absorbed/legacy). /dev/fd operand
materialization gains DevOperandFlush::Channel: the child reads
its bytes from a temp operand and the parent appends them into
the queue at finish; the bytes arm declines a bare channel (the
queue's bytes ride the paired read endpoint), and
record_output_fd_ledger CLEARS a channel end's keys — the queue
cannot travel to a respawned shell, so no stale description may
ride the state file under its name.
s02/u02 (transient + persistent procsub spelling writes), x08
(/proc/self/fd/N exactly like /dev/fd/N), x01/x02 (exec binds:
write appends; > and >> are the same write on a pipe), x03 (<>
one description), x09 (herestring channel), y04 (repeated opens
accumulate), y05 (append ignores the read cursor), y01 (noclobber
does not refuse a pipe link), x05/y02 guards (dup EBADF stays;
closed source ENOENT), a pipeline-stage spelling redirect,
u01/x06/x07/x10 (stdin pipe: transient write, prefilled-FIFO
order, external reader, dup spelling), x04 (external-stage
complementary write over a kernel pipe), y06 (coproc read end).
Every expectation measured against GNU bash 5.3 script files;
producer-race shapes compare delivered lines as sets (GNU itself
races there). Run under the scrubbed-fd harness; 19/19.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 1, 2026 14:58
@caomengxuan666
caomengxuan666 merged commit 7a66413 into master Nov 1, 2026
10 of 11 checks passed
@caomengxuan666
caomengxuan666 deleted the design/2385-appendable-endpoint branch November 2, 2026 01:08
caomengxuan666 added a commit that referenced this pull request Nov 2, 2026
…livery contract

Refs #2521

Regression of the #2333 carrier restore, bisected: the pins in
tests/issue2333_inherited_carrier_restore_regressions.rs pass 7/7 at
7a66413 (PR #2470) and fail 5/7 at e6ccbf1 (PR #2471). The regressing
commit is ec40a88 ("deliver ambient std-stream fds across child
spawns"), which added an occupancy probe to the startup restore:
"prefer the stamped descriptor when it is open in this child".

GNU semantics (the spec): a forked child inherits the parent's whole
descriptor table, each descriptor keeping its own open file
description (bash-5.3 execute_cmd.c, execute_in_subshell, :1576).
rubash materializes that inheritance through spawn-time pins plus the
__RUBASH_FD_HANDLE_<slot> stamps, and the restore must reconstruct
each slot from the descriptor the TRANSPORT actually delivered:

- pin transports (external ambient pass, input/output procsub,
  coproc): the carrier is pinned AT the slot fd; the stamp records
  the PARENT's descriptor number, which designates nothing in the
  child (the #2333 at-slot adoption rule, 8105a63).
- whitelist respawn transport (fd::spawn_whitelisted, sole caller
  execute_background_ast_command): each extra crosses AT its stamped
  number; the child's raw slot fd can be its own read-only script
  file (the #2212 EBADF case ec40a88 was protecting).

The occupancy probe cannot tell these apart: the pin pass stages each
carrier above the ceiling and dup2s it onto its slot, leaving the
staging sources open — so in a pin-delivered child the stamped number
of slot N is open and holds a leftover copy of ANOTHER slot's
carrier. The probe adopted those leftovers and rotated every carrier
by one (n3 got body-four, n4 got the already-consumed carrier: EOF).

Fix by mechanism: the spawner declares its delivery contract and the
restore obeys the declaration instead of inferring it from fd
occupancy. execute_background_ast_command — the only whitelist
spawner — now sets __RUBASH_FD_AT_STAMPED=1 in the child env; the
startup transport-key consumption removes it (it never surfaces as a
shell variable or rides the env mirror) and the restore adopts the
stamped descriptor only when the key declared at-stamped delivery.
The fcntl occupancy probe is deleted; the #2333 at-slot rule is
unchanged for every pin transport, and the Windows path (stamped
value verbatim) is untouched.

Verification (build slot, scrubbed_cargo.py cargo test):
issue2333_inherited_carrier_restore_regressions 7/7 (was 2/7),
issue2212_bats_remainder_regressions 15/15,
issue2256_devfd_fresh_open_regressions 25/25,
issue2479_devfd_reopen_cursor_regressions 8/8,
issue2502_pipeline_alias_delivery_regressions 6/6,
issue2476_born_closed_stdout_regressions 7/7,
issue2484_stage_fd3_delivery 4/4.
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