nodejs / nodejs/node

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

Open
#55,094 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

child_process
Dominant language
JavaScript
Stars
122k
Forks
37.3k
Avg merge
4d 2h
Merged PRs (30d)
283

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running repro.mjs on the reported Node.js version and compare the forked child process with the Worker case using node:child_process.fork and node:worker_threads. Trace how --cpu-prof handles shutdown after subprocess.kill(), then add or update coverage for the reproduction. Done means a killed forked child writes a CPU profile consistently with the terminated Worker.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.