Skip to content

Commit bbf3c10

Browse files
ztannerwyattjoh
authored andcommitted
ensure webpack worker exits bubble to parent process (#72921)
This matches the handling added in #70997 but for the Webpack build worker. This unifies the worker code to be the same as the one for the static export worker which already handles this case. When the worker exits ungracefully, we need to propagate an error signal to the parent process so that it doesn't hang. Fixes #67097 Closes NDX-485 Closes #67154
1 parent 12d4c38 commit bbf3c10

File tree

1 file changed

+14
-34
lines changed
  • packages/next/src/build/webpack-build

1 file changed

+14
-34
lines changed

packages/next/src/build/webpack-build/index.ts

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import type { COMPILER_INDEXES } from '../../shared/lib/constants'
22
import * as Log from '../output/log'
33
import { NextBuildContext } from '../build-context'
44
import type { BuildTraceContext } from '../webpack/plugins/next-trace-entrypoints-plugin'
5-
import { Worker } from 'next/dist/compiled/jest-worker'
5+
import { Worker } from '../../lib/worker'
66
import origDebug from 'next/dist/compiled/debug'
7-
import type { ChildProcess } from 'child_process'
87
import path from 'path'
98
import { exportTraceState, recordTraceEvents } from '../../trace'
109

@@ -38,44 +37,24 @@ async function webpackBuildWithWorker(
3837

3938
prunedBuildContext.pluginState = pluginState
4039

41-
const getWorker = (compilerName: string) => {
42-
const _worker = new Worker(path.join(__dirname, 'impl.js'), {
43-
exposedMethods: ['workerMain'],
44-
numWorkers: 1,
45-
maxRetries: 0,
46-
forkOptions: {
47-
env: {
48-
...process.env,
49-
NEXT_PRIVATE_BUILD_WORKER: '1',
50-
},
40+
const worker = new Worker(path.join(__dirname, 'impl.js'), {
41+
exposedMethods: ['workerMain'],
42+
numWorkers: 1,
43+
maxRetries: 0,
44+
forkOptions: {
45+
env: {
46+
...process.env,
47+
NEXT_PRIVATE_BUILD_WORKER: '1',
5148
},
52-
}) as Worker & typeof import('./impl')
53-
_worker.getStderr().pipe(process.stderr)
54-
_worker.getStdout().pipe(process.stdout)
55-
56-
for (const worker of ((_worker as any)._workerPool?._workers || []) as {
57-
_child: ChildProcess
58-
}[]) {
59-
worker._child.on('exit', (code, signal) => {
60-
if (code || (signal && signal !== 'SIGINT')) {
61-
debug(
62-
`Compiler ${compilerName} unexpectedly exited with code: ${code} and signal: ${signal}`
63-
)
64-
}
65-
})
66-
}
67-
68-
return _worker
69-
}
49+
},
50+
}) as Worker & typeof import('./impl')
7051

7152
const combinedResult = {
7253
duration: 0,
7354
buildTraceContext: {} as BuildTraceContext,
7455
}
7556

7657
for (const compilerName of compilerNames) {
77-
const worker = getWorker(compilerName)
78-
7958
const curResult = await worker.workerMain({
8059
buildContext: prunedBuildContext,
8160
compilerName,
@@ -88,8 +67,6 @@ async function webpackBuildWithWorker(
8867
if (nextBuildSpan && curResult.debugTraceEvents) {
8968
recordTraceEvents(curResult.debugTraceEvents)
9069
}
91-
// destroy worker so it's not sticking around using memory
92-
await worker.end()
9370

9471
// Update plugin state
9572
pluginState = deepMerge(pluginState, curResult.pluginState)
@@ -125,6 +102,9 @@ async function webpackBuildWithWorker(
125102
}
126103
}
127104

105+
// destroy worker so it's not sticking around using memory
106+
worker.end()
107+
128108
if (compilerNames.length === 3) {
129109
Log.event('Compiled successfully')
130110
}

0 commit comments

Comments
 (0)