Unverified Commit a2814731 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

[Turbopack] await shutdown before ending pool (#75545)

### What?

Make sure to gracefully shutdown the Turbopack process, by waiting for shutdown before killing the process.

This makes sure Persistent Caching will have time to store the cache, before it's killed. That should happen in background while doing SSG. (Maybe we need to change that in future for memory reasons, but it will work for now.)
parent 49b93c6d
...@@ -306,6 +306,7 @@ export async function turbopackBuild(): Promise<{ ...@@ -306,6 +306,7 @@ export async function turbopackBuild(): Promise<{
} }
} }
let shutdownPromise: Promise<void> | undefined
export async function workerMain(workerData: { export async function workerMain(workerData: {
buildContext: typeof NextBuildContext buildContext: typeof NextBuildContext
}): Promise<Awaited<ReturnType<typeof turbopackBuild>>> { }): Promise<Awaited<ReturnType<typeof turbopackBuild>>> {
...@@ -332,5 +333,12 @@ export async function workerMain(workerData: { ...@@ -332,5 +333,12 @@ export async function workerMain(workerData: {
setGlobal('telemetry', telemetry) setGlobal('telemetry', telemetry)
const result = await turbopackBuild() const result = await turbopackBuild()
shutdownPromise = result.shutdownPromise
return result return result
} }
export async function waitForShutdown(): Promise<void> {
if (shutdownPromise) {
await shutdownPromise
}
}
...@@ -11,7 +11,7 @@ async function turbopackBuildWithWorker() { ...@@ -11,7 +11,7 @@ async function turbopackBuildWithWorker() {
try { try {
const worker = new Worker(path.join(__dirname, 'impl.js'), { const worker = new Worker(path.join(__dirname, 'impl.js'), {
exposedMethods: ['workerMain'], exposedMethods: ['workerMain', 'waitForShutdown'],
numWorkers: 1, numWorkers: 1,
maxRetries: 0, maxRetries: 0,
forkOptions: { forkOptions: {
...@@ -27,8 +27,11 @@ async function turbopackBuildWithWorker() { ...@@ -27,8 +27,11 @@ async function turbopackBuildWithWorker() {
buildContext: prunedBuildContext, buildContext: prunedBuildContext,
}) })
// destroy worker so it's not sticking around using memory // destroy worker when Turbopack has shutdown so it's not sticking around using memory
await worker.end() // We need to wait for shutdown to make sure persistent cache is flushed
result.shutdownPromise = worker.waitForShutdown().then(() => {
worker.end()
})
return result return result
} catch (err: any) { } catch (err: any) {
......
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