`node:child_process.fork` does not generate cpu-prof when process is killed
还没有人认领这个 Issue。
- 主要语言
- JavaScript
- 星标
- 122k
- 派生
- 37.4k
- 平均合并
- 4 天 3 小时
- 30 天内合并 PR
- 272
描述
Version
v20.17.0
Platform
Darwin Aris-MacBook-Air.local 23.6.0 Darwin Kernel Version 23.6.0: Mon Jul 29 21:16:46 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T8112 arm64
Subsystem
No response
What steps will reproduce the bug?
// repro.mjs
import { fork } from "node:child_process";
import { Worker } from "node:worker_threads";
import { existsSync, writeFileSync } from "node:fs";
import assert from "node:assert";
writeFileSync( "./example.mjs", `
console.log("Hello world");
// Keep alive
setInterval(() => {}, 1_000);
`, "utf8");
const subprocess = fork("./example.mjs", { execArgv: ["--cpu-prof", "--cpu-prof-dir=forks-profile"] });
const onExit = new Promise((r) => subprocess.on("exit", r));
await new Promise((r) => setTimeout(r, 1000));
subprocess.kill();
await onExit;
const thread = new Worker("./example.mjs", { execArgv: ["--cpu-prof", "--cpu-prof-dir=threads-profile"] });
await new Promise((r) => setTimeout(r, 1000));
await thread.terminate();
assert(existsSync("./threads-profile"), "Threads profile missing");
assert(existsSync("./forks-profile"), "Forks profile missing");
$ node repro.mjs
Hello world
Hello world
node:internal/modules/run_main:129
triggerUncaughtException(
^
AssertionError [ERR_ASSERTION]: Forks profile missing
at file:///x/repros/scripts/repro.mjs:26:1
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
generatedMessage: false,
code: 'ERR_ASSERTION',
actual: false,
expected: true,
operator: '=='
}
Node.js v20.17.0
How often does it reproduce? Is there a required condition?
Always
What is the expected behavior? Why is that the expected behavior?
When a child process is killed with .kill(), it does not generate the CPU profile that --cpu-prof argument instructs it to do. I would expect profile to be generated.
This is inconsistent with node:worker_threads where terminating a Worker with .terminate() does still generate the profile. It also makes it difficult to debug slow child processes as you cannot get profile info without waiting for graceful exit.
What do you see instead?
Child process is killed and CPU profile is not written.
Additional information
No response
This issue has been marked as stale due to 210 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.
Update to prevent being stale.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先,在报告的 Node.js 版本上运行 repro.mjs,并使用 node:child_process.fork 和 node:worker_threads 对比 fork 出的子进程与 Worker 情况。跟踪 subprocess.kill() 之后 --cpu-prof 如何处理关闭,然后为该复现添加或更新覆盖测试。完成的标准是:被 kill 的 fork 子进程能够像已终止的 Worker 一样,始终写入 CPU profile。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- javascript, node.js
- 领域
- backend
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 48/100