Skip to content

[node-core-library] Handle SIGHUP in SubprocessTerminator - #6235

Open
Guru R Bharadwaj (guru-bharadwaj20) wants to merge 1 commit into
microsoft:mainfrom
guru-bharadwaj20:subprocess-terminator-sighup
Open

Guru R Bharadwaj (guru-bharadwaj20) wants to merge 1 commit into
microsoft:mainfrom
guru-bharadwaj20:subprocess-terminator-sighup

Conversation

@guru-bharadwaj20

Copy link
Copy Markdown

Summary

Fixes #6124

If a tool that uses SubprocessTerminator is running in a terminal and the terminal is closed, the tool gets SIGHUP and exits, but its child processes keep running. On Linux and macOS RECOMMENDED_OPTIONS sets detached: true, so the children are in their own session and never get the hangup, and SubprocessTerminator only listened for SIGINT, SIGTERM and exit.

I added SIGHUP next to SIGINT and SIGTERM in _ensureInitialized() and remove it again in _cleanupChildProcesses().

Details

SIGHUP goes through the same _onTerminateSignal handler: the tracked process trees are killed, then the signal is re-sent to the current process with our listener removed, so the process still exits the way it would on SIGHUP without the listener.

The listener is registered on every platform, like the existing SIGINT/SIGTERM ones. Node supports listening for SIGHUP on Windows (it fires when the console window is closed), but I only tested the behavior on Linux.

No API change. I also updated the @remarks text that lists the hooked events.

How it was tested

  • On Windows, rush build --to @rushstack/node-core-library and rush test --only @rushstack/node-core-library both pass.
  • On Linux (Ubuntu under WSL, Node 22) I installed the published @rushstack/node-core-library@5.25.3 and ran the script from the issue (spawn sleep 1000 with RECOMMENDED_OPTIONS and register it with killProcessTreeOnExit), then sent the parent a signal.
    • With the published 5.25.3, after kill -HUP the parent exits and sleep keeps running. SIGTERM and SIGINT kill it.
    • With the same package but lib-commonjs/SubprocessTerminator.js replaced by the build from this branch, sleep is killed for SIGHUP, SIGTERM and SIGINT, and the parent exits in all three cases.

I added a change file with type: patch.

SubprocessTerminator only hooked SIGINT, SIGTERM and exit. Detached children live in their own session, so when the terminal closes and the parent gets SIGHUP, the children never see the hangup and keep running. Treat SIGHUP like the other two signals.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block the SIGHUP child-process cleanup change.

No open issues 🎉

What changed in this PR

Addresses #6124 by extending SubprocessTerminator to clean up tracked child processes on SIGHUP, preventing detached children from surviving terminal closure.

Changes:

  • Registers and removes a SIGHUP listener alongside SIGINT and SIGTERM, reusing the existing termination handler.
  • Updates the API remarks to list SIGHUP.
  • Adds a patch change entry for @rushstack/node-core-library.
File Description
libraries/​node-core-library/​src/​SubprocessTerminator.ts Adds SIGHUP handling and updates the documented event list.
common/​changes/​@rushstack/​node-core-library/​subprocess-terminator-sighup_2026-11-28-12-00.json Records the fix for a patch release.

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment