fix(core): handle process killing more robustly (#31131)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

Cleanup is happening on SIGTERM, SIGINT, and SIGHUP signals and on
exit....

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

Cleanup is mostly done on exit.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
This commit is contained in:
Jason Jean 2025-05-08 15:25:33 -04:00 committed by GitHub
parent b6dc0d9e8e
commit aea60e1a5e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 28 deletions

View File

@ -83,17 +83,3 @@ async function ptyProcess(
});
});
}
// TODO: This only works because pseudo terminal registers signal handlers first but we need a service to handle this
process.on('SIGHUP', () => {
cp.kill('SIGHUP');
process.exit(signalToCode('SIGHUP'));
});
process.on('SIGTERM', () => {
cp.kill('SIGTERM');
process.exit(signalToCode('SIGTERM'));
});
process.on('SIGINT', () => {
cp.kill('SIGINT');
process.exit(signalToCode('SIGINT'));
});

View File

@ -4,20 +4,12 @@ import { getForkedProcessOsSocketPath } from '../daemon/socket-utils';
import { ChildProcess, IS_WASM, RustPseudoTerminal } from '../native';
import { PseudoIPCServer } from './pseudo-ipc';
import { RunningTask } from './running-tasks/running-task';
import { codeToSignal } from '../utils/exit-codes';
// Register single event listeners for all pseudo-terminal instances
const pseudoTerminalShutdownCallbacks: Array<(s?: NodeJS.Signals) => void> = [];
process.on('SIGINT', () => {
pseudoTerminalShutdownCallbacks.forEach((cb) => cb('SIGINT'));
});
process.on('SIGTERM', () => {
pseudoTerminalShutdownCallbacks.forEach((cb) => cb('SIGTERM'));
});
process.on('SIGHUP', () => {
pseudoTerminalShutdownCallbacks.forEach((cb) => cb('SIGHUP'));
});
process.on('exit', () => {
pseudoTerminalShutdownCallbacks.forEach((cb) => cb());
const pseudoTerminalShutdownCallbacks: Array<(s: number) => void> = [];
process.on('exit', (code) => {
pseudoTerminalShutdownCallbacks.forEach((cb) => cb(code));
});
export function createPseudoTerminal(skipSupportCheck: boolean = false) {
@ -56,10 +48,10 @@ export class PseudoTerminal {
this.initialized = true;
}
shutdown(s?: NodeJS.Signals) {
shutdown(code: number) {
for (const cp of this.childProcesses) {
try {
cp.kill(s);
cp.kill(codeToSignal(code));
} catch {}
}
if (this.initialized) {

View File

@ -14,3 +14,19 @@ export function signalToCode(signal: NodeJS.Signals): number {
return 128;
}
}
/**
* Translates numeric exit codes to NodeJS signals
*/
export function codeToSignal(code: number): NodeJS.Signals {
switch (code) {
case 128 + 1:
return 'SIGHUP';
case 128 + 2:
return 'SIGINT';
case 128 + 15:
return 'SIGTERM';
default:
return 'SIGTERM';
}
}