Repository navigation
fix(executor): cmdsub child drops inherited fd-1 binding - #123
Conversation
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
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>
|
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. |
|
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
|
Refreshed the branch with master as requested: merge 14a9c41.
Result: Verification on the refreshed tree: One heads-up on the merge gate: the "Rust tests" CI job currently fails on the repo-wide |
…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>
Fixes the capture-bypass behind niubash shell-quirks Q16 (
docs/shell-quirks-observed.mdin 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
2bb0277cunder niubash, 20/20; observed intermittently as 3/400 in a real caps-flake polling loop)Control: builtins and pure subshells inside the same loop capture correctly;
rubashstandalone (own script driver) does not reproduce — the bypass needs the fd-1 binding the compound-redirect layer installs.Root cause
4fe2a48fbinds compound/group/loop redirects once into the fd table (redir.c:767-955).command_substitution_executorclones the parent's fd table verbatim, so the substitution child inherits that fd-1 file binding.apply_external_stdout_redirectchecks the fd-table fd-1 binding before the capture context (stdout_capture.is_some() || stdout_capture_active()), so an external child inside$( )was spawned withStdio::from(dup(file))— straight intosummary.txt— while the substitution drained an empty pipe.GNU semantics: subst.c:7143
command_substituteruns 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_executorboundary:stdout_capturepipe branch and drain into the substitution buffer;$( { a; b; } > f )still writesf);$( )does not capture stderr (subst.c:7149); in-body2>&1resolves through its dedicated branch (stderr piped, drained into the capture buffer).Verification
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 clean2bb0277con this host (file-timestamp/environment family), zero new failures.rubashthrough the niubash runtime — redirected-loop probe 0 bypass / 20 rounds (was 20/20 before the fix); full niubash workspace suite 430 passed / 0 failed.