Skip to content

Commit 7c0429c

Browse files
ztannerwyattjoh
authored andcommitted
Reapply "ensure webpack worker exits bubble to parent process (#72921)" (#73138)
This relands #72921 to be a more minimal refactor. This re-applies the core change to use the existing worker that has proper error bubbling handling without changing the worker lifecycle. For the purposes of the internal OOM we were seeing, this ensures that any custom `max-old-space-size` flags are preserved during the webpack build step, even when using the shared worker. Instead, I moved it to `createStaticWorker`, as that was where it was intended to be respected when it landed in #46705.
1 parent 3d99330 commit 7c0429c

File tree

3 files changed

+26
-41
lines changed

3 files changed

+26
-41
lines changed

packages/next/src/build/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ import { inlineStaticEnv } from './flying-shuttle/inline-static-env'
216216
import { FallbackMode, fallbackModeToFallbackField } from '../lib/fallback'
217217
import { RenderingMode } from './rendering-mode'
218218
import { getParamKeys } from '../server/request/fallback-params'
219+
import {
220+
formatNodeOptions,
221+
getParsedNodeOptionsWithoutInspect,
222+
} from '../server/lib/utils'
219223

220224
type Fallback = null | boolean | string
221225

@@ -684,6 +688,12 @@ export function createStaticWorker(
684688
clear: () => void
685689
}
686690
): StaticWorker {
691+
// Get the node options without inspect and also remove the
692+
// --max-old-space-size flag as it can cause memory issues.
693+
const nodeOptions = getParsedNodeOptionsWithoutInspect()
694+
delete nodeOptions['max-old-space-size']
695+
delete nodeOptions['max_old_space_size']
696+
687697
return new Worker(staticWorkerPath, {
688698
logger: Log,
689699
numWorkers: getNumberOfWorkers(config),
@@ -694,7 +704,7 @@ export function createStaticWorker(
694704
progress?.clear()
695705
},
696706
forkOptions: {
697-
env: process.env,
707+
env: { ...process.env, NODE_OPTIONS: formatNodeOptions(nodeOptions) },
698708
},
699709
enableWorkerThreads: config.experimental.workerThreads,
700710
exposedMethods: staticWorkerExposedMethods,

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

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ 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'
9+
import {
10+
formatNodeOptions,
11+
getParsedNodeOptionsWithoutInspect,
12+
} from '../../server/lib/utils'
1013

1114
const debug = origDebug('next:build:webpack-build')
1215

@@ -38,43 +41,26 @@ async function webpackBuildWithWorker(
3841

3942
prunedBuildContext.pluginState = pluginState
4043

41-
const getWorker = (compilerName: string) => {
42-
const _worker = new Worker(path.join(__dirname, 'impl.js'), {
44+
const combinedResult = {
45+
duration: 0,
46+
buildTraceContext: {} as BuildTraceContext,
47+
}
48+
49+
const nodeOptions = getParsedNodeOptionsWithoutInspect()
50+
51+
for (const compilerName of compilerNames) {
52+
const worker = new Worker(path.join(__dirname, 'impl.js'), {
4353
exposedMethods: ['workerMain'],
4454
numWorkers: 1,
4555
maxRetries: 0,
4656
forkOptions: {
4757
env: {
4858
...process.env,
4959
NEXT_PRIVATE_BUILD_WORKER: '1',
60+
NODE_OPTIONS: formatNodeOptions(nodeOptions),
5061
},
5162
},
5263
}) 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-
}
70-
71-
const combinedResult = {
72-
duration: 0,
73-
buildTraceContext: {} as BuildTraceContext,
74-
}
75-
76-
for (const compilerName of compilerNames) {
77-
const worker = getWorker(compilerName)
7864

7965
const curResult = await worker.workerMain({
8066
buildContext: prunedBuildContext,

packages/next/src/lib/worker.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import type { ChildProcess } from 'child_process'
22
import { Worker as JestWorker } from 'next/dist/compiled/jest-worker'
3-
import {
4-
getParsedNodeOptionsWithoutInspect,
5-
formatNodeOptions,
6-
} from '../server/lib/utils'
73
import { Transform } from 'stream'
84

95
type FarmOptions = ConstructorParameters<typeof JestWorker>[1]
@@ -47,20 +43,13 @@ export class Worker {
4743
})
4844

4945
const createWorker = () => {
50-
// Get the node options without inspect and also remove the
51-
// --max-old-space-size flag as it can cause memory issues.
52-
const nodeOptions = getParsedNodeOptionsWithoutInspect()
53-
delete nodeOptions['max-old-space-size']
54-
delete nodeOptions['max_old_space_size']
55-
5646
this._worker = new JestWorker(workerPath, {
5747
...farmOptions,
5848
forkOptions: {
5949
...farmOptions.forkOptions,
6050
env: {
6151
...((farmOptions.forkOptions?.env || {}) as any),
6252
...process.env,
63-
NODE_OPTIONS: formatNodeOptions(nodeOptions),
6453
} as any,
6554
},
6655
maxRetries: 0,

0 commit comments

Comments
 (0)