Unverified Commit 23a1f0cb authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: ensure tests are dropping turbo tasks correctly (#99601)

### What?

Let the unit tests check if we cleanup correctly: after
`stop_and_wait()`, the test harness checks that it holds the only
reference to the `TurboTasks` instance, then drops it.

Recreates #85612, rebased on latest `canary`.

### Details

- `stop_and_wait()` returns once all jobs have signaled completion, but
the spawned futures that ran them (e.g. priority-runner workers,
background jobs) may still be dropping their `Arc<TurboTasks>` clones at
that point. An immediate `Arc::strong_count == 1` assertion therefore
fails nondeterministically (33 of 1166 tests locally), even though the
references are released shortly after.
- Instead of adding tracking overhead to the manager, the harness polls
(1 ms sleeps) until `Arc::strong_count` reaches 1, and panics with the
remaining reference count after 10 s. Real leaks are still caught.
- Adapted to `TestInstance`: the `snapshot_and_evict` closure (which
holds its own reference) is dropped before the check.

### Verification

- `cargo nextest run` for turbo-tasks-backend, turbo-tasks-fetch,
turbo-tasks-fs, turbo-tasks-macros-tests, turbopack-analyze,
turbopack-core, turbopack-ecmascript, turbopack-node,
turbo-tasks-testing: 1166 passed.
- turbo-tasks-backend + turbo-tasks-fetch repeated 3×: all passing.

<!-- NEXT_JS_LLM -->


<!-- fleet 9f22b386-e1c1-48c7-b471-5ec406e064fe -->

---------
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 8c96a740
...@@ -93,6 +93,26 @@ where ...@@ -93,6 +93,26 @@ where
.unwrap() .unwrap()
} }
/// Asserts that `tt` is the last reference to the TurboTasks instance, i.e. that nothing leaked a
/// reference to it.
///
/// `stop_and_wait` returns once all jobs have signaled completion, but the spawned futures that
/// ran them may still be in the process of dropping their own references. Poll for a while before
/// failing.
async fn wait_for_sole_reference(tt: &Arc<dyn TurboTasksApi>) {
const TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);
let start = std::time::Instant::now();
while Arc::strong_count(tt) != 1 {
assert!(
start.elapsed() < TIMEOUT,
"TurboTasks instance is still referenced {} more time(s) {TIMEOUT:?} after \
stop_and_wait",
Arc::strong_count(tt) - 1
);
tokio::time::sleep(std::time::Duration::from_millis(1)).await;
}
}
fn closure_to_name<T>(value: &T) -> String { fn closure_to_name<T>(value: &T) -> String {
let name = std::any::type_name_of_val(value); let name = std::any::type_name_of_val(value);
name.replace("::{{closure}}", "").replace("::", "_") name.replace("::{{closure}}", "").replace("::", "_")
...@@ -159,6 +179,13 @@ where ...@@ -159,6 +179,13 @@ where
let start = std::time::Instant::now(); let start = std::time::Instant::now();
instance.tt.stop_and_wait().await; instance.tt.stop_and_wait().await;
println!("Stopping TurboTasks took {:?}", start.elapsed()); println!("Stopping TurboTasks took {:?}", start.elapsed());
// The eviction closure holds its own reference to the TurboTasks instance.
drop(instance.snapshot_and_evict);
wait_for_sole_reference(&instance.tt).await;
let start = std::time::Instant::now();
drop(instance.tt);
println!("Dropping TurboTasks took {:?}", start.elapsed());
if !single_run { if !single_run {
for _ in 10..20 { for _ in 10..20 {
let instance = registration.create_turbo_tasks(&name, false); let instance = registration.create_turbo_tasks(&name, false);
...@@ -170,6 +197,11 @@ where ...@@ -170,6 +197,11 @@ where
let start = std::time::Instant::now(); let start = std::time::Instant::now();
instance.tt.stop_and_wait().await; instance.tt.stop_and_wait().await;
println!("Stopping TurboTasks took {:?}", start.elapsed()); println!("Stopping TurboTasks took {:?}", start.elapsed());
drop(instance.snapshot_and_evict);
wait_for_sole_reference(&instance.tt).await;
let start = std::time::Instant::now();
drop(instance.tt);
println!("Dropping TurboTasks took {:?}", start.elapsed());
assert_eq!(first, third); assert_eq!(first, third);
} }
} }
......
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