fix(core): explicitly cleanup forked process task runner (#31106)

<!-- 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 -->

Forked process task runner cleanup is not explicitly invoked.

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

Forked process task runner cleanup is explicitly invoked.

## 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-07 11:29:35 -04:00 committed by GitHub
parent 3b19cf6811
commit 480a20e3c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 13 deletions

View File

@ -359,7 +359,7 @@ class RunningNodeProcess implements RunningTask {
kill(signal?: NodeJS.Signals): Promise<void> {
return new Promise<void>((res, rej) => {
if (process.platform === 'win32' || process.platform === 'darwin') {
if (process.platform === 'win32') {
if (this.childProcess.kill(signal)) {
res();
} else {

View File

@ -367,6 +367,13 @@ export class ForkedProcessTaskRunner {
writeFileSync(outputPath, content);
}
cleanup(signal?: NodeJS.Signals) {
this.processes.forEach((p) => {
p.kill(signal);
});
this.cleanUpBatchProcesses();
}
private setupProcessEventListeners() {
const messageHandler = (message: Serializable) => {
this.pseudoTerminals.forEach((p) => {
@ -383,30 +390,26 @@ export class ForkedProcessTaskRunner {
// When the nx process gets a message, it will be sent into the task's process
process.on('message', messageHandler);
const cleanUp = (signal?: NodeJS.Signals) => {
this.processes.forEach((p) => {
p.kill(signal);
});
process.off('message', messageHandler);
this.cleanUpBatchProcesses();
};
// Terminate any task processes on exit
process.once('exit', () => {
cleanUp();
this.cleanup();
process.off('message', messageHandler);
});
process.once('SIGINT', () => {
cleanUp('SIGTERM');
this.cleanup('SIGTERM');
process.off('message', messageHandler);
// we exit here because we don't need to write anything to cache.
process.exit(signalToCode('SIGINT'));
});
process.once('SIGTERM', () => {
cleanUp('SIGTERM');
this.cleanup('SIGTERM');
process.off('message', messageHandler);
// no exit here because we expect child processes to terminate which
// will store results to the cache and will terminate this process
});
process.once('SIGHUP', () => {
cleanUp('SIGTERM');
this.cleanup('SIGTERM');
process.off('message', messageHandler);
// no exit here because we expect child processes to terminate which
// will store results to the cache and will terminate this process
});

View File

@ -1004,6 +1004,7 @@ export class TaskOrchestrator {
// endregion utils
private async cleanup() {
this.forkedProcessTaskRunner.cleanup();
await Promise.all([
...Array.from(this.runningContinuousTasks).map(async ([taskId, t]) => {
try {