diff --git a/packages/nx/src/executors/run-commands/running-tasks.ts b/packages/nx/src/executors/run-commands/running-tasks.ts index 58288487e6..b3420c0e90 100644 --- a/packages/nx/src/executors/run-commands/running-tasks.ts +++ b/packages/nx/src/executors/run-commands/running-tasks.ts @@ -359,7 +359,7 @@ class RunningNodeProcess implements RunningTask { kill(signal?: NodeJS.Signals): Promise { return new Promise((res, rej) => { - if (process.platform === 'win32' || process.platform === 'darwin') { + if (process.platform === 'win32') { if (this.childProcess.kill(signal)) { res(); } else { diff --git a/packages/nx/src/tasks-runner/forked-process-task-runner.ts b/packages/nx/src/tasks-runner/forked-process-task-runner.ts index 1ebf190a4b..2a4be28ada 100644 --- a/packages/nx/src/tasks-runner/forked-process-task-runner.ts +++ b/packages/nx/src/tasks-runner/forked-process-task-runner.ts @@ -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 }); diff --git a/packages/nx/src/tasks-runner/task-orchestrator.ts b/packages/nx/src/tasks-runner/task-orchestrator.ts index 3b6b1fe49e..73257cc5b8 100644 --- a/packages/nx/src/tasks-runner/task-orchestrator.ts +++ b/packages/nx/src/tasks-runner/task-orchestrator.ts @@ -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 {