Skip to content

WIP: fd delivery — ambient/command fds to children (rubash#1686 #1687 #1688) - #1783

Merged
caomengxuan666 merged 7 commits into
masterfrom
fix8/fd-delivery
Oct 27, 2026
Merged

caomengxuan666 merged 7 commits into
masterfrom
fix8/fd-delivery

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes #1686
Fixes #1687
Fixes #1688

三条 fd 交付修复,各一 commit(簇 REPORT:~/workspace/rubash-fix8/clusters/fd-delivery/REPORT.md):

簇侧闸门(基线 5418fb8,本树):targeted 1626 12/12、1642 11/11、1643 9/9、1686 5/5、1687 7/7;fd/redirect 族守卫 RC=0;lib 771/1/1 失败集与基线同集;diffcorpus 失败集 {gl08, ms10} 为本基线(早于 #1609)的 known-stable 集。

集成注记:本分支基线较旧,rebase 到现行 master 后须补跑真守卫 + diffcorpus(届时 gl08 应转 PASS、失败集应收敛 {ms10})。

…lot numbers (rubash#1626)

The fd table's descriptors reached only rubash children via the
__RUBASH_FD_* env channel; foreign children got EBADF for every fd
bound by exec N>file / N<file / N<>file, and the handles are opened
O_CLOEXEC so nothing leaked through the raw fd space either. On Unix
the spawn path now registers a pre_exec pin that dup2's each ambient
fd-table descriptor (File/Coproc endpoints) onto its slot number in
the child, mirroring GNU fork inheritance. Slots the command itself
redirects are skipped (its binding is the more local one), and the
spawn machinery's own exec-error pipe is never clobbered.

Regression tests: tests/issue1626_exec_fd_child_visibility_regressions.rs
(9 pass; the pipeline-member listing form is pinned #[ignore] as an
adjacent gap — pipeline spawns do not pass through this path).
…bash#1642)

The /dev/stdin arm consulted only the pipeline stage buffer and the
process stdin handle, so source /dev/stdin <<< '...' and
source /dev/stdin < file silently sourced nothing — the command's
fd-0 binding lives in the command node / virtual fd table and never
reaches the process handle. The arm now resolves through the same
stdin ladder every other consumer uses (command stdin text via
stdin_string_for_command, exposed to builtins as command_stdin_text),
and when the command carries its own fd-0 redirection that resolves
to no text it sources nothing instead of falling through to a stdin
GNU would not consult.

Regression tests: tests/issue1642_source_dev_stdin_regressions.rs
(11 pass; pipeline / process-stdin / function / compound forms are
controls that already worked).
The no-name arm of execute_builtin_direct_command returned success
without applying the invocation's redirections, so builtin > f
created nothing. GNU performs the command node's redirections
regardless of the named builtin; the arm now runs the same
apply_no_output_builtin_redirects_with_status helper the
:/true/false arms use, with status 1 on redirect failure.

Regression tests: tests/issue1643_bare_builtin_redirect_regressions.rs
(8 pass; the numbered-fd 3> form is pinned #[ignore] — the shared
helper walks only the stdio fields, a gap bare ':' shares).
…rubash#1687)

apply_no_output_builtin_redirects_with_status only walked the structured stdio fields (fds 0-2) of a bare builtin / ':' / true / false node, so numbered redirects in cmd.redirects were silently skipped: ': 3>file' created nothing, a bad path returned 0, and ': 3>&9' from a closed source succeeded. GNU performs every redirection on the command (redir.c do_redirections) even when the builtin writes nothing itself.

Add the numbered-fd pass after the structured stdio block, before the Ok(redirect_failed) return: skips {fd}> var form, fd-less and fd<3 entries; Output-family arms mirror the bare-builtin arm of execute_materialized_command (closed target -> nothing, dup target -> validate_builtin_dup_source, else create via create_redirect_output with the procsub-materializer guard); Append reuses the existing append arm; Input validates via probe_input_redirect; ReadWrite opens create+read+write; Duplicate kinds validate on the moved value; combined kinds stay no-ops.

Tests: tests/issue1687_numbered_fd_no_output_builtin_regressions.rs (7 cases measured against GNU bash 5.3.20: create, truncate, append, bad-path rc=1, input missing/present, dup closed/open) and the de-ignored known-gap pin in tests/issue1643_bare_builtin_redirect_regressions.rs.
…ash#1688)

Only the single-command external spawner pinned the shell's ambient descriptors (exec 9>file) into children; hand-built pipeline stage processes never got them, so a pipeline member listing or writing through an ambient fd saw EBADF where GNU's fork shares the descriptor table with every member (execute_cmd.c execute_pipeline / make_child). The concurrent loops were only part of the surface: the sequential stage executor in pipeline_stages.rs builds its child by hand too and was the path taken by mixed builtin-headed pipelines.

Extract the ambient pin computation from spawn_external_process verbatim into ambient_fd_pins_for (with the injected-group fall-through refinement) plus attach_ambient_fd_pins in unix (pre_exec) and non-unix (empty) forms, and attach at every hand-built pipeline spawn point: the shell-writer consumer loop, spawn_wait_concurrent_external_stages, execute_external_prefix_streaming_tail, the not(windows) concurrent loop (all pipeline_exec.rs), and execute_external_pipeline_stage_inner (pipeline_stages.rs).

Tests (tests/issue1626_exec_fd_child_visibility_regressions.rs): the pipeline known-gap pin is de-ignored and two cases are added — the second stage's /proc/self/fd listing contains the ambient fd, and a member writes through an ambient fd mid-pipeline. Expectations measured against GNU bash 5.3.20.
…en (rubash#1686)

spawn_external_process opened a command's numbered redirects in the parent (the files were created) but delivered the descriptors to the child only through the __RUBASH_FD_* env channel, which just rubash children consume: a foreign child (bash -c 'echo x >&3' with 3>file) got EBADF. GNU opens the redirects in the child after fork, so the descriptor itself is inherited (redir.c do_redirections; execute_cmd.c).

On Unix, record each opened descriptor as a command_fd_pin during the ordered redirect pass and merge the pins into the single pre_exec registration after the ambient pins, so a command binding wins any slot both sides name (the pin helper dup2's in list order). The env channel stays for rubash children.

Collision ruling (measured on the baseline): the redirect pass skipped re-opening whenever ANY table entry existed for the fd, but an ambient entry is not this command's binding — baseline created own2.txt via the pass yet never delivered it. The skip now applies only when the entry is an injected group redirect (that entry IS the binding and arrives via the ambient pins); a merely ambient entry no longer skips, and the command's own open wins the slot.

Tests: tests/issue1686_command_redirect_fd_child_delivery_regressions.rs (5 cases driving a real foreign child: write, read, append, /proc/self/fd listing, and command-fd-wins-over-ambient collision). Expectations measured against GNU bash 5.3.20.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 26, 2026 12:38
Integrate PR #1783 onto current master for re-gating on the merged
tree. The three precondition commits (aa097de #1626, e6769a7 #1642,
5418fb8 #1643) are patch-equivalent to master (git cherry '-') and
drop out of the net diff; the delivery is the three per-issue commits
(755d151 #1687, 818da34 #1688, 9f0f347 #1686), unmodified.

Conflicts and resolutions (bilateral preservation throughout):

- src/executor/external_inner.rs (2 hunks):
  1. fd_is_anonymous_pipe: took master's u32-widened S_IFMT comparison
     (bionic compile fix) — same semantics, supersedes the branch's
     raw comparison from the #1626 commit.
  2. spawn_external_process pin computation: took the branch side —
     the table is computed by the extracted ambient_fd_pins_for helper
     (verbatim extraction of the loop master carries inline, plus the
     injected-group fall-through) and the #1686 command_fd_pins are
     appended after the ambient pins so a command binding wins a
     contested slot. The helper also serves the pipeline attach points,
     so spawn and pipeline paths share one computation; the
     command-redirect skip ruling is preserved inside the helper, and
     the #1686 collision ruling (re-open skipped only for injected
     group redirects) survived in the auto-merged region above.
- src/executor/pipeline_exec.rs (4 hunks): union at all four spawn
  sites — the branch's #1688 attach_ambient_fd_pins call AND master's
  ms-stopbleed G-02 process.current_dir(&self.shell_state.cwd) stamp
  both kept at every site (shell-writer consumer loop,
  spawn_wait_concurrent_external_stages,
  execute_external_prefix_streaming_tail,
  execute_external_pipeline_concurrently).
- tests (add/add, resolved to the superset side each):
  issue1626: branch side — #1688 de-ignored the pipeline known-gap
  pin and added two tests; the only master-only line was the removed
  #[ignore] itself.
  issue1642: master side — master carries two extra tests
  incorporated from PR #1685 under the 2026-10-26 fd-source ruling;
  the branch side had nothing master lacks.
  issue1643: branch side — identical to master except the #1687
  commit's de-ignore of the numbered-fd known-gap pin.

Auto-merged overlap verified: pipeline_stages.rs carries the #1688
attach beside the #438 attach; builtin_redirects.rs carries the
#1687 numbered-fd loop; merged-tree diff vs master is exactly the
8 delivery files. src/lexer/continuation.rs untouched.
@caomengxuan666

Copy link
Copy Markdown
Member Author

集成完成(合并树全门已跑,待主会话合并)

合并 commit 7fd264f8:普通 merge 当前 master 8fffc99e 入 fix8/fd-delivery(未 rebase、未 force push)。盘点:三条前置 commit(aa097de #1626、e6769a71 #1642、5418fb87 #1643)经 git cherry 证为与 master 补丁等价、净差自动消解;交付恰为三条逐单 commit(755d151e #1687、818da34e #1688、9f0f347c #1686),原样未改。合并树 vs master 净差 = 8 个交付文件。

冲突处置(5 文件,逐处双边保全)

合并树门(单槽、JOBS=1、净化环境,日志 + .done 实存非空)

完整报告:~/workspace/issue-clearance/clusters/fd1783-integ/REPORT.md。本 PR 正文关单词未动(逐行 Fixes #1686/#1687/#1688),未自合。

@caomengxuan666
caomengxuan666 merged commit 2815333 into master Oct 27, 2026
8 of 9 checks passed
@caomengxuan666
caomengxuan666 deleted the fix8/fd-delivery branch October 30, 2026 01:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant