From 2e697d4bf781ad5f98fb969c1a54e96d0f40e2d2 Mon Sep 17 00:00:00 2001 From: eps1lon Date: Sun, 24 Nov 2024 14:05:36 +0100 Subject: [PATCH] Revert "ensure webpack worker exits bubble to parent process (#72921)" This reverts commit df2c4a302a03d28239da86338941c5203e5f697c. --- .../next/src/build/webpack-build/index.ts | 48 +++++++++++++------ 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/packages/next/src/build/webpack-build/index.ts b/packages/next/src/build/webpack-build/index.ts index e1e9fc0e1638d..c88c6781d7239 100644 --- a/packages/next/src/build/webpack-build/index.ts +++ b/packages/next/src/build/webpack-build/index.ts @@ -2,8 +2,9 @@ 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 '../../lib/worker' +import { Worker } from 'next/dist/compiled/jest-worker' import origDebug from 'next/dist/compiled/debug' +import type { ChildProcess } from 'child_process' import path from 'path' import { exportTraceState, recordTraceEvents } from '../../trace' @@ -37,17 +38,35 @@ async function webpackBuildWithWorker( prunedBuildContext.pluginState = pluginState - const worker = new Worker(path.join(__dirname, 'impl.js'), { - exposedMethods: ['workerMain'], - numWorkers: 1, - maxRetries: 0, - forkOptions: { - env: { - ...process.env, - NEXT_PRIVATE_BUILD_WORKER: '1', + const getWorker = (compilerName: string) => { + const _worker = new Worker(path.join(__dirname, 'impl.js'), { + exposedMethods: ['workerMain'], + numWorkers: 1, + maxRetries: 0, + forkOptions: { + env: { + ...process.env, + NEXT_PRIVATE_BUILD_WORKER: '1', + }, }, - }, - }) as Worker & typeof import('./impl') + }) 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, @@ -55,6 +74,8 @@ async function webpackBuildWithWorker( } for (const compilerName of compilerNames) { + const worker = getWorker(compilerName) + const curResult = await worker.workerMain({ buildContext: prunedBuildContext, compilerName, @@ -67,6 +88,8 @@ async function webpackBuildWithWorker( if (nextBuildSpan && curResult.debugTraceEvents) { recordTraceEvents(curResult.debugTraceEvents) } + // destroy worker so it's not sticking around using memory + await worker.end() // Update plugin state pluginState = deepMerge(pluginState, curResult.pluginState) @@ -102,9 +125,6 @@ async function webpackBuildWithWorker( } } - // destroy worker so it's not sticking around using memory - worker.end() - if (compilerNames.length === 3) { Log.event('Compiled successfully') }