Fix premature semaphore handle closure in Windows IPC waits #262
All Vouch review confidence checks passed
Residual risk 0.14 = Change Risk (0.37) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.37 · 219 lines diff across 2 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.323
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #262"
general
weight: 0.35
Lastique
Windows IPC queue synchronization can close a semaphore handle while a live thread still needs it.
A waiter records the handle, releases the queue mutex, and then enters the Windows wait call. If the thread is delayed between unlocking and waiting, another thread can retire the semaphore after its cleanup timeout. The original waiter can then use an invalid handle, or one that Windows has reused for a different object.
Track local waiters and keep their semaphore handles alive until the waiting path finishes. Release those references on success, cancellation, and exceptions so unused semaphores can still be reclaimed.
Also initialize the semaphore-retirement flag and make access to the lazily resolved semaphore-query function pointer atomic. Its atomic accesses can remain relaxed because the existing dispatcher publishes the pointer through its release/acquire pair; atomic access is still needed because initializers can run concurrently.
Add three tests in
test/run/util_ipc_windows_sync.cpp. They exercise the actual synchronization implementation with real Windows handles while controlling wait scheduling and elapsed time. The test wrapper caps native waits at five seconds and reports a timeout as a failure of the current case, allowing the remaining cases to run.Against the original code, the lifetime test detects that cleanup closes the handle before the waiting path finishes. With the fix, the handle remains open. Two further tests verify that cancelled and failed waits release their references and allow later cleanup.
All three tests pass on native Windows with MSYS2 GCC 16.2 and C++11. A temporary lost-notification test variant fails through the five-second timeout and then runs both remaining cases successfully.