Skip to content

[bug] Pre-opened fds 3/4 are invisible inside $(): { v=$(cmd 3>&1 1>&4); } 4>&1 captures BOTH streams — nvm-exec nvm_rc_version fd-3 protocol collapses (nvm f28 fast-suite) #368

Closed

Description

@caomengxuan666

Summary

$( ) does not see extra file descriptors pre-opened by an enclosing group
redirection: a 3>&1 / 1>&4 juggle inside the substitution silently falls
back to the capture pipe, so writes intended for the outer stdout are
captured instead. This is the remaining blocker for the nvm fast-suite test
"Running 'nvm-exec' should display required node version" (f28): it fails
on the pristine base (2e6b0e9) and on the current hist274 lane build
identically — reported by the sig22 lane as "test shell xtrace inherited by
the nvm-exec subprocess into the capture"; reproduction here shows the
capture corruption is fd-shape, not xtrace (verification below).

Minimal reproducer (byte-for-byte divergence)

#!/usr/bin/env bash
emit() { echo "progress"; echo "value" >&3; }
{ captured="$(emit 3>&1 1>&4)"; } 4>&1
echo "captured=[$captured]"
  • GNU 5.3.0 (WSL /usr/local/bin/bash): prints progress to real stdout, then captured=[value].
  • rubash (base 2e6b0e9 release, and hist274 lane): captured=[progress\nvalue] — no outer print, both lines captured.

Primitive decomposition (every line diverges):

{ c1="$(echo tofd3 >&3)"; } 3>&1          # GNU: "tofd3" outside, c1=""; rubash: c1="tofd3"
{ c2="$(echo progress 1>&4)"; } 4>&1      # GNU: "progress" outside, c2=""; rubash: c2="progress"
{ c3="$(echo progress 1>&4; echo value >&3)"; } 3>&1 4>&1   # GNU: both outside, c3=""; rubash: c3=both

So BOTH halves of the juggle are broken inside $( ): an fd3 established
outside is not honored for >&3 writes, and 1>&4 does not move the child's
stdout. Nested plain comsubs inside the juggled region capture correctly
(verified), i.e. the bug is specifically the pre-opened fd table crossing the
command-substitution boundary.

Fixture evidence (nvm f28)

nvm.sh's nvm_rc_version ends with nvm_echo "${NVM_RC_VERSION}" >&3 and
prints Found '<path>' with version <v> to stdout; nvm-exec consumes it
with { NVM_RC_VERSION="$(nvm_rc_version 3>&1 1>&4)"; } 4>&1. With the fd
table dropped, the captured "version" becomes the multi-line
Found '...' ...\nv0.42, every downstream nvm version/error path parses it
as invalid, and the test dies at its first [ "$EXPECTED" = "$OUTPUT" ]
compare. Full-test artifacts: target/hist274/out/ in the hist274 worktree
(the raw failing compare is in the run log; minimized forms m*/t4/t5 under
/tmp/nvmf28).

Observed downstream corruption in the same capture (color code 0;31m and
tput colors output 256 appearing as their own lines inside the capture)
follows from nvm.sh's error path re-running under the corrupted fd state; the
minimal primitive above is the root.

GNU anchor

  • subst.c:7143 command_substitute(): the comsub child runs with fd1 = the
    capture pipe plus the inherited fd table; redirections inside the
    substituted command are applied by redir.c do_redirections against that
    inherited table, so 3>&1 (fd3 := pipe) and 1>&4 (fd1 := outer stdout,
    fd4 pre-opened by the enclosing { ... ; } 4>&1 group redirect) take effect.
  • Group redirections for { } compounds: execute_cmd.c applies
    do_redirections around the group body and undoes them after.

xtrace verification (the sig22 hypothesis, checked and narrowed)

GNU's option-propagation model (shell.c:1971-1987 initialize_shell_options:
children import $SHELLOPTS from the environment only when not
privileged/setuid; variables.c:510 marks an imported SHELLOPTS readonly;
bash never auto-exports SHELLOPTS — printenv SHELLOPTS under set -x is
empty) was verified empirically: a GNU child bash of a set -x parent does
NOT trace, and rubash's child processes also start xtrace-clean in every
shape probed (direct external child, #!/usr/bin/env bash shebang child
resolved to rubash, rubash -c child, ambient-env probe). No xtrace
inheritance could be reproduced on base or lane; the f28 capture corruption
is fully explained by the fd bug above. If a harness environment ever carries
SHELLOPTS in the ambient env, rubash's import side is
src/executor/init.rs (SHELLOPTS/BASHOPTS replay) — the model matches GNU's
import-at-startup; the export side is where any future xtrace leak would have
to originate, and no such leak exists today.

Environment

  • rubash base 2e6b0e9 + hist274 lane (both release builds), Windows 10
    19044, Git Bash parent.
  • GNU baseline: WSL owner-compiled 5.3.0 /usr/local/bin/bash (script-file
    comparison per AGENTS.md).

Activity

  1. added
    bug
    gnu-baseline
    on Oct 1, 2026
  2. added 2 commits that reference this issue on Oct 1, 2026
  3. caomengxuan666 commented on Oct 1, 2026

    @caomengxuan666
    MemberAuthor

    Landed as 3d311ca (wt25/fdvis lane commit 09de57d3). Root cause: the fd-1 dup record's Some(None) arm ('real stdout snapshotted at dup time') routed through write_stdout_bytes, which re-resolved the ACTIVE capture — inside a comsub those bytes landed in the substitution buffer instead of escaping. Fix: write_real_stdout_uncaptured factored out and used by all three write arms; write_diagnostic_fd2 honors fd 2's dup record; command_substitution_executor clears fd 1's alias record at the fork boundary (GNU subst.c:7320 fresh-description rule — fd >= 3 bindings survive verbatim per fork semantics). 15-case fd matrix + 11 regression tests byte-identical to WSL GNU 5.3.0; true-baseline comsub/comsub2/procsub/redir slices byte-identical pre/post fix. CI green on 3d311ca. The nvm-exec nvm_rc_version fd-3 protocol should now hold — will verify in the 1.2.3 engine bump.

  4. added a commit that references this issue on Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggnu-baseline

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions