nodejs / nodejs/node

child_process: add cross-platform subprocess.killTree([signal])

未关闭
#64,406 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

child_process
主要语言
JavaScript
星标
122k
派生
37.4k
平均合并
4 天 3 小时
30 天内合并 PR
272

描述

What is the problem this feature will solve?

subprocess.kill([signal]) only targets the direct child PID. Descendants often keep running, especially when:

  • shell: true (shell exits, real command survives)
  • the child is a build driver (bash / make / ninja) that spawns grandchildren
  • on Windows, where POSIX signals are not real and kill() does not terminate a process tree

Userland workarounds are fragmented and OS-specific:

  • Unix: detached: true + process.kill(-pid, signal) (process group; not always applicable)
  • Windows: taskkill /pid <pid> /T /F (or equivalent)
  • npm packages such as tree-kill

This comes up often for Ctrl+C / timeout cleanup in CLIs and build orchestrators.

Related prior request (closed stale): https://github.com/nodejs/node/issues/40438 (killDeep).

What is the feature you are proposing to solve the problem?

Add an explicit, documented API, for example:

subprocess.killTree([signal]);
// and/or
child_process.killTree(pid[, signal]);

Goals:

  1. Cross-platform: one call that best-effort terminates the process and its descendants on Windows, Linux, and macOS.
  2. Clear semantics vs kill(): kill() stays single-PID; killTree() is the tree variant.
  3. Document limitations: PID reuse races, process-group vs true tree walk, signal mapping on Windows (where signals are not POSIX).

A reasonable Windows implementation can use the platform tree-terminate facility (today: taskkill /T); Unix can use process-group kill when the child is a group leader, otherwise a documented tree walk.

What alternatives have you considered?
  • Keep using tree-kill / ad-hoc taskkill in every project
  • Rely on detached + negative PID (Unix-only, requires spawn options up front)
  • Expand subprocess.kill() to kill trees (breaking / surprising; prefer a separate killTree name)

Naming killTree (rather than overloading kill) makes the stronger, OS-dependent behavior opt-in and discoverable.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 child_process subprocess.kill API 开始,并将 Unix 进程组方案与 issue 中描述的 Windows taskkill /T /F 进行比较。定义跨平台的 killTree 语义、信号映射和已记录的限制,然后验证其在 Windows、Linux 和 macOS 上的行为。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, node.js
领域
api, operating-systems
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
活跃
描述清晰度
需要澄清
新手友好度
38/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。