Commit 36d8d585 authored by Tobias Koppers's avatar Tobias Koppers

Fix five bugs found in code review of worker pool rewrite

- export/worker.ts: fix realConsoleError reset ordering — restore
  console.error *before* clearing isEarlyExiting, not after (the old
  code was overwriting realConsoleError with the silenced no-op)
- type-check.ts: await typeCheckWorker.shutdown() in .then() callback
- next-dev-server.ts: await staticPathsWorker.shutdown() in finally block
- worker/index.ts: hoist aborted flag to instance field (_activityAborted)
  and reset it in setOnActivityAbort() so each new progress spinner gets
  exactly one abort notification across batches
- worker-pool.ts: on unexpected worker exit, drain the task queue to
  already-available workers before spawning new ones to avoid unnecessary
  cold-boot latency
Co-Authored-By: 's avatarClaude <noreply@anthropic.com>
parent ac79faff
......@@ -1190,5 +1190,6 @@
"1189": "Worker module exports a non-function \"setup\": %s",
"1190": "Worker default export is not a function: %s",
"1191": "Worker has no own export named \"%s\"",
"1192": "Worker export \"%s\" is not a function: %s"
"1192": "Worker export \"%s\" is not a function: %s",
"1193": "Worker is shut down, no more calls can be done to it"
}
......@@ -68,7 +68,7 @@ const worker = new Worker(require.resolve('./my-worker'), {
})
const result = await (worker as any).doWork(args)
await worker.end()
await worker.shutdown()
```
### Low-level (WorkerPool class)
......
......@@ -264,8 +264,14 @@ export class Worker {
args: unknown[]
): Promise<unknown> => {
for (let attempt = 0; ; attempt++) {
const currentPool = this._pool
if (!currentPool) {
throw new Error(
'Worker is shut down, no more calls can be done to it'
)
}
try {
return await this._pool!.dispatch(method, args)
return await currentPool.dispatch(method, args)
} catch (error) {
if (error instanceof WorkerExitError) {
if (attempt < maxRetries) {
......
......@@ -117,7 +117,14 @@ export function createMessageHandler(
* Called once during INITIALIZE.
*/
function initialize(workerPath: string, setupArgs: unknown[]): void {
main = require(workerPath)
try {
main = require(workerPath)
} catch (err) {
// Module not found, syntax error, etc. Report back to the parent so it
// doesn't wait for PARENT_MESSAGE_READY that will never arrive.
reportInitializeError(err)
return
}
if (!main.setup) {
transport.send([PARENT_MESSAGE_READY])
......
......@@ -410,6 +410,8 @@ export class WorkerPool {
}
this._allWorkers.clear()
this._availableWorkers.clear()
this._stdout.end()
this._stderr.end()
}
// ---------------------------------------------------------------------------
......@@ -688,6 +690,14 @@ export class WorkerPool {
private _handleSpawnError(worker: PoolWorker, error: Error): void {
this._rejectActiveRequests(worker, error)
// Remove the failed worker from the pool and fix the booting count so
// _canSpawnWorker() doesn't permanently see a phantom worker.
if (worker.state === WorkerState.BOOTING) {
this._bootingCount--
}
this._allWorkers.delete(worker)
this._availableWorkers.delete(worker)
// Also reject all queued tasks — spawn errors typically indicate a
// system-level problem that would affect new workers too.
for (const task of this._taskQueue) {
......
......@@ -57,6 +57,12 @@ const transport: ChildTransport = {
},
disconnect(): void {
port.removeListener('message', listener)
// Unlike child_process (which has process.disconnect() to close the IPC
// channel and allow natural exit), worker_threads stay alive as long as
// the parentPort ref is active. Call process.exit() so the thread
// terminates and the parent's waitForExit() resolves without needing
// the force-kill timeout.
process.exit(0)
},
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment