Repository navigation
XGameRuntime: track task queue waiters safely and avoid terminate races - #315
Michael-Glaser wants to merge 1 commit into
Conversation
|
A Windows run for this draft, since Not tested says it has not been built or run on Windows yet: Tested
Not tested
AI use: Claude Code with Claude Opus 5.5 ran the build and tests and wrote this comment; the numbers come from those runs. |
0cdf319 to
04b8042
Compare
|
Thanks @regiumlepidi! Rebased onto main (a6f5f7a, commit 04b8042) and replaced the timing loop with a deterministic dispatch_due_timed pop-vs-terminate test that holds Terminate right between pop() and submit_now() so it reliably reproduces the dropped callback without the fix. |
|
Updated-head Windows validation: Tested
Not tested AI use |
XTaskQueueRegisterWaiter handed the raw Box<Waiter> pointer out as the registration token and wrote (*w).wait after RegisterWaitForSingleObject had already armed waiter_fired. XTaskQueueUnregisterWaiter blindly cast any token back to Box<Waiter> (double-free on duplicate or bogus tokens) and called UnregisterWaitEx(..., INVALID_HANDLE_VALUE) synchronously (deadlocking when called from a MODE_IMMEDIATE waiter callback). XTaskQueueTerminate also left waiters registered and could race with timer() between popping a due item and submit_now, silently dropping the canceled callback. Track waiters in a token-keyed map with Arc<Waiter>, hold the wait-handle lock across RegisterWaitForSingleObject, detect self-unregister via thread-local IN_WAITER and defer blocking UnregisterWaitEx to the worker pool, unregister a queue's remaining waiters during XTaskQueueTerminate, and hold an inflight handoff guard across timer()'s pop-to-submit_now transition under the existing timer -> q.state lock order. Assisted-by: Antigravity (Gemini 4 Neon)
04b8042 to
3a684ef
Compare
|
Thanks @WebIdentity1 for validating |
|
Regression run of Against the limits in CONTRIBUTING.md: 101 % of AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0, AI use: Claude Code (Claude Opus 5.5) built both trees and ran |
|
Verification on RTX 3060 Ti (Windows 11), branch merged with current
Thanks! |
…l-dev#1239 + xcl-dev#315, verbatim Windows + Linux twin) Windows hle/xgameruntime/taskqueue.rs: xcl-dev#315 waiter form (superset of xcl-dev#1239): immutable Arc<Waiter> (wait Mutex + stopped + callback_arc), monotonic-u64 token map, IN_WAITER thread-local + guard, non-blocking self-unregister offload to pool, post-insert terminated race, Terminate waiter cleanup. xcl-dev#1239 + xcl-dev#315 waiter tests verbatim. xcl-dev#315 timer hunk skipped: landed xcl-dev#1276 already covers the due-timer race (different dispatch_due_timed shape); its timer test superseded by the landed deterministic + 40-iter tests. Linux hle/xtaskqueue_linux.rs twin: helper-thread-per-waiter on kernelx WaitForSingleObject (5ms poll), same token map / guard / cleanup, win64 ABI, 4 twin tests (fires-once, dup/bogus, self-unregister, terminate). Failing-first: grafted twin probe onto HEAD worktree E0425 (no waiters()); twin 11/11 green.
luckyteagirl
left a comment
There was a problem hiding this comment.
At 3a684ef, both XTaskQueueUnregisterWaiter (taskqueue.rs:808–809) and the post-registration terminated cleanup (:795–796) call unregister_waiter inside a successful if let Some(...) = waiters().lock().unwrap().remove(...) body. The temporary mutex guard remains held through that body. unregister_waiter then calls UnregisterWaitEx(..., INVALID_HANDLE_VALUE), which waits for any running callback to finish.
One reachable deadlock: waiter A's MODE_IMMEDIATE callback has entered title code; another thread removes A and waits in UnregisterWaitEx while still holding the waiter map; A's callback tries to unregister its own (now absent) token and blocks trying to inspect the map. Neither thread can finish. The self-unregister branch cannot help because the callback never reaches unregister_waiter. The legacy base has no registry mutex around its blocking OS unregister, so this particular lock cycle is introduced here.
Could each site first bind the removal result in a separate statement, letting the map guard drop before the blocking unregister? A gated immediate callback that looks up its already-removed token while another thread unregisters A would exercise this. A Rust 2024 mutex-scope probe confirmed the guard is held in the current if let body and released by the split statement; I did not deliberately hang XCL's native waiter test. Codex (GPT-6.2 Sol and GPT-6.2 Astra) verified this exact-head source path and interleaving.
Fixes raw-pointer waiter double-free,
MODE_IMMEDIATEself-unregister deadlock, leaked waiters onXTaskQueueTerminate, and atimer()pop-vs-terminate race that dropped canceled delayed callbacks.What is in it
crates/xcl/src/hle/xgameruntime/taskqueue.rs: token-keyedArc<Waiter>map, lockedRegisterWaitForSingleObject, deferred worker-pool unregister on self-unregister, waiter cleanup on terminate,inflighthandoff guard intimer(), and unit tests.Tested
aarch64-apple-darwin, Rust 1.100.0):cargo check --offline --target x86_64-pc-windows-msvc --workspace --tests(0 errors, 0 new warnings).Not tested
cargo testandscripts/smoke.ps1not run yet.AI use