Skip to content

Commit

Permalink
fix(export): make static export work with worker_threads
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Apr 1, 2023
1 parent 7d27895 commit 7e2972f
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions packages/next/src/lib/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class Worker {
timeout?: number
onRestart?: (method: string, args: any[], attempts: number) => void
exposedMethods: ReadonlyArray<string>
enableWorkerThreads?: boolean
}
) {
let { timeout, onRestart, ...farmOptions } = options
Expand Down Expand Up @@ -44,18 +45,29 @@ export class Worker {
(resolve) => (resolveRestartPromise = resolve)
)

for (const worker of ((this._worker as any)._workerPool?._workers ||
[]) as {
_child: ChildProcess
}[]) {
worker._child.on('exit', (code, signal) => {
// log unexpected exit if .end() wasn't called
if ((code || signal) && this._worker) {
console.error(
`Static worker unexpectedly exited with code: ${code} and signal: ${signal}`
)
}
})
/**
* Jest Worker has two worker types, ChildProcessWorker (uses child_process) and NodeThreadWorker (uses worker_threads)
* Next.js uses ChildProcessWorker by default, but it can be switched to NodeThreadWorker with an experimental flag
*
* We only want to handle ChildProcessWorker's orphan process issue, so we access the private property "_child":
* https://github.com/facebook/jest/blob/b38d7d345a81d97d1dc3b68b8458b1837fbf19be/packages/jest-worker/src/workers/ChildProcessWorker.ts
*
* But this property is not available in NodeThreadWorker, so we need to check if we are using ChildProcessWorker
*/
if (!farmOptions.enableWorkerThreads) {
for (const worker of ((this._worker as any)._workerPool?._workers ||
[]) as {
_child?: ChildProcess
}[]) {
worker._child?.on('exit', (code, signal) => {
// log unexpected exit if .end() wasn't called
if ((code || signal) && this._worker) {
console.error(
`Static worker unexpectedly exited with code: ${code} and signal: ${signal}`
)
}
})
}
}

this._worker.getStdout().pipe(process.stdout)
Expand Down

0 comments on commit 7e2972f

Please sign in to comment.