Skip to content

Commit d751e3a

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 27f9bf2 commit d751e3a

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
@@ -35,10 +35,7 @@ const onSignal = (workers: Terminable[], signal: string) => {
3535
// worker.terminate() might return a Promise or might be synchronous. This async helper function
3636
// creates a consistent interface.
3737
const terminate = async (worker: Terminable) => worker.terminate()
38-
Promise.all(workers.map(worker => terminate(worker))).then(
39-
() => process.exit(1),
40-
() => process.exit(1),
41-
)
38+
Promise.all(workers.map(worker => terminate(worker).catch(() => {}))).then(() => process.exit(1))
4239
workers.length = 0
4340
}
4441

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)