Repository navigation
Conversation
`ThreadWorker` only listened for `error` events, but `process.exit()` and other clean shutdowns in a worker emit only `exit`. The task stayed pending forever and the dead worker stayed in the pool, so the queue could not make progress. The `child_process` runtime already reports an unexpected exit as an error through `ProcessWorker.onUnexpectedExit`; do the same for threads. A worker that throws emits `error` before `exit`, so the `exit` listener is skipped once an error was reported to avoid handling the same failure twice. `terminate()` flags intentional shutdowns so they are ignored.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
With the default
worker_threadsruntime, a worker that exits during a task (for example by callingprocess.exit(1)) leavespool.run()pending forever, and the dead worker stays in the pool instead of being replaced. Thechild_processruntime already handles this case.Cause
ThreadWorkeronly forwardederror, and Node emitsexit(withouterror) when a worker callsprocess.exit().ProcessWorkeralready turns an unexpected exit into an error viaonUnexpectedExit, so this brings the thread runtime to parity.Fix
ThreadWorkernow listens forexitand reportsWorker exited unexpectedlywhen the exit was neither requested nor preceded by anerrorevent. A worker that throws emitserrorbeforeexit, so theexitlistener is skipped in that case to avoid reporting the same failure twice.terminate()marks intentional shutdowns and removes theexitlistener, likeProcessWorker.terminate().The pool's existing error path then rejects the in-flight task, removes the dead worker, spawns a replacement and drains the queue.
Tests
Added
test/worker-exit.test.ts, asserting that bothworker_threadsandchild_processreject the in-flight task and that a subsequent task succeeds on the replacement worker. It also passes withisolateWorkers: true.Fixes #147.