Unverified Commit 1630dc98 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

Backport: ensure worker exits bubble to parent process (#73433)

Backports:
- https://github.com/vercel/next.js/pull/70997
- https://github.com/vercel/next.js/pull/73138

To avoid introducing the larger worker refactor in #68546, this PR
introduces additional handling needed to special case the "restarting"
flow to not exit the parent process.
parent 16ec7d33
...@@ -186,6 +186,7 @@ import { createProgress } from './progress' ...@@ -186,6 +186,7 @@ import { createProgress } from './progress'
import { traceMemoryUsage } from '../lib/memory/trace' import { traceMemoryUsage } from '../lib/memory/trace'
import { generateEncryptionKeyBase64 } from '../server/app-render/encryption-utils' import { generateEncryptionKeyBase64 } from '../server/app-render/encryption-utils'
import type { DeepReadonly } from '../shared/lib/deep-readonly' import type { DeepReadonly } from '../shared/lib/deep-readonly'
import { getNodeOptionsWithoutInspect } from '../server/lib/utils'
interface ExperimentalBypassForInfo { interface ExperimentalBypassForInfo {
experimentalBypassFor?: RouteHas[] experimentalBypassFor?: RouteHas[]
...@@ -564,7 +565,6 @@ function createStaticWorker( ...@@ -564,7 +565,6 @@ function createStaticWorker(
): StaticWorker { ): StaticWorker {
let infoPrinted = false let infoPrinted = false
const timeout = config.staticPageGenerationTimeout || 0 const timeout = config.staticPageGenerationTimeout || 0
return new Worker(staticWorkerPath, { return new Worker(staticWorkerPath, {
timeout: timeout * 1000, timeout: timeout * 1000,
logger: Log, logger: Log,
...@@ -607,6 +607,11 @@ function createStaticWorker( ...@@ -607,6 +607,11 @@ function createStaticWorker(
? incrementalCacheIpcPort + '' ? incrementalCacheIpcPort + ''
: undefined, : undefined,
__NEXT_INCREMENTAL_CACHE_IPC_KEY: incrementalCacheIpcValidationKey, __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, enableWorkerThreads: config.experimental.workerThreads,
......
...@@ -2,9 +2,8 @@ import type { COMPILER_INDEXES } from '../../shared/lib/constants' ...@@ -2,9 +2,8 @@ import type { COMPILER_INDEXES } from '../../shared/lib/constants'
import * as Log from '../output/log' import * as Log from '../output/log'
import { NextBuildContext } from '../build-context' import { NextBuildContext } from '../build-context'
import type { BuildTraceContext } from '../webpack/plugins/next-trace-entrypoints-plugin' 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 origDebug from 'next/dist/compiled/debug'
import type { ChildProcess } from 'child_process'
import path from 'path' import path from 'path'
import { exportTraceState, recordTraceEvents } from '../../trace' import { exportTraceState, recordTraceEvents } from '../../trace'
...@@ -38,8 +37,13 @@ async function webpackBuildWithWorker( ...@@ -38,8 +37,13 @@ async function webpackBuildWithWorker(
prunedBuildContext.pluginState = pluginState prunedBuildContext.pluginState = pluginState
const getWorker = (compilerName: string) => { const combinedResult = {
const _worker = new Worker(path.join(__dirname, 'impl.js'), { duration: 0,
buildTraceContext: {} as BuildTraceContext,
}
for (const compilerName of compilerNames) {
const worker = new Worker(path.join(__dirname, 'impl.js'), {
exposedMethods: ['workerMain'], exposedMethods: ['workerMain'],
numWorkers: 1, numWorkers: 1,
maxRetries: 0, maxRetries: 0,
...@@ -50,31 +54,6 @@ async function webpackBuildWithWorker( ...@@ -50,31 +54,6 @@ async function webpackBuildWithWorker(
}, },
}, },
}) 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,
buildTraceContext: {} as BuildTraceContext,
}
for (const compilerName of compilerNames) {
const worker = getWorker(compilerName)
const curResult = await worker.workerMain({ const curResult = await worker.workerMain({
buildContext: prunedBuildContext, buildContext: prunedBuildContext,
......
import type { ChildProcess } from 'child_process' import type { ChildProcess } from 'child_process'
import { Worker as JestWorker } from 'next/dist/compiled/jest-worker' import { Worker as JestWorker } from 'next/dist/compiled/jest-worker'
import { getNodeOptionsWithoutInspect } from '../server/lib/utils'
type FarmOptions = ConstructorParameters<typeof JestWorker>[1] type FarmOptions = ConstructorParameters<typeof JestWorker>[1]
const RESTARTED = Symbol('restarted') const RESTARTED = Symbol('restarted')
...@@ -15,6 +15,7 @@ const cleanupWorkers = (worker: JestWorker) => { ...@@ -15,6 +15,7 @@ const cleanupWorkers = (worker: JestWorker) => {
export class Worker { export class Worker {
private _worker: JestWorker | undefined private _worker: JestWorker | undefined
private _restarting: boolean = false
constructor( constructor(
workerPath: string, workerPath: string,
...@@ -42,14 +43,10 @@ export class Worker { ...@@ -42,14 +43,10 @@ export class Worker {
env: { env: {
...((farmOptions.forkOptions?.env || {}) as any), ...((farmOptions.forkOptions?.env || {}) as any),
...process.env, ...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 any,
}, },
}) as JestWorker }) as JestWorker
this._restarting = false
restartPromise = new Promise( restartPromise = new Promise(
(resolve) => (resolveRestartPromise = resolve) (resolve) => (resolveRestartPromise = resolve)
) )
...@@ -73,6 +70,12 @@ export class Worker { ...@@ -73,6 +70,12 @@ export class Worker {
logger.error( logger.error(
`Static worker exited with code: ${code} and signal: ${signal}` `Static worker exited with code: ${code} and signal: ${signal}`
) )
// We're restarting the worker, so we don't want to exit the parent process
if (!this._restarting) {
// if a child process doesn't exit gracefully, we want to bubble up the exit code to the parent process
process.exit(code ?? 1)
}
} }
}) })
} }
...@@ -87,14 +90,17 @@ export class Worker { ...@@ -87,14 +90,17 @@ export class Worker {
const worker = this._worker const worker = this._worker
if (!worker) return if (!worker) return
const resolve = resolveRestartPromise const resolve = resolveRestartPromise
createWorker()
logger.warn( logger.warn(
`Sending SIGTERM signal to static worker due to timeout${ `Sending SIGTERM signal to static worker due to timeout${
timeout ? ` of ${timeout / 1000} seconds` : '' timeout ? ` of ${timeout / 1000} seconds` : ''
}. Subsequent errors may be a result of the worker exiting.` }. Subsequent errors may be a result of the worker exiting.`
) )
this._restarting = true
worker.end().then(() => { worker.end().then(() => {
resolve(RESTARTED) resolve(RESTARTED)
createWorker()
}) })
} }
...@@ -119,6 +125,7 @@ export class Worker { ...@@ -119,6 +125,7 @@ export class Worker {
(this._worker as any)[method](...args), (this._worker as any)[method](...args),
restartPromise, restartPromise,
]) ])
if (result !== RESTARTED) return result if (result !== RESTARTED) return result
if (onRestart) onRestart(method, args, ++attempts) if (onRestart) onRestart(method, args, ++attempts)
} }
......
export default function Page() {
process.kill(process.pid, 'SIGKILL')
return <div>Kaboom</div>
}
export default function Layout({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <div>Hello World</div>
}
...@@ -2,10 +2,14 @@ import { nextBuild } from 'next-test-utils' ...@@ -2,10 +2,14 @@ import { nextBuild } from 'next-test-utils'
describe('worker-restart', () => { describe('worker-restart', () => {
it('should properly exhaust all restart attempts and not fail with any worker errors', async () => { it('should properly exhaust all restart attempts and not fail with any worker errors', async () => {
const { stdout, stderr } = await nextBuild(__dirname, [], { const { stdout, stderr } = await nextBuild(
stdout: true, __dirname + '/fixtures/timeout',
stderr: true, [],
}) {
stdout: true,
stderr: true,
}
)
const output = stdout + stderr const output = stdout + stderr
expect(output).toContain( expect(output).toContain(
...@@ -24,4 +28,20 @@ describe('worker-restart', () => { ...@@ -24,4 +28,20 @@ describe('worker-restart', () => {
'Error: Farm is ended, no more calls can be done to it' 'Error: Farm is ended, no more calls can be done to it'
) )
}) })
it('should fail the build if a worker process is killed', async () => {
const { stdout, stderr } = await nextBuild(
__dirname + '/fixtures/worker-kill',
[],
{
stdout: true,
stderr: true,
}
)
const output = stdout + stderr
expect(output).toContain(
'Static worker exited with code: null and signal: SIGKILL'
)
})
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment