Skip to content

fix(executor): cmdsub child drops inherited fd-1 binding - #123

Merged
caomengxuan666 merged 4 commits into
unixwin:masterfrom
daiaji:fix/cmdsub-capture-compound-redirect
Sep 24, 2026
Merged

caomengxuan666 merged 4 commits into
unixwin:masterfrom
daiaji:fix/cmdsub-capture-compound-redirect

Conversation

@daiaji

@daiaji daiaji commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

Fixes the capture-bypass behind niubash shell-quirks Q16 (docs/shell-quirks-observed.md in unixwin/niubash): inside a compound-redirect context, $( external-cmd ) came back empty and the child's output landed in the outer redirect target.

Repro (deterministic on 2bb0277c under niubash, 20/20; observed intermittently as 3/400 in a real caps-flake polling loop)

for i in $(seq 1 400); do
  out=$(cargo test -q -p caps --lib 2>&1)
  if echo "$out" | grep -q "FAILED\|panicked"; then …; fi
done > summary.txt 2>&1     # failing rounds: cargo output lands in summary.txt, $out empty

Control: builtins and pure subshells inside the same loop capture correctly; rubash standalone (own script driver) does not reproduce — the bypass needs the fd-1 binding the compound-redirect layer installs.

Root cause

  • 4fe2a48f binds compound/group/loop redirects once into the fd table (redir.c:767-955).
  • command_substitution_executor clones the parent's fd table verbatim, so the substitution child inherits that fd-1 file binding.
  • apply_external_stdout_redirect checks the fd-table fd-1 binding before the capture context (stdout_capture.is_some() || stdout_capture_active()), so an external child inside $( ) was spawned with Stdio::from(dup(file)) — straight into summary.txt — while the substitution drained an empty pipe.

GNU semantics: subst.c:7143 command_substitute runs the body with stdout on the capture pipe; only body-owned redirects may rebind it.

Fix

Drop the inherited fd-1 binding at the command_substitution_executor boundary:

  • external children reach the stdout_capture pipe branch and drain into the substitution buffer;
  • body-owned redirects still rebind fd 1 on the child's table ($( { a; b; } > f ) still writes f);
  • fd 2 stays inherited — $( ) does not capture stderr (subst.c:7149); in-body 2>&1 resolves through its dedicated branch (stderr piped, drained into the capture buffer).

Verification

  • New regression tests: cmdsub_external_capture_survives_loop_redirect_binding / …_group_redirect_binding — assert non-empty capture and no marker leak into the redirect file. 2/2 pass.
  • cargo test --lib: 420 passed / 3 failed — the 3 are pre-existing on clean 2bb0277c on this host (file-timestamp/environment family), zero new failures.
  • niubash integration: patched rubash through the niubash runtime — redirected-loop probe 0 bypass / 20 rounds (was 20/20 before the fix); full niubash workspace suite 430 passed / 0 failed.

GNU subst.c:7143 command_substitute runs the substitution body with its
stdout on the capture pipe; only body-owned redirects may rebind it.
The forked executor cloned the parent's fd table verbatim, and since
4fe2a48 (compound redirects opened once into the fd table) an
enclosing for/group redirect surfaces there as an fd-1 file binding.
apply_external_stdout_redirect preferred that binding over the capture
context, so external children inside `$( )` wrote straight into the
outer redirect target while the substitution read an empty pipe
(niubash shell-quirks Q16: a caps-flake polling loop of the form
`for ...; do out=$(cargo test 2>&1); ...; done > summary.txt` landed
the failing round's output in summary.txt and left $out empty ---
failure branches keyed on the capture silently flipped to success).

Drop the inherited fd-1 binding at the command_substitution_executor
boundary: external children then reach the stdout_capture pipe branch
and drain into the substitution buffer. Body-owned redirects rebind
fd 1 on the child's table, so `$( { a; b; } > f )` still writes f; fd 2
stays inherited because $( ) does not capture stderr (subst.c:7149).

Deterministic repro on 2bb0277 under niubash script mode (20/20 with
`$(hostname 2>&1)` / `$(cargo test -q 2>&1)` inside a redirected
loop); fixed 0/20. Regression tests:
cmdsub_external_capture_survives_loop_redirect_binding /
cmdsub_external_capture_survives_group_redirect_binding.
…compound-redirect

# Conflicts:
#	src/executor/tests.rs
caomengxuan666 added a commit that referenced this pull request Sep 24, 2026
Follow-up to the PR #123 cherry-pick (7188baf): deleting the fd-1 entry
broke every consumer that resolves output through fd_table[1] — with no
entry, write_fd_endpoint returns early and drops the bytes, so even
'$(echo hi 2>&1)' captured empty. Rebinding fd 1 to the default
FdWriteEndpoint::Stdout keeps the GNU subst.c:7143 semantics (the
endpoint resolves to the active stdout_capture) while preserving fd
resolution for in-body writes and '2>&1' dups.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666

Copy link
Copy Markdown
Member

Fixed and verified on master: commit 0b45379 (fix(executor): cmdsub child drops inherited fd-1 binding) landed 15:02, this day. Regression tests cmdsub_external_capture_survives_loop_redirect_binding and cmdsub_external_capture_survives_group_redirect_binding pass 2/2; manual loop repro captures correctly (got=round1/2/3) with no output leaking to the redirect target. Closing per verification.

@caomengxuan666

Copy link
Copy Markdown
Member

Reopening: the equivalent fix landed on master as 0b45379 (with this PR's authorship preserved — commit author = @daiaji), but to give this PR the proper Merged record the branch needs a conflict refresh (master moved ahead ~15 commits, including the same fix + follow-ups in the fd/cmdsub region).

@daiaji could you update this branch with master (resolve the conflict in src/executor/command_substitution.rs — take master's comment wording; note both sides added identical copies of the cmdsub_external_capture tests in executor/tests.rs, keep one copy) and push? Once the branch is mergeable I'll merge it immediately. Thanks!

…compound-redirect

# Conflicts:
#	src/executor/command_substitution.rs
@daiaji

daiaji commented Sep 24, 2026

Copy link
Copy Markdown
Contributor Author

Refreshed the branch with master as requested: merge 14a9c41.

  • src/executor/command_substitution.rs — resolved with master's comment wording (the rebind-to-Stdout-endpoint code itself was already identical on both sides).
  • src/executor/tests.rs — kept master's copy of cmdsub_external_probe_command + the two cmdsub_external_capture_* regression tests and dropped the duplicate PR-side copy, so a single copy remains.

Result: git diff origin/master HEAD is empty — the branch tree is byte-identical to master, so the PR now carries exactly master's fix + follow-ups (0b45379 rebind follow-up ef74342 included) and is mergeable.

Verification on the refreshed tree: cargo build --locked clean; cargo test --locked --lib cmdsub_external passes 2/2.

One heads-up on the merge gate: the "Rust tests" CI job currently fails on the repo-wide check-utf8-boundary-hygiene step (12 unreviewed byte→char widening sites in src/executor/arithmetic/value.rs, declare_local.rs, conditional.rs, prompt_expansion.rs, alias_helpers.rs). None of these files are touched by this PR — the tree is identical to master, and master's own recent push runs (a48d3fb, fd1ea48) fail the same step, so this PR inherits the red rather than introducing it. The other three checks (Cross-target check, Shell functionality tests, and the UTF-8-independent steps) pass, and GitHub reports the PR as MERGEABLE. Happy to help sweep those 12 sites into the allowlist/fixes separately if useful.

caomengxuan666 added a commit that referenced this pull request Sep 24, 2026
…aster into PR #123 branch

Both master (0b45379) and PR #123 (7188baf/28d5757d) implemented the
fd-1 rebind fix with the same regression tests; the auto-merge
concatenated the duplicate copies. Keep master's comment wording and
one copy of the tests (2/2 pass).

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666
caomengxuan666 merged commit c917742 into unixwin:master Sep 24, 2026
3 of 4 checks passed
@daiaji
daiaji deleted the fix/cmdsub-capture-compound-redirect branch September 24, 2026 16:15
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.

2 participants