Fix three races in how the pool counts and hands out workers #452
All Vouch review confidence checks passed
Residual risk 0.17 = Change Risk (0.40) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.40 · 279 lines diff across 3 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.337
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #452"
general
weight: 0.35
cjrh
Four small races in how the pool counts and hands out workers. Each has a deterministic test that forces the interleaving and fails on
main. No API change.1. A pool at its minimum can drop below it.
done_with_processreads the busy count and then the idle count. A second worker that finishes in between is in both, so the first one looks surplus and is stopped. The default pool (nomin_workers, so the minimum ismax_workers) is affected too: it loses a worker until the nextget_processfinds none idle and starts one. Fixed by reading both counts under_workers_lock.2. A task can wait although the pool has room.
get_processstarts a worker for a task and then takes one from the queue. Another task can take that worker first, and the first then waits on the queue for a busy worker to return. Fixed by starting and taking under a new lock,_checkout_lock. When the pool really is full, the wait happens outside it.3.
clear_workerscan block. It checksempty()and then calls a blockingget(); a task can take the last worker in between. Fixed withget_nowait.4.
clear_workerscan strand a task. A task counts one idle worker and so does not start one; the clear takes that worker first; the task then waits with nothing busy and nothing idle. Fixed by having the clear take each worker under_checkout_lock. Workers are stopped outside it and still one at a time, so a worker returned during the clear is cleared too.Cost. Worker starts are now one at a time per pool. Starting one took 0.3 ms at the median and 6.5 ms at worst with forkserver here (spawn 0.7 and 14.8, fork 0.5 and 1.4).
Tests.
tests/test_worker_accounting.py. Reverting each fix on its own fails its test.The full suite passes on Python 3.10, 3.11, 3.12, 3.13, 3.14 and 3.14t with the GIL off (203 passed on each, 3.14t four times). The new tests ran 60 times, four at a time, on each of 3.10, 3.13 and 3.14t without a failure.
blackis clean.Next
Agreed in the review: an opt-in idle timeout in its own PR (minimum read live, most recently used order only when it is set, no thread otherwise), then a stop deadline in its own PR. The refresh of kept workers is under discussion below.