Skip to content

fix(executor): coproc 子进程继承父函数表快照,DEBUG trap 函数可解析 (fixes #441) - #469

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/coproc-functable
Oct 9, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/coproc-functable

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

closes #441

问题

GNU coproc 通过 fork 创建 coprocess(execute_cmd.c coproc_getfd -> make_child),子进程完整继承父 shell 的函数表。rubash 的 coproc 是 spawn 一个新的内部 rubash -c 子进程,此前只传递了导出变量与 trap 状态,没有传递函数表。于是 coproc 之前安装的 DEBUG trap(如 oh-my-bash 的 bash-preexec 注入的 __bp_preexec_invoke_exec)在子进程里触发时解析不到函数定义,退回 PATH 查找并报 command not found(每条命令两次)。

修复

execute_coproc_command(src/executor/compound_exec.rs)在 spawn coprocess 子进程时,把父 shell 的完整函数表以 BASH_FUNC_name%% 环境项的形式做成快照传给子进程(Executor::new 启动时导入为函数并剥离载体键)。语义与 GNU fork 对齐:

  • 子进程看到全部函数,DEBUG trap 正常调用 preexec 函数;
  • 快照只进入子进程环境,父进程的函数表与 export 标记不受影响(单向快照,无泄漏);
  • 未导出的函数不会被子进程再导出给孙进程,export -f 语义不变。

验证

  • 复现脚本(函数 + trap 'preexec' DEBUG + coproc):修复前子进程报 preexec: command not found,修复后输出 PREEXEC: inside-coproc,无诊断噪音。
  • GNU 对照 oracle:WSL 不可用,使用 Git for Windows 真 GNU bash 5.2.37(msys,D:\Git\usr\bin\bash.exe)运行同一脚本。coproc + DEBUG trap + 函数表是 5.x 稳定语义,5.2 可作对照;oracle 下 coproc 子进程正常执行 preexec inside-coproc,与本修复行为一致。未观察到语义差异。
  • 新增回归测试(tests/cli_tests/process_substitution.rs):
    • coproc_child_inherits_function_table_for_debug_trap:DEBUG trap 在子进程内成功调用父函数;
    • coproc_function_snapshot_is_one_way_into_child:子进程可见函数、父进程函数表保留且无新增导出(单向快照)。
  • cargo test --test cli_tests -- --test-threads=1 process_substitution 全绿;--test issue352_coproc_reap_regressions 全绿;--lib 624 全绿。
  • 注:本机与另一 worktree 并行跑全量套件时,个别依赖外部命令(grep/tee/tr)的用例会间歇性 127(命令查找在并发负载下偶发失败),在基线(HEAD~1)上同样复现、与本改动无关。

cargo fmt 已执行。

@caomengxuan666
caomengxuan666 merged commit 42e41b9 into master Oct 9, 2026
7 of 8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/coproc-functable branch October 9, 2026 14:30
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.

[bug] coproc 子进程丢失函数表,DEBUG trap 引用的 preexec 函数报 command not found

1 participant