Skip to content

Commit e984795

Browse files
committed
Signal handling: Wait for all workers to terminate
The promise returned from `Promise.all()` immediately rejects if any of the underlying promises reject, even if some of the other promises have not yet settled. Catch each rejection to give the other promises an opportunity to resolve. `Promise.allSettled()` could be used instead, but that's a relatively new function that was added in Node.js v12.9.0.
1 parent ac76cc3 commit e984795

File tree

2 files changed

+2
-4
lines changed

2 files changed

+2
-4
lines changed

src/master/implementation.node.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ const onSignal = (workers: Terminable[], signal: string) => {
3636
// worker.terminate() might return a Promise or might be synchronous. This async helper function
3737
// creates a consistent interface.
3838
const terminate = async (worker: Terminable) => worker.terminate()
39-
Promise.all(workers.map(worker => terminate(worker))).then(
40-
() => process.exit(1),
41-
() => process.exit(1),
42-
)
39+
Promise.all(workers.map(worker => terminate(worker).catch(() => {}))).then(() => process.exit(1))
4340
workers.length = 0
4441
}
4542

tslint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
],
1313
"interface-over-type-literal": false,
1414
"member-ordering": false,
15+
"no-empty": [true, "allow-empty-functions"],
1516
"no-implicit-dependencies": [
1617
true,
1718
["tiny-worker", "worker_threads"]

0 commit comments

Comments
 (0)