Skip to content

Commit 8518f69

Browse files
authored
fix(core): make forked-process-task-runner robust to closed processes (#14289)
1 parent a5766a8 commit 8518f69

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

packages/nx/src/tasks-runner/forked-process-task-runner.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export class ForkedProcessTaskRunner {
6060
this.processes.add(p);
6161

6262
p.once('exit', (code, signal) => {
63+
this.processes.delete(p);
6364
if (code === null) code = this.signalToCode(signal);
6465
if (code !== 0) {
6566
const results: BatchResults = {};
@@ -171,6 +172,7 @@ export class ForkedProcessTaskRunner {
171172
});
172173

173174
p.on('exit', (code, signal) => {
175+
this.processes.delete(p);
174176
if (code === null) code = this.signalToCode(signal);
175177
// we didn't print any output as we were running the command
176178
// print all the collected output|
@@ -407,28 +409,36 @@ export class ForkedProcessTaskRunner {
407409
// When the nx process gets a message, it will be sent into the task's process
408410
process.on('message', (message: Serializable) => {
409411
this.processes.forEach((p) => {
410-
p.send(message);
412+
if (p.connected) {
413+
p.send(message);
414+
}
411415
});
412416
});
413417

414418
// Terminate any task processes on exit
415419
process.on('SIGINT', () => {
416420
this.processes.forEach((p) => {
417-
p.kill('SIGTERM');
421+
if (p.connected) {
422+
p.kill('SIGTERM');
423+
}
418424
});
419425
// we exit here because we don't need to write anything to cache.
420426
process.exit();
421427
});
422428
process.on('SIGTERM', () => {
423429
this.processes.forEach((p) => {
424-
p.kill('SIGTERM');
430+
if (p.connected) {
431+
p.kill('SIGTERM');
432+
}
425433
});
426434
// no exit here because we expect child processes to terminate which
427435
// will store results to the cache and will terminate this process
428436
});
429437
process.on('SIGHUP', () => {
430438
this.processes.forEach((p) => {
431-
p.kill('SIGTERM');
439+
if (p.connected) {
440+
p.kill('SIGTERM');
441+
}
432442
});
433443
// no exit here because we expect child processes to terminate which
434444
// will store results to the cache and will terminate this process

0 commit comments

Comments
 (0)