From 82d87cb7ce8a652a7aba3c7a9ddd1de61b5919ca Mon Sep 17 00:00:00 2001 From: Zack Tanner <1939140+ztanner@users.noreply.github.com> Date: Sun, 24 Nov 2024 09:19:00 -0800 Subject: [PATCH] Backport "ensure webpack worker exits bubble to parent process (#72921)" (#73138) --- packages/next/src/build/index.ts | 7 ++- .../next/src/build/webpack-build/index.ts | 44 +++++++------------ packages/next/src/lib/worker.ts | 7 +-- 3 files changed, 22 insertions(+), 36 deletions(-) diff --git a/packages/next/src/build/index.ts b/packages/next/src/build/index.ts index 4ac2f7881a82e3..3bbd8b4c7a1fc4 100644 --- a/packages/next/src/build/index.ts +++ b/packages/next/src/build/index.ts @@ -186,6 +186,7 @@ import { createProgress } from './progress' import { traceMemoryUsage } from '../lib/memory/trace' import { generateEncryptionKeyBase64 } from '../server/app-render/encryption-utils' import type { DeepReadonly } from '../shared/lib/deep-readonly' +import { getNodeOptionsWithoutInspect } from '../server/lib/utils' interface ExperimentalBypassForInfo { experimentalBypassFor?: RouteHas[] @@ -564,7 +565,6 @@ function createStaticWorker( ): StaticWorker { let infoPrinted = false const timeout = config.staticPageGenerationTimeout || 0 - return new Worker(staticWorkerPath, { timeout: timeout * 1000, logger: Log, @@ -607,6 +607,11 @@ function createStaticWorker( ? incrementalCacheIpcPort + '' : undefined, __NEXT_INCREMENTAL_CACHE_IPC_KEY: incrementalCacheIpcValidationKey, + // we don't pass down NODE_OPTIONS as it can + // extra memory usage + NODE_OPTIONS: getNodeOptionsWithoutInspect() + .replace(/--max-old-space-size=[\d]{1,}/, '') + .trim(), }, }, enableWorkerThreads: config.experimental.workerThreads, diff --git a/packages/next/src/build/webpack-build/index.ts b/packages/next/src/build/webpack-build/index.ts index 5200e65da7aeec..112ec335e44785 100644 --- a/packages/next/src/build/webpack-build/index.ts +++ b/packages/next/src/build/webpack-build/index.ts @@ -2,11 +2,14 @@ import type { COMPILER_INDEXES } from '../../shared/lib/constants' import * as Log from '../output/log' import { NextBuildContext } from '../build-context' import type { BuildTraceContext } from '../webpack/plugins/next-trace-entrypoints-plugin' -import { Worker } from 'next/dist/compiled/jest-worker' +import { Worker } from '../../lib/worker' import origDebug from 'next/dist/compiled/debug' -import type { ChildProcess } from 'child_process' import path from 'path' import { exportTraceState, recordTraceEvents } from '../../trace' +import { + formatNodeOptions, + getParsedNodeOptionsWithoutInspect, +} from '../../server/lib/utils' const debug = origDebug('next:build:webpack-build') @@ -38,8 +41,15 @@ async function webpackBuildWithWorker( prunedBuildContext.pluginState = pluginState - const getWorker = (compilerName: string) => { - const _worker = new Worker(path.join(__dirname, 'impl.js'), { + const combinedResult = { + duration: 0, + buildTraceContext: {} as BuildTraceContext, + } + + const nodeOptions = getParsedNodeOptionsWithoutInspect() + + for (const compilerName of compilerNames) { + const worker = new Worker(path.join(__dirname, 'impl.js'), { exposedMethods: ['workerMain'], numWorkers: 1, maxRetries: 0, @@ -47,34 +57,10 @@ async function webpackBuildWithWorker( env: { ...process.env, NEXT_PRIVATE_BUILD_WORKER: '1', + NODE_OPTIONS: formatNodeOptions(nodeOptions), }, }, }) as Worker & typeof import('./impl') - _worker.getStderr().pipe(process.stderr) - _worker.getStdout().pipe(process.stdout) - - for (const worker of ((_worker as any)._workerPool?._workers || []) as { - _child: ChildProcess - }[]) { - worker._child.on('exit', (code, signal) => { - if (code || (signal && signal !== 'SIGINT')) { - debug( - `Compiler ${compilerName} unexpectedly exited with code: ${code} and signal: ${signal}` - ) - } - }) - } - - return _worker - } - - const combinedResult = { - duration: 0, - buildTraceContext: {} as BuildTraceContext, - } - - for (const compilerName of compilerNames) { - const worker = getWorker(compilerName) const curResult = await worker.workerMain({ buildContext: prunedBuildContext, diff --git a/packages/next/src/lib/worker.ts b/packages/next/src/lib/worker.ts index 13d654b7b65c66..6555c83c026e05 100644 --- a/packages/next/src/lib/worker.ts +++ b/packages/next/src/lib/worker.ts @@ -1,6 +1,6 @@ import type { ChildProcess } from 'child_process' import { Worker as JestWorker } from 'next/dist/compiled/jest-worker' -import { getNodeOptionsWithoutInspect } from '../server/lib/utils' + type FarmOptions = ConstructorParameters[1] const RESTARTED = Symbol('restarted') @@ -42,11 +42,6 @@ export class Worker { env: { ...((farmOptions.forkOptions?.env || {}) as any), ...process.env, - // we don't pass down NODE_OPTIONS as it can - // extra memory usage - NODE_OPTIONS: getNodeOptionsWithoutInspect() - .replace(/--max-old-space-size=[\d]{1,}/, '') - .trim(), } as any, }, }) as JestWorker