nodejs / nodejs/node

`node:child_process.fork` does not generate cpu-prof when process is killed

オープン
#55,094 コメント 6 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

child_process
主要言語
JavaScript
スター
122k
フォーク
37.3k
平均マージ
4日 2時間
マージ済み PR(30日)
283

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、報告された Node.js バージョンで repro.mjs を実行し、node:child_process.fork と node:worker_threads を使って fork された子プロセスと Worker のケースを比較します。subprocess.kill() 後のシャットダウンを --cpu-prof がどのように処理するかを追跡し、その後、再現ケースのカバレッジを追加または更新します。終了条件は、kill された fork 子プロセスが、終了した Worker と同様に一貫して CPU プロファイルを書き込むことです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, node.js
領域
backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
48/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。