From 4a1947eafd291ec7beceb40e3147ae9293c1891c Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Mon, 21 Oct 2024 08:55:38 -0700 Subject: [PATCH] Ensure workers are cleaned up always (#71564) This ensures we properly clean up workers even if an error is thrown before our normal `.end()` call is done. This can be verified currently by building `pnpm next test/e2e/app-dir/dynamic-io` which should encounter an error and fail but the workers were previously being kept and using lots of CPU, now they are properly cleaned up. --- packages/next/src/lib/verifyAndLint.ts | 16 ++++++++++++---- packages/next/src/lib/worker.ts | 5 +++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/next/src/lib/verifyAndLint.ts b/packages/next/src/lib/verifyAndLint.ts index ff3e308623203..c56ee8f5d1f69 100644 --- a/packages/next/src/lib/verifyAndLint.ts +++ b/packages/next/src/lib/verifyAndLint.ts @@ -15,8 +15,14 @@ export async function verifyAndLint( enableWorkerThreads: boolean | undefined, telemetry: Telemetry ): Promise { + let lintWorkers: + | (Worker & { + runLintCheck: typeof import('./eslint/runLintCheck').runLintCheck + }) + | undefined + try { - const lintWorkers = new Worker(require.resolve('./eslint/runLintCheck'), { + lintWorkers = new Worker(require.resolve('./eslint/runLintCheck'), { numWorkers: 1, enableWorkerThreads, maxRetries: 0, @@ -37,7 +43,7 @@ export async function verifyAndLint( [] ) - const lintResults = await lintWorkers.runLintCheck(dir, lintDirs, { + const lintResults = await lintWorkers?.runLintCheck(dir, lintDirs, { lintDuringBuild: true, eslintOptions: { cacheLocation, @@ -63,8 +69,6 @@ export async function verifyAndLint( if (lintOutput) { console.log(lintOutput) } - - lintWorkers.end() } catch (err) { if (isError(err)) { if (err.type === 'CompileError' || err instanceof CompileError) { @@ -77,5 +81,9 @@ export async function verifyAndLint( } } throw err + } finally { + try { + lintWorkers?.end() + } catch {} } } diff --git a/packages/next/src/lib/worker.ts b/packages/next/src/lib/worker.ts index 9c834ab26ebb0..6726af61ff079 100644 --- a/packages/next/src/lib/worker.ts +++ b/packages/next/src/lib/worker.ts @@ -38,6 +38,11 @@ export class Worker { this._worker = undefined + // ensure we end workers if they weren't before exit + process.on('exit', () => { + this.close() + }) + const createWorker = () => { // Get the node options without inspect and also remove the // --max-old-space-size flag as it can cause memory issues.