Skip to content

fix: align utilityProcess kill() with child_process.kill() - #53992

Merged
MarshallOfSound merged 4 commits into
mainfrom
claude/fix/utility-process-kill-double-reap
Sep 17, 2026
Merged

MarshallOfSound merged 4 commits into
mainfrom
claude/fix/utility-process-kill-double-reap

Conversation

@claude

@claude claude Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Requested by John Kleinschmidt · Slack thread

Description of Change

Before: every utilityProcess.kill() on macOS and Linux made the browser process reap the child itself. UtilityProcessWrapper::Kill() sent SIGTERM and handed the process to base::EnsureProcessTerminated, which reaps the child (immediately via kqueue on macOS, after ~2s on Linux) and SIGKILLs it after 2 seconds. content's BrowserChildProcessHostImpl reaps the same child once its mojo pipe drops, so content's own kill(SIGKILL) failed with ESRCH and its waitpid() failed with ECHILD, logging ERROR:base/process/kill_posix.cc:87 Unable to terminate process and kill_posix.cc:33 waitpid(): No child processes for each kill (about 1,200 lines per main CI run, e.g. https://github.com/electron/electron/actions/runs/35042445438). A child that handled SIGTERM to finish its work was force-killed 2 seconds later regardless.

After: kill() only delivers the signal: SIGTERM on POSIX, TerminateProcess on Windows. content reaps the child exactly once, so the ERROR lines are gone and the exit code comes from the real waitpid status (the SIGTERM signal, which HandleTermination already maps to 0). A child that handles SIGTERM exits on its own terms, matching Node's child_process.kill(), which delivers the signal and guarantees nothing more. This is the change @deepak1556 asked for in #53992 (comment).

How: Kill() is base::Process::Open(pid_).Terminate(RESULT_CODE_NORMAL_EXIT, false) and nothing else, on every platform. base::EnsureProcessTerminated (introduced in 489d3ac / #34980) is removed along with the ThreadPool SIGKILL fallback from an earlier revision of this PR, the now-unused includes, and the stale crbug 818244 / zygote comment. The child.kill() docs describe the new contract, the change is recorded with a Behavior Changed entry in docs/breaking-changes.md, and a POSIX spec forks a child whose SIGTERM handler exits with 42 after 2.5 seconds and asserts that kill() returns true and the child exits with 42.

Validation: no Electron build is available in this environment, so the C++ compiles for the first time in CI; clang-format --dry-run -Werror with the repo .clang-format is clean. Lints run locally and passing: lint:js, oxfmt --check on the spec and fixture, markdownlint-cli2, lint:js-in-markdown, lint:docs-relative-links. The new spec was run against the prebuilt 46.0.0-nightly.20260915 (which still has the 2s force-kill) with script/spec-runner.js --electronVersion=... -g kill: the existing kill() test passes and the new test fails there with expected +0 to equal 42, which is the behaviour this PR removes and shows the test discriminates. It cannot pass until CI builds this branch.

Checklist

🤖 Generated with Claude Code

https://claude.ai/code/session_01AFucMLGyVVqSUAKXrqbaUT

Release Notes

Notes: child.kill() on utilityProcess now sends SIGTERM on POSIX like child_process.kill() and no longer force-kills a child that handles the signal.

@claude claude Bot added the semver/patch backwards-compatible bug fixes label Sep 16, 2026
UtilityProcessWrapper::Kill() opened a second base::Process for the
child, sent SIGTERM and then handed it to base::EnsureProcessTerminated,
which on POSIX reaps the child itself (immediately via kqueue on macOS,
after a 2s delay on Linux). content's BrowserChildProcessHostImpl reaps
the same child once its mojo pipe drops, via
base::GetKnownDeadTerminationStatus. Whichever side lost that race
failed: content's kill(SIGKILL) hit ESRCH and its waitpid() hit ECHILD,
logging "ERROR:base/process/kill_posix.cc:87 Unable to terminate
process" and "kill_posix.cc:33 waitpid(): No child processes" (about
1,200 lines per main CI run), and the exit code content reported was
derived from a status that was already gone.

content reaps reliably on its own, so only keep base's helper on
Windows. On POSIX send SIGTERM and post a delayed SIGKILL that fires
only if the wrapper has not yet seen the child terminate, matching the
2s grace period base::EnsureProcessTerminated used. waitpid now reports
the signal, which HandleTermination already maps to exit code 0.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01AFucMLGyVVqSUAKXrqbaUT
@claude
claude Bot force-pushed the claude/fix/utility-process-kill-double-reap branch from ca60bc1 to 5507bf0 Compare September 16, 2026 16:20
@claude
claude Bot marked this pull request as ready for review September 16, 2026 18:22
Review feedback on the previous commit: the delayed SIGKILL was posted to
the UI thread, so it could not run while main-process JavaScript kept
that sequence busy, and a child that ignores SIGTERM could outlive
kill() indefinitely. The base::EnsureProcessTerminated call it replaced
enforced its two-second deadline on a background thread.

Post the fallback to a ThreadPool sequence (MayBlock, BEST_EFFORT)
instead. The task takes only the pid and the child's
base::Process::CreationTime(), read before SIGTERM while the child is
certainly alive, and re-reads the creation time when it fires: once
content has reaped the child the pid yields a null time, and a reused
pid yields a different one, so neither is signalled. A not-yet-reaped
zombie matches and accepts SIGKILL harmlessly. No wrapper state is
touched off the UI thread, so nothing has to be marshalled back, and
the ForceKill member goes away.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01AFucMLGyVVqSUAKXrqbaUT
@claude
claude Bot requested a review from ckerr September 17, 2026 05:26

@deepak1556 deepak1556 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't realize this race when implementing the original kill api , good find!

However, should we take this opportunity to avoid the delayed kill that was accidentally introduced in 489d3ac . The process kill api contract was to align with Node.js child_process.kill whose contract is also delivering the signals to the process not ensuring they terminate. We now have chromium handling the reap on pipe disconnection when the process terminates (this was not known at the time of implementation so I had introduced eager base::EnsureProcessTerminated), for cases where a utility process installs SIGTERM handler so far we have been force killing it unintentionally. Should we preserve that or not ?

process.on('SIGTERM', async () => {
  await flushState();
  // The process decides when—or whether—to exit.
});

@claude

claude Bot commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

Agreed this is the moment to decide the escalation rather than just move it between threads.

What the tree promises today: docs/api/utility-process.md says kill() "uses SIGTERM but will ensure the process is reaped on exit". Nothing promises termination, and no spec exercises a child that handles SIGTERM (the closest is the endless service test, which only asserts exit code 0). Before 489d3ac, Kill(signal) was just uv_kill(pid_, signal); that commit (part of #34980) replaced it with Terminate() plus base::EnsureProcessTerminated, which brought both the eager reap this PR removes and the SIGKILL after 2s (base/process/kill_posix.cc:169, BackgroundReaper::ThreadMain at :145), so the escalation arrived as a side effect of the reap, not as a designed contract.

My recommendation: drop it and match child_process.kill(). Kill() becomes Terminate() plus the existing Windows EnsureProcessTerminated; content reaps on pipe disconnect; a child with a SIGTERM handler decides when, or whether, to exit. That also makes the UI-thread question moot: the ThreadPool task, the creation-time guard and kKillGracePeriod all go away. The behaviour change is for apps whose utility process handles SIGTERM and never exits: kill() no longer ends it, and it lives until the app exits, when content's ChildProcessLauncher terminates it. That is the documented Node behaviour, and arguably what installing a SIGTERM handler asks for. I'd update the kill() docs to say so and relabel semver/patch to semver/minor with a release note, unless you'd rather treat it as a fix of unintended behaviour.

If you'd rather preserve the escalation, the current head (c6f905e) does that off the UI thread per @ckerr. Say which and I'll push it.


Generated by Claude Code

@deepak1556

Copy link
Copy Markdown
Member

Make the recommended change that aligns with child_process.kill(), update docs, release notes, tag as semver/minor so it gets an api reivew and drop the eager kill code.

Kill() now only delivers the signal: base::Process::Terminate() sends
SIGTERM on POSIX and calls TerminateProcess on Windows, and nothing else
happens. The base::EnsureProcessTerminated call introduced in 489d3ac
(#34980) is gone on every platform, and with it the ThreadPool SIGKILL
fallback from the previous commit. EnsureProcessTerminated reaped the
child itself and SIGKILLed it after 2s, which raced content's
BrowserChildProcessHost reap (its kill()/waitpid() then failed with
ESRCH/ECHILD and logged two ERROR lines per kill) and force-killed a
child that handles SIGTERM. Like Node's child_process.kill(), delivery
is now all kill() guarantees; content reaps the child once its mojo
pipe drops.

The docs for child.kill() describe the new contract, with an API
history entry, and a POSIX spec forks a child whose SIGTERM handler
exits with 42 after 2.5s and asserts that kill() lets it.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01AFucMLGyVVqSUAKXrqbaUT
@claude claude Bot added the semver/minor backwards-compatible functionality label Sep 17, 2026
@claude claude Bot changed the title fix: don't reap utilityProcess children in kill() fix: align utilityProcess kill() with child_process.kill() Sep 17, 2026
@claude
claude Bot requested a review from deepak1556 September 17, 2026 06:10
@claude

claude Bot commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

@deepak1556 Done in 76b02e0: Kill() is now Terminate() only on every platform (no EnsureProcessTerminated, no delayed SIGKILL), docs and API history updated, semver/minor, release note in the PR body, plus a POSIX spec that forks a child with a SIGTERM handler exiting with 42 after 2.5s and asserts kill() lets it. @ckerr: this supersedes the ThreadPool fallback from c6f905e, which is gone.


Generated by Claude Code

@claude claude Bot removed the semver/patch backwards-compatible bug fixes label Sep 17, 2026
@release-clerk

release-clerk Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

Release note looks good now.

@deepak1556 deepak1556 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with minor nits

Comment thread docs/api/utility-process.md Resolved
Comment thread docs/api/utility-process.md Resolved
Apply review nits: drop the API-history block under `child.kill()`,
note the behavior change under Breaking API Changes (46.0) in
docs/breaking-changes.md instead, and say that the child is reaped by
Electron on POSIX.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01AFucMLGyVVqSUAKXrqbaUT
@claude
claude Bot requested a review from a team as a code owner September 17, 2026 11:11
@claude
claude Bot requested a review from deepak1556 September 17, 2026 11:12

@jkleinsc jkleinsc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API LGTM

@MarshallOfSound MarshallOfSound added semver/major incompatible API changes no-backport and removed semver/minor backwards-compatible functionality labels Sep 17, 2026
@MarshallOfSound

Copy link
Copy Markdown
Member

API LGTM

@MarshallOfSound
MarshallOfSound merged commit c7e11ac into main Sep 17, 2026
94 of 95 checks passed
@release-clerk

release-clerk Bot commented Sep 17, 2026

Copy link
Copy Markdown

Release Notes Persisted

child.kill() on utilityProcess now sends SIGTERM on POSIX like child_process.kill() and no longer force-kills a child that handles the signal.

@MarshallOfSound
MarshallOfSound deleted the claude/fix/utility-process-kill-double-reap branch September 17, 2026 22:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

5 participants