Skip to content

fix(executor): pass process-substitution fds to external command arguments on Unix - #468

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/procsub-fd-arg
Oct 9, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/procsub-fd-arg

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fix external commands receiving a process substitution as an argument on
Unix: the literal /dev/fd/N argv word now resolves in the child, as in
GNU bash.

Problem (#438)

diff <(echo a) <(echo a)
# diff: /dev/fd/63: No such file or directory

GNU forks before exec'ing an external command, so the child inherits the
shell's descriptors and resolves /dev/fd/N through the OS /dev/fd layer
(execute_cmd.c passes argv to execve unchanged; the live pipe read end at
descriptor N is the whole contract — subst.c:6362 process_substitute ->
move_to_high_fd -> make_dev_fd_filename). rubash's virtual fd table is not
the child's descriptor table: the substitution's buffered endpoint had no
real descriptor, so the child's open("/dev/fd/N") failed ENOENT. Only
the procsub-as-argument path was broken (cat < <(...), mapfile < <(...)
already worked).

Fix

  • materialize_dev_fd_operands (src/executor/dev_fd_operands.rs): on
    Unix, a /dev/fd/N operand naming a buffered virtual read endpoint
    (Text/ProcessSubstitution) is now backed with a real anonymous pipe
    carrying the endpoint's remaining bytes (fed from a writer thread so a
    payload larger than the kernel pipe buffer cannot deadlock the shell).
    The argv word itself stays literal /dev/fd/N, as in GNU. File-backed
    endpoints keep the literal word (the OS resolves it natively).
  • attach_unix_dev_fd_operands: a pre_exec hook (dup2, async-signal-
    safe) pins each recorded read end at its /dev/fd/N number in the child
    between fork and exec; it also clears FD_CLOEXEC when the fresh pipe fd
    landed on the target number itself (dup2(fd, fd) does not). The parent
    closes its copies when the materialization record drops, matching GNU's
    post-fork close unwind.
  • Wired into every Unix spawn path that consumes the record:
    spawn_external_process (simple commands), the sequential pipeline
    stage executor, and both command-substitution external spawns.

On Windows nothing changes: the documented platform contract remains
temp-file materialization of the virtual endpoint (rubash#355 residuals).

Residuals (documented, out of scope)

  • exec diff <(a) <(b) (the exec builtin's own spawn helpers in
    builtins::exec) does not yet attach the pipes; same behavior as before.
  • >(cmd) as an external argument keeps its previous behavior.

Tests

  • New tests/issue438_procsub_fd_arg_regressions.rs (black-box,
    CARGO_BIN_EXE_rubash, #[cfg(unix)]): external arg read, pipeline
    stage, the issue's diff reproducer (equal and differing inputs),
    independent streams, >64 KiB payload (writer-thread path), procsub arg
    inside $(...), and the argv word staying /dev/fd/N (no temp-path
    rewrite on Unix).
  • Existing issue355_procsub_dev_fd_form_regressions (incl. its external
    diff/wc cases) green.

Oracle note

WSL was unavailable in the fix environment, so behavior was contrasted
against Git for Windows' real GNU bash 5.2.37 (msys build); #438 is the
classic fork/exec /dev/fd inheritance contract, not a 5.3 feature, so
5.2 is a valid reference. (The msys child itself cannot reopen a foreign
/proc fd on Windows, so the oracle comparison there is limited to word
forms; the byte contracts in the new tests are the longstanding GNU
semantics.)

Closes #438

@caomengxuan666
caomengxuan666 merged commit d137b64 into master Oct 9, 2026
7 of 8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/procsub-fd-arg branch October 9, 2026 14:30
caomengxuan666 added a commit that referenced this pull request Oct 10, 2026
…483)

GNU folds `word<(` into the word via read_token's fall-through
(parse.y:3793-3797) and read_token_word's shellexp arm
(parse.y:5494-5524), with the `(...)` body consumed under
parse_matched_pair's LEX_GTLT discipline (parse.y:4147-4153) where
newlines are ordinary body characters. rubash's word scanner already
spans the body (skip_cmd_subst), but the tokenizer's logical-line join
gate had no signal for it, so `echo outer<(` NL `echo inner` NL `)` —
and the h4l/json.bash jb-cli `jb menu:json@<(...)' nesting example —
died with `syntax error near unexpected token `)'`.

- word_ends_in_open_process_substitution (continuation.rs): quote/
  escape/backtick-aware scan of the last word token's raw span; answers
  whether the word ended inside an unclosed word-attached `<(`/`>(`.
- GroupScanFeeder join gate (lexer/mod.rs): when the pass's last token
  is that word, hold the logical line open for the next physical line,
  clear the tail-resume checkpoint (the pass ended mid-word), and let
  token_level_needs_more report it so the incremental group reader
  (script/`-c` execution) keeps appending too.
- Bare `<(` (word-initial) is untouched — the parser's own procsub
  bridge handles it; #424's extglob redirect rejections stay green;
  #339/#355/#468 procsub suites pass.

Oracle: GNU bash 5.2.37 msys (D:\Git\usr\bin\bash.exe) — rc=0 on every
accepted shape, byte-identical stdout on executed shapes. Known
out-of-scope gap, pre-existing on master: interactive/stdin-source
completeness (stdin_source_text_needs_more) and the `-c` space-form
body still lack their own arms; truncated-EOF diagnostics unchanged.

Tests: tests/issue463_multiline_procsub_word_regressions.rs (9 cases:
minimal repro, line-spanning continue, jb-cli nesting, output procsub,
comsub-wrapped, single-line forms, #424 rejections, quoted opener).
src/executor/path.rs is cargo fmt reformat of pre-existing test code.

Closes #463
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.

[bug] 进程替换作为外部命令参数时 /dev/fd/N 指向不存在的 fd

1 participant