Unverified Commit 4a1947ea authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

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.
parent e406def6
......@@ -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,
......@@ -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 {}
}
}
......@@ -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.
......
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