Commit ac79faff 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 4388d62a
......@@ -76,8 +76,8 @@ function verifyAndRunTypeScript(
debugBuildPaths,
rootParams,
})
.then((result) => {
typeCheckWorker?.shutdown()
.then(async (result) => {
await typeCheckWorker?.shutdown()
return result
})
.catch(() => {
......
......@@ -372,9 +372,11 @@ export async function exportPages(
installCodeFrameSupport()
// Reset per-call so the flag doesn't leak across sequential batches.
// Restore console.error first (realConsoleError is still the original captured
// at module load; reassigning it here would overwrite it with the no-op set by
// setEarlyExiting() if a previous batch triggered an early exit).
console.error = realConsoleError
isEarlyExiting = false
realConsoleError = console.error
console.error = realConsoleError // restore in case a previous call silenced it
const {
exportPaths,
......
......@@ -162,6 +162,12 @@ export class Worker {
private _onActivity: (() => void) | undefined
private _onActivityAbort: (() => void) | undefined
/**
* Tracks whether onActivityAbort has already fired since the last
* setOnActivityAbort() call. Reset when the caller re-registers the callback
* so that each new progress spinner gets exactly one abort notification.
*/
private _activityAborted = false
/**
* Bound exit handler registered on `process`. Stored so it can be removed
......@@ -206,11 +212,10 @@ export class Worker {
forkOptions,
})
let aborted = false
const onActivityAbortImpl = () => {
if (!aborted) {
if (!this._activityAborted) {
this._activityAborted = true
this._onActivityAbort?.()
aborted = true
}
}
......@@ -290,6 +295,8 @@ export class Worker {
}
setOnActivityAbort(onActivityAbort: (() => void) | undefined): void {
this._onActivityAbort = onActivityAbort
// Reset the guard so the new callback fires on the next stdout/stderr output.
this._activityAborted = false
}
/** Remove the `process.on('exit')` handler to prevent listener leaks */
......
......@@ -570,9 +570,9 @@ export class WorkerPool {
const error = new Error('Error when calling setup: ' + message[2])
;(error as any).type = message[1]
error.stack = message[3]
// Setup errors affect all in-flight requests on this worker.
// Reject them first, then clear booting so queued tasks can be
// dispatched to this (now available) worker and new workers can spawn.
// setup() threw, but require() already ran — main is set and the worker
// can still handle CALLs. Reject any active requests, then transition to
// RUNNING so queued tasks are dispatched to this (now available) worker.
this._rejectActiveRequests(worker, error)
if (worker.state === WorkerState.BOOTING) {
worker.state = WorkerState.RUNNING
......@@ -668,8 +668,13 @@ export class WorkerPool {
this._allWorkers.delete(worker)
this._availableWorkers.delete(worker)
// A slot freed up — try to spawn replacements for queued tasks
// Offer queued tasks to any already-available workers before spawning new
// ones; this avoids unnecessary cold-boot latency when idle workers exist.
if (!this._ending) {
for (const available of this._availableWorkers) {
if (this._taskQueue.length === 0) break
this._drainQueue(available)
}
this._spawnForQueuedTasks()
}
}
......
......@@ -828,7 +828,7 @@ export default class DevServer extends Server {
return pathsResult
} finally {
// we don't re-use workers so destroy the used one
staticPathsWorker.shutdown()
await staticPathsWorker.shutdown()
}
}
const result = this.staticPathsCache.get(pathname)
......
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