Skip to content

Commit

Permalink
await shutdown before ending pool
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Feb 3, 2025
1 parent 7533f25 commit 5fa05bf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 8 additions & 0 deletions packages/next/src/build/turbopack-build/impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ export async function turbopackBuild(): Promise<{
}
}

let shutdownPromise: Promise<void> | undefined
export async function workerMain(workerData: {
buildContext: typeof NextBuildContext
}): Promise<Awaited<ReturnType<typeof turbopackBuild>>> {
Expand All @@ -330,5 +331,12 @@ export async function workerMain(workerData: {
setGlobal('telemetry', telemetry)

const result = await turbopackBuild()
shutdownPromise = result.shutdownPromise
return result
}

export async function waitForShutdown(): Promise<void> {
if (shutdownPromise) {
await shutdownPromise
}
}
9 changes: 6 additions & 3 deletions packages/next/src/build/turbopack-build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async function turbopackBuildWithWorker() {

try {
const worker = new Worker(path.join(__dirname, 'impl.js'), {
exposedMethods: ['workerMain'],
exposedMethods: ['workerMain', 'waitForShutdown'],
numWorkers: 1,
maxRetries: 0,
forkOptions: {
Expand All @@ -27,8 +27,11 @@ async function turbopackBuildWithWorker() {
buildContext: prunedBuildContext,
})

// destroy worker so it's not sticking around using memory
await worker.end()
// destroy worker when Turbopack has shutdown so it's not sticking around using memory
// We need to wait for shutdown to make sure persistent cache is flushed
result.shutdownPromise = worker.waitForShutdown().then(() => {
worker.end()
})

return result
} catch (err: any) {
Expand Down

0 comments on commit 5fa05bf

Please # to comment.