Skip to content

[bug] 编号 fd 重定向对进程内 $SH -c 子进程不生效: "$SH" -c 'echo N3 >&3' 3>f 报 3: Bad file descriptor(GNU 写入 f) #180

Closed

Description

@caomengxuan666

来源

rubash#164 验收矩阵(wt/m1-here)排查中发现;基线 c5be754 重建即复现(stash 验证与 #164 修复无关)。

现象

命令的编号输出重定向(3>file)作用在本 shell + -c 脚本形态的子进程上时,fd 3 没有进入子进程的 fd 表:>&3 在子进程内报 bash: line 1: 3: Bad file descriptor,文件为空。GNU Bash 5.3.0 中重定向随 fork 的子进程生效,N3 写入文件。脚本文件形态("$SH" p3.sh >f,fd 1)不受影响。

最小复现(m13.sh,LF 落盘双侧脚本文件运行)

SH=${THIS_SH:-bash}
"$SH" -c 'echo N3 >&3' 3>k13p.tmp
echo "P [$(cat k13p.tmp 2>/dev/null)]"
  • GNU 5.3.0 (WSL, THIS_SH=/usr/local/bin/bash): P [N3],rc=0
  • rubash (wt/m1-here @ c5be754): bash: line 1: 3: Bad file descriptor + P [],rc=0

GNU 锚点

execute_cmd.c:5771 execute_disk_command:fork 的子进程在 :5884 以 do_redirections (redirects, RX_ACTIVE) 应用整条重定向列表(含编号 fd),随后 exec——fd 3 在子进程全程可见。rubash 的进程内 -c 子路径(external_finish.rs execute_same_shell_script/execute_direct_shell_script 一族)未把 3>file 之类的编号重定向绑定进子作用域 fd 表(fd 1 的 >file 正常,说明仅编号 fd 漏绑)。

产物

  • target/issue-suites/results/childfd-not-bound/(wt/m1-here): m13.sh、m13.rub.out、m13.gnu.out
  • 验证命令: target/debug/rubash.exe m13.sh vs MSYS_NO_PATHCONV=1 wsl bash -c "export THIS_SH=/usr/local/bin/bash; bash m13.sh"(各自 cwd 下)

版本: wt/m1-here @ c5be754,对照 WSL GNU Bash 5.3.0(1)-release。

Activity

  1. caomengxuan666 commented on Sep 27, 2026

    @caomengxuan666
    MemberAuthor

    已在 de86dd5/9b1eb043 修复并关闭(四个独立 commit + captain darwin pipe2 热修)。验收矩阵与双侧产物见各 commit message 及 worktree target/ 探针;套件切片 trap/comsub/redir/attr/func 均 0 diff;474/474 + 双交叉干净。

  2. added
    bug
    area:fd
    on Oct 17, 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

    area:fdbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions