Skip to content

fix: kill 杀不掉后台 shell 子进程 + 管道写端无 SIGPIPE (#737) - #790

Merged
arookieofc merged 2 commits into
masterfrom
fix/737-kill-background-sigpipe
Oct 18, 2026
Merged

arookieofc merged 2 commits into
masterfrom
fix/737-kill-background-sigpipe

Conversation

@arookieofc

Copy link
Copy Markdown
Member

Closes #737

问题 1:kill 杀不掉后台任务

cmd & 启动的后台任务,kill %1 杀不掉——因为 & 实际 spawn 的是一个 wrapper 进程,kill 只杀死了 wrapper,真正的 shell 子进程还在跑。

修复

wrapper 的 pre_exec 里用 prctl(PR_SET_PDEATHSIG, SIGKILL)(带 race 检查),父进程死后子进程自动被 SIGKILL;并通过环境变量 __RUBASH_BACKGROUND_WRAPPER=1 标记 wrapper 身份。KILL/TERM/INT/HUP 四种信号验证均为 DIED。

问题 2:管道写端无 SIGPIPE

execute_shell_writer_pipeline 在 Unix 上是个空 stub,根本没启用。

修复

把 Windows 实现的 #[cfg(windows)] 放宽为 #[cfg(any(windows, unix))],并加 Unix 版 stdio 转换分支。验证 PIPESTATUS=141 与 GNU 一致。

顺带

修了 kill %N 立刻丢 job 表项的问题(现在与 GNU 一致,job 保留到被 wait/reap)。

验证

issue345 测试 5/5 通过;cargo test --lib 608 通过(1 个失败是 root 环境下属主检查的 pre-existing 问题)。

未做

裸 coproc 的 parser 问题(与本 issue 无关,另案处理)。

@github-actions

Copy link
Copy Markdown
Contributor

欢迎!这是你的第一个 PR 🎉 请确认已跑过本地测试套件;CI 通过后维护者会 review。

@arookieofc
arookieofc merged commit b43a79c into master Oct 18, 2026
6 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/737-kill-background-sigpipe branch October 18, 2026 09:29
caomengxuan666 added a commit that referenced this pull request Oct 18, 2026
kill %1 followed by jobs/wait printed '[1]+  Exit 143  while true; do
true; done' where GNU bash 5.3.0 prints nothing: a non-interactive
reader suppresses the completion notice of a job the kill builtin
itself terminated (jobs.c notify_of_job_status, the
DONT_REPORT_SIGINT/SIGPIPE/SIGTERM + trapped arm - nothing printed,
the dead job is cleaned up), and #790 (rubash#737) had dropped the
jobspec-kill table cleanup to keep wait resolving the signal status.

Restore the cleanup with the status-preserving removal: stash the
signal status, close the coproc endpoints, drop the entry via
remove_job_by_pid_preserve_status. kill %1; jobs lists nothing, no
notice fires at the reap boundary, and a pid-shaped wait still
resolves 143 through completed_statuses (#737's contract). The signal
set follows the DONT_REPORT arm (SIGINT/SIGPIPE/SIGTERM or trapped,
the same set report_pending_signal_notices already implements):
kill -9 %1 keeps the entry so the async reap still announces Killed
(GNU-verified), and pid operands are untouched for the #737
wrapper-death flow.

Differential pins (part_036): test_kill_jobspec_leaves_no_completion_
notice and test_kill_pid_operand_then_wait_reports_signal_status,
byte-verified against the WSL GNU 5.3.0(1)-release oracle.
caomengxuan666 added a commit that referenced this pull request Oct 18, 2026
kill %1 followed by jobs/wait printed '[1]+  Exit 143  while true; do
true; done' where GNU bash 5.3.0 prints nothing: a non-interactive
reader suppresses the completion notice of a job the kill builtin
itself terminated (jobs.c notify_of_job_status, the
DONT_REPORT_SIGINT/SIGPIPE/SIGTERM + trapped arm - nothing printed,
the dead job is cleaned up), and #790 (rubash#737) had dropped the
jobspec-kill table cleanup to keep wait resolving the signal status.

Restore the cleanup with the status-preserving removal: stash the
signal status, close the coproc endpoints, drop the entry via
remove_job_by_pid_preserve_status. kill %1; jobs lists nothing, no
notice fires at the reap boundary, and a pid-shaped wait still
resolves 143 through completed_statuses (#737's contract). The signal
set follows the DONT_REPORT arm (SIGINT/SIGPIPE/SIGTERM or trapped,
the same set report_pending_signal_notices already implements):
kill -9 %1 keeps the entry so the async reap still announces Killed
(GNU-verified), and pid operands are untouched for the #737
wrapper-death flow.

Differential pins (part_036): test_kill_jobspec_leaves_no_completion_
notice and test_kill_pid_operand_then_wait_reports_signal_status,
byte-verified against the WSL GNU 5.3.0(1)-release oracle.
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.

[S9] process ownership: kill does not terminate background shell children (and wait fabricates 128+n); no SIGPIPE for builtin writers

1 participant