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 |
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.
XTaskQueueRegisterWaiter cast a Box<Waiter> raw pointer to a RegistrationToken without tracking it, risking double-free on duplicate or bogus tokens, deadlocking when a MODE_IMMEDIATE callback unregistered itself via UnregisterWaitEx(INVALID_HANDLE_VALUE), and leaking waiters on XTaskQueueTerminate. timer() also dropped popped delayed items when terminate raced between pop() and submit_now(). Track registered waiters in a token-keyed Arc<Waiter> map, lock RegisterWaitForSingleObject around handle publication, drop the waiter map mutex before blocking in UnregisterWaitEx (both in XTaskQueueUnregisterWaiter and post-registration terminate cleanup), defer unregister to the worker pool on self-unregister, clean up remaining waiters on XTaskQueueTerminate, guard delayed handoff with an inflight counter, and add unit tests covering self-unregister, concurrent unregister lock-release, and deterministic delayed-terminate races. Assisted-by: Antigravity (Gemini 4 Neon)
3a684ef to
c3063fd
Compare
|
Great catch @luckyteagirl — thank you! Fixed both |
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, drop thewaiters()mutex before blocking inUnregisterWaitEx(fixing the review deadlock caught by@luckyteagirl), deferred worker-pool unregister on self-unregister, waiter cleanup on terminate,inflighthandoff guard intimer(), and unit tests (waiter_concurrent_unregister_releases_map_lock_before_blocking_os_wait,dispatch_due_timeddeterministic pop-vs-terminate race).Tested
c3063fdon macOS (aarch64-apple-darwin, Rust 1.100.0):cargo check --offline --target x86_64-pc-windows-msvc --workspace --tests(0 errors, 0 new warnings).04b8042/3a684ef) were validated on Windows by@WebIdentity1(12/12taskqueuetests + negative control),@Toons29(60 s RX 9070 retail smoke run), and@DavidM1911(RTX 3060 Ti).Not tested
c3063fd(rebased onto623d02ewith@luckyteagirl's lock-drop fix and new concurrent-unregister test) has not been executed on Windows or re-smoked withscripts/smoke.ps1yet.AI use