Skip to content

Commit

Permalink
Ensure workers are cleaned up always (#71564)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ijjk authored Oct 21, 2024
1 parent e406def commit 4a1947e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
16 changes: 12 additions & 4 deletions packages/next/src/lib/verifyAndLint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ export async function verifyAndLint(
enableWorkerThreads: boolean | undefined,
telemetry: Telemetry
): Promise<void> {
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,
Expand All @@ -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,
Expand All @@ -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) {
Expand All @@ -77,5 +81,9 @@ export async function verifyAndLint(
}
}
throw err
} finally {
try {
lintWorkers?.end()
} catch {}
}
}
5 changes: 5 additions & 0 deletions packages/next/src/lib/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 4a1947e

Please sign in to comment.