Commit 0af3f2b3 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by Niklas Mischkulnig

Fix worker thread stderr drain before process.exit and prettier formatting

When a worker thread exits non-zero, the parent's 'exit' event fires
before the thread's stderr MessageChannel has finished delivering all
buffered output to process.stderr. Waiting for thread.stderr to end
ensures all error output (e.g. console.error(err)) is flushed to the
parent before calling process.exit(), fixing truncated build error output.

Also fix prettier formatting in export/worker.ts from previous commit.
Co-Authored-By: 's avatarClaude <noreply@anthropic.com>
parent 35894d06
......@@ -610,8 +610,9 @@ async function exportPage(
if (process.send) {
process.send([3, { type: 'activity' }])
} else {
(require('worker_threads') as typeof import('worker_threads')).parentPort?.postMessage([3, { type: 'activity' }])
;(
require('worker_threads') as typeof import('worker_threads')
).parentPort?.postMessage([3, { type: 'activity' }])
}
// Otherwise we can return the result.
......
......@@ -204,14 +204,32 @@ export class Worker {
[]) as {
_worker?: WorkerThread
}[]) {
worker._worker?.on('exit', (code) => {
// Capture the current thread handle at registration time.
// After jest-worker restarts a failed thread, worker._worker is
// updated to the new thread, so we must hold the old reference here.
const thread = worker._worker
thread?.on('exit', (code) => {
if (code && code !== 0 && this._worker) {
logger.error(
`Next.js build worker exited with code: ${code} and signal: null`
)
// if a worker thread doesn't exit gracefully, bubble up the exit code to the parent process
process.exit(code ?? 1)
// Worker thread stderr is delivered via a MessageChannel and may
// still be draining when the 'exit' event fires. Wait for the thread's
// own stderr stream to end before calling process.exit(), so all
// buffered output is written to process.stderr first.
const exitCode = code ?? 1
const threadStderr = thread.stderr
if (threadStderr && !threadStderr.destroyed) {
threadStderr.once('end', () => {
process.exit(exitCode)
})
threadStderr.once('error', () => {
process.exit(exitCode)
})
} else {
process.exit(exitCode)
}
}
})
......
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