Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Turbopack] await shutdown before ending pool #75545

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading