Unverified Commit 0da5c258 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

[Turbopack] gracefully stop turbo-tasks to allow persisting to complete (#69661)

### What?

Make sure to shutdown turbo-tasks gracefully to allow persisting to
complete.
parent 18d32b09
...@@ -432,6 +432,13 @@ pub async fn project_update( ...@@ -432,6 +432,13 @@ pub async fn project_update(
Ok(()) Ok(())
} }
#[napi(ts_return_type = "{ __napiType: \"Project\" }")]
pub async fn project_shutdown(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
) {
project.turbo_tasks.stop_and_wait().await;
}
#[napi(object)] #[napi(object)]
#[derive(Default)] #[derive(Default)]
struct AppPageNapiRoute { struct AppPageNapiRoute {
......
...@@ -1249,6 +1249,7 @@ export default async function build( ...@@ -1249,6 +1249,7 @@ export default async function build(
async function turbopackBuild(): Promise<{ async function turbopackBuild(): Promise<{
duration: number duration: number
buildTraceContext: undefined buildTraceContext: undefined
shutdownPromise: Promise<void>
}> { }> {
if (!IS_TURBOPACK_BUILD) { if (!IS_TURBOPACK_BUILD) {
throw new Error("next build doesn't support turbopack yet") throw new Error("next build doesn't support turbopack yet")
...@@ -1464,6 +1465,8 @@ export default async function build( ...@@ -1464,6 +1465,8 @@ export default async function build(
} }
} }
const shutdownPromise = project.shutdown()
if (warnings.length > 0) { if (warnings.length > 0) {
Log.warn( Log.warn(
`Turbopack build collected ${warnings.length} warnings:\n${warnings `Turbopack build collected ${warnings.length} warnings:\n${warnings
...@@ -1487,6 +1490,7 @@ export default async function build( ...@@ -1487,6 +1490,7 @@ export default async function build(
return { return {
duration: process.hrtime(startTime)[0], duration: process.hrtime(startTime)[0],
buildTraceContext: undefined, buildTraceContext: undefined,
shutdownPromise,
} }
} }
...@@ -1531,9 +1535,15 @@ export default async function build( ...@@ -1531,9 +1535,15 @@ export default async function build(
}, },
}) })
let shutdownPromise = Promise.resolve()
if (!isGenerateMode) { if (!isGenerateMode) {
if (turboNextBuild) { if (turboNextBuild) {
const { duration: compilerDuration, ...rest } = await turbopackBuild() const {
duration: compilerDuration,
shutdownPromise: p,
...rest
} = await turbopackBuild()
shutdownPromise = p
traceMemoryUsage('Finished build', nextBuildSpan) traceMemoryUsage('Finished build', nextBuildSpan)
buildTraceContext = rest.buildTraceContext buildTraceContext = rest.buildTraceContext
...@@ -3566,6 +3576,8 @@ export default async function build( ...@@ -3566,6 +3576,8 @@ export default async function build(
await nextBuildSpan await nextBuildSpan
.traceChild('telemetry-flush') .traceChild('telemetry-flush')
.traceAsyncFn(() => telemetry.flush()) .traceAsyncFn(() => telemetry.flush())
await shutdownPromise
}) })
} finally { } finally {
// Ensure we wait for lockfile patching if present // Ensure we wait for lockfile patching if present
......
...@@ -679,6 +679,8 @@ export interface Project { ...@@ -679,6 +679,8 @@ export interface Project {
aggregationMs: number aggregationMs: number
): AsyncIterableIterator<TurbopackResult<UpdateMessage>> ): AsyncIterableIterator<TurbopackResult<UpdateMessage>>
shutdown(): Promise<void>
onExit(): Promise<void> onExit(): Promise<void>
} }
...@@ -1100,6 +1102,10 @@ function bindingToApi( ...@@ -1100,6 +1102,10 @@ function bindingToApi(
return subscription return subscription
} }
shutdown(): Promise<void> {
return binding.projectShutdown(this._nativeProject)
}
onExit(): Promise<void> { onExit(): Promise<void> {
return binding.projectOnExit(this._nativeProject) return binding.projectOnExit(this._nativeProject)
} }
......
...@@ -315,6 +315,10 @@ impl TurboTasksApi for VcStorage { ...@@ -315,6 +315,10 @@ impl TurboTasksApi for VcStorage {
) -> std::pin::Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>> { ) -> std::pin::Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>> {
unimplemented!() unimplemented!()
} }
fn stop_and_wait(&self) -> std::pin::Pin<Box<dyn Future<Output = ()> + Send + 'static>> {
Box::pin(async {})
}
} }
impl VcStorage { impl VcStorage {
......
...@@ -97,11 +97,13 @@ where ...@@ -97,11 +97,13 @@ where
println!("Run #1 (without cache)"); println!("Run #1 (without cache)");
let first = run_once(tt.clone(), fut()).await?; let first = run_once(tt.clone(), fut()).await?;
println!("Run #2 (with memory cache, same TurboTasks instance)"); println!("Run #2 (with memory cache, same TurboTasks instance)");
let second = run_once(tt, fut()).await?; let second = run_once(tt.clone(), fut()).await?;
assert_eq!(first, second); assert_eq!(first, second);
tt.stop_and_wait().await;
let tt = registration.create_turbo_tasks(&name, false); let tt = registration.create_turbo_tasks(&name, false);
println!("Run #3 (with persistent cache if available, new TurboTasks instance)"); println!("Run #3 (with persistent cache if available, new TurboTasks instance)");
let third = run_once(tt.clone(), fut()).await?; let third = run_once(tt.clone(), fut()).await?;
tt.stop_and_wait().await;
assert_eq!(first, third); assert_eq!(first, third);
Ok(()) Ok(())
} }
...@@ -191,6 +191,8 @@ pub trait TurboTasksApi: TurboTasksCallApi + Sync + Send { ...@@ -191,6 +191,8 @@ pub trait TurboTasksApi: TurboTasksCallApi + Sync + Send {
&self, &self,
f: Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>>, f: Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>>; ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>>;
fn stop_and_wait(&self) -> Pin<Box<dyn Future<Output = ()> + Send>>;
} }
/// A wrapper around a value that is unused. /// A wrapper around a value that is unused.
...@@ -1415,6 +1417,13 @@ impl<B: Backend + 'static> TurboTasksApi for TurboTasks<B> { ...@@ -1415,6 +1417,13 @@ impl<B: Backend + 'static> TurboTasksApi for TurboTasks<B> {
), ),
)) ))
} }
fn stop_and_wait(&self) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>> {
let this = self.pin();
Box::pin(async move {
this.stop_and_wait().await;
})
}
} }
impl<B: Backend + 'static> TurboTasksBackendApi<B> for TurboTasks<B> { impl<B: Backend + 'static> TurboTasksBackendApi<B> for TurboTasks<B> {
...@@ -1431,6 +1440,7 @@ impl<B: Backend + 'static> TurboTasksBackendApi<B> for TurboTasks<B> { ...@@ -1431,6 +1440,7 @@ impl<B: Backend + 'static> TurboTasksBackendApi<B> for TurboTasks<B> {
this.backend.run_backend_job(id, &*this).await; this.backend.run_backend_job(id, &*this).await;
}) })
} }
#[track_caller] #[track_caller]
fn schedule_backend_foreground_job(&self, id: BackendJobId) { fn schedule_backend_foreground_job(&self, id: BackendJobId) {
self.schedule_foreground_job(move |this| async move { self.schedule_foreground_job(move |this| async move {
......
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