7922: runtime: fix race condition during the blocking pool shutdown#67
7922: runtime: fix race condition during the blocking pool shutdown#67martin-augment wants to merge 1 commit intomasterfrom
Conversation
Summary of ChangesHello @martin-augment, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a race condition within the Tokio runtime's blocking pool shutdown mechanism. By introducing a precise tracking flag for idle threads, it ensures that the Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
WalkthroughThe changes modify thread idle tracking in the blocking thread pool runtime component. A per-thread ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Code Review
The changes effectively address a race condition during the blocking pool shutdown by introducing an is_counted_idle flag. This flag accurately tracks whether a thread is currently contributing to the num_idle_threads metric, preventing incorrect decrements and potential underflow panics. The logic is sound and directly resolves the issue described in the pull request title, enhancing the robustness of the blocking pool.
🤖 Augment PR SummarySummary: Fixes a race in the blocking pool shutdown path where worker exit could double-decrement the idle-thread metric. 🤖 Was this summary useful? React with 👍 or 👎 |
Code Review: Fix Race Condition During Blocking Pool ShutdownSummaryThis PR fixes a real race condition in the blocking thread pool where Root Cause AnalysisThe race arises from this interleaved sequence:
The concrete panic: Correctness of the FixThe fix is correct. The Key path analysis:
Rust's definite-initialization analysis guarantees Suggestions1. Missing loom test (important) The existing #[test]
fn spawn_task_and_shutdown_race() {
loom::model(|| {
let rt = mk_runtime(1);
{
let _enter = rt.enter();
// Spawn so a thread goes idle, then spawn again to trigger the
// pre-decrement of num_idle_threads racing with shutdown.
let _ = crate::task::spawn_blocking(|| {});
}
// Dropping exercises the spawn_task + shutdown race.
drop(rt);
});
}Loom would exhaustively explore the interleaving where 2. Minor — uninitialized declaration
3. Minor — duplicate comment The comment Overall AssessmentThe fix is minimal, well-targeted, and correct. The main gap is the absence of a loom regression test for this specific race. Given that the blocking pool already has loom tests in |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
tokio/src/runtime/blocking/pool.rs (2)
502-603: Add a loom test for thespawn_task+ shutdown race conditionThe race condition fixed here is subtle: when
spawn_taskpre-decrementsnum_idle_threadsbut shutdown fires before the worker thread wakes up and setsis_counted_idle = false, the thread exits withis_counted_idle = truedespitenum_idle_threadsalready being decremented—leading to a double-decrement. This is exactly what loom testing is designed to catch. Since no loom tests currently exist for the blocking pool, adding one would make this fix self-documenting and prevent regression.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@tokio/src/runtime/blocking/pool.rs` around lines 502 - 603, Add a loom test that reproduces the spawn_task + shutdown race where spawn_task pre-decrements num_idle_threads and shutdown wins before the worker thread wakes, causing a double-decrement when Inner::run exits with is_counted_idle == true; implement the test to (1) use loom's threading/scheduling primitives to create a worker thread running Inner::run (or exercise the public spawning API that calls spawn_task) and a spawner that calls spawn_task and then triggers shutdown, (2) interleave operations so spawn_task decrements num_idle_threads before the worker observes the wakeup and sets is_counted_idle = false, and (3) assert via loom that no underflow/double-decrement occurs (e.g., num_idle_threads stays valid and no panic), referencing Inner::run, spawn_task, num_idle_threads, is_counted_idle, and shared.shutdown so the test targets the exact race.
510-511: Remove redundant comment at line 510The comment "is this thread currently counted in
num_idle_threads?" is repeated at line 578 (before the read). Remove the declaration-site comment since the reading-site comment provides sufficient context.The uninitialized declaration is correct: Rust's definite-assignment analysis verifies that all execution paths reaching the read at line 579 assign
is_counted_idlebeforehand. This is stricter than a default initializer—future code introducing uninitialized reads will be caught at compile time.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@tokio/src/runtime/blocking/pool.rs` around lines 510 - 511, Remove the redundant declaration-site comment for the local variable `is_counted_idle` in the `pool.rs` blocking pool code: keep the comment that explains its meaning at the read site (before the read at the later location) and delete the earlier comment that duplicates "is this thread currently counted in `num_idle_threads`?" placed above the `let mut is_counted_idle;` declaration; do not change the uninitialized declaration itself—leave `let mut is_counted_idle;` as-is so Rust's definite-assignment checks remain in place.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@tokio/src/runtime/blocking/pool.rs`:
- Around line 502-603: Add a loom test that reproduces the spawn_task + shutdown
race where spawn_task pre-decrements num_idle_threads and shutdown wins before
the worker thread wakes, causing a double-decrement when Inner::run exits with
is_counted_idle == true; implement the test to (1) use loom's
threading/scheduling primitives to create a worker thread running Inner::run (or
exercise the public spawning API that calls spawn_task) and a spawner that calls
spawn_task and then triggers shutdown, (2) interleave operations so spawn_task
decrements num_idle_threads before the worker observes the wakeup and sets
is_counted_idle = false, and (3) assert via loom that no
underflow/double-decrement occurs (e.g., num_idle_threads stays valid and no
panic), referencing Inner::run, spawn_task, num_idle_threads, is_counted_idle,
and shared.shutdown so the test targets the exact race.
- Around line 510-511: Remove the redundant declaration-site comment for the
local variable `is_counted_idle` in the `pool.rs` blocking pool code: keep the
comment that explains its meaning at the read site (before the read at the later
location) and delete the earlier comment that duplicates "is this thread
currently counted in `num_idle_threads`?" placed above the `let mut
is_counted_idle;` declaration; do not change the uninitialized declaration
itself—leave `let mut is_counted_idle;` as-is so Rust's definite-assignment
checks remain in place.
7922: To review by AI