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( ...@@ -76,8 +76,8 @@ function verifyAndRunTypeScript(
debugBuildPaths, debugBuildPaths,
rootParams, rootParams,
}) })
.then((result) => { .then(async (result) => {
typeCheckWorker?.shutdown() await typeCheckWorker?.shutdown()
return result return result
}) })
.catch(() => { .catch(() => {
......
...@@ -372,9 +372,11 @@ export async function exportPages( ...@@ -372,9 +372,11 @@ export async function exportPages(
installCodeFrameSupport() installCodeFrameSupport()
// Reset per-call so the flag doesn't leak across sequential batches. // 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 isEarlyExiting = false
realConsoleError = console.error
console.error = realConsoleError // restore in case a previous call silenced it
const { const {
exportPaths, exportPaths,
......
...@@ -162,6 +162,12 @@ export class Worker { ...@@ -162,6 +162,12 @@ export class Worker {
private _onActivity: (() => void) | undefined private _onActivity: (() => void) | undefined
private _onActivityAbort: (() => 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 * Bound exit handler registered on `process`. Stored so it can be removed
...@@ -206,11 +212,10 @@ export class Worker { ...@@ -206,11 +212,10 @@ export class Worker {
forkOptions, forkOptions,
}) })
let aborted = false
const onActivityAbortImpl = () => { const onActivityAbortImpl = () => {
if (!aborted) { if (!this._activityAborted) {
this._activityAborted = true
this._onActivityAbort?.() this._onActivityAbort?.()
aborted = true
} }
} }
...@@ -290,6 +295,8 @@ export class Worker { ...@@ -290,6 +295,8 @@ export class Worker {
} }
setOnActivityAbort(onActivityAbort: (() => void) | undefined): void { setOnActivityAbort(onActivityAbort: (() => void) | undefined): void {
this._onActivityAbort = onActivityAbort 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 */ /** Remove the `process.on('exit')` handler to prevent listener leaks */
......
...@@ -570,9 +570,9 @@ export class WorkerPool { ...@@ -570,9 +570,9 @@ export class WorkerPool {
const error = new Error('Error when calling setup: ' + message[2]) const error = new Error('Error when calling setup: ' + message[2])
;(error as any).type = message[1] ;(error as any).type = message[1]
error.stack = message[3] error.stack = message[3]
// Setup errors affect all in-flight requests on this worker. // setup() threw, but require() already ran — main is set and the worker
// Reject them first, then clear booting so queued tasks can be // can still handle CALLs. Reject any active requests, then transition to
// dispatched to this (now available) worker and new workers can spawn. // RUNNING so queued tasks are dispatched to this (now available) worker.
this._rejectActiveRequests(worker, error) this._rejectActiveRequests(worker, error)
if (worker.state === WorkerState.BOOTING) { if (worker.state === WorkerState.BOOTING) {
worker.state = WorkerState.RUNNING worker.state = WorkerState.RUNNING
...@@ -668,8 +668,13 @@ export class WorkerPool { ...@@ -668,8 +668,13 @@ export class WorkerPool {
this._allWorkers.delete(worker) this._allWorkers.delete(worker)
this._availableWorkers.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) { if (!this._ending) {
for (const available of this._availableWorkers) {
if (this._taskQueue.length === 0) break
this._drainQueue(available)
}
this._spawnForQueuedTasks() this._spawnForQueuedTasks()
} }
} }
......
...@@ -828,7 +828,7 @@ export default class DevServer extends Server { ...@@ -828,7 +828,7 @@ export default class DevServer extends Server {
return pathsResult return pathsResult
} finally { } finally {
// we don't re-use workers so destroy the used one // we don't re-use workers so destroy the used one
staticPathsWorker.shutdown() await staticPathsWorker.shutdown()
} }
} }
const result = this.staticPathsCache.get(pathname) 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