runtime: fix double increment of num_idle_threads on shutdown (#7910)

This commit is contained in:
Varun Chawla
2026-02-16 23:19:50 -08:00
committed by GitHub
parent 96f64f4ee2
commit d83921bc51
2 changed files with 32 additions and 6 deletions
-6
View File
@@ -561,12 +561,6 @@ impl Inner {
shared = self.shared.lock();
}
// Work was produced, and we "took" it (by decrementing num_notify).
// This means that num_idle was decremented once for our wakeup.
// But, since we are exiting, we need to "undo" that, as we'll stay idle.
self.metrics.inc_num_idle_threads();
// NOTE: Technically we should also do num_notify++ and notify again,
// but since we're shutting down anyway, that won't be necessary.
break;
}
}
+32
View File
@@ -62,6 +62,38 @@ fn num_idle_blocking_threads() {
assert_eq!(1, rt.metrics().num_idle_blocking_threads());
}
#[test]
fn num_idle_blocking_threads_is_zero_after_shutdown() {
let rt = current_thread();
let handle = rt.handle().clone();
// Spawn a blocking task to create a worker thread.
let _ = rt.block_on(rt.spawn_blocking(move || {}));
// Wait for the thread to become idle.
rt.block_on(async {
time::sleep(Duration::from_millis(5)).await;
});
if handle.metrics().num_idle_blocking_threads() == 0 {
rt.block_on(async {
time::sleep(Duration::from_secs(1)).await;
});
}
assert_eq!(1, handle.metrics().num_idle_blocking_threads());
// Drop the runtime, which triggers shutdown and joins all blocking
// threads. Before the fix for #6439, the shutdown path incremented
// num_idle_threads a second time for each idle worker, so this
// counter stayed at 1 instead of going back to 0.
drop(rt);
assert_eq!(
0,
handle.metrics().num_idle_blocking_threads(),
"num_idle_blocking_threads should be 0 after shutdown (see #6439)"
);
}
#[test]
fn blocking_queue_depth() {
let rt = tokio::runtime::Builder::new_current_thread()