HLE: wait for in-flight monitor callbacks in XTaskQueueUnregisterMonitor #3261
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3263"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T14:58:33Z.
Summary
In
crates/victory-hle/src/modules/xasync.rs,Queue::deliverclonedself.monitorsout of the mutex and invoked callbacks without tracking active invocations, whileXTaskQueueUnregisterMonitoronly removed the token from the vector and returned immediately. Per the documented GDKXTaskQueueUnregisterMonitorcontract, callers may free a monitor's context as soon asXTaskQueueUnregisterMonitorreturns, which produced two use-after-free races:XTaskQueueUnregisterMonitorreturned while another thread was still executing that monitor's callback, allowing the caller to freecontextunderneath the running callback.M2was unregistered while an earlier monitorM1was running,XTaskQueueUnregisterMonitor(q, M2)returned immediately (allowingM2's context to be freed), yetdeliverstill invokedM2from its cloned snapshot onceM1returned.Additionally,
XTaskQueueRegisterMonitorpublished the monitor entry before writing*token, allowing an immediately triggered callback on another thread to race with the token write.Changes
Queue::monitorswithMonitors { entries: Mutex<Vec<Monitor>>, done: Condvar }incrates/victory-hle/src/modules/xasync.rs:Monitors::notifysnapshots active tokens (keeping a zero-allocation fast path when empty), re-checks under the lock right before each invocation that the token is still registered (skipping monitors unregistered mid-notification), records the active callingThreadId, and clears it via aDropguard that removes the entry and wakes waiters onceremoved && running.is_empty().Monitors::unregistermarks the entry removed, removes it immediately when idle, avoids self-deadlock (and dual-thread concurrent self-unregister deadlock) when called from a thread already inside that monitor callback, and otherwise blocks onCondvaruntil every in-flight invocation has returned.XTaskQueueRegisterMonitorwrites*tokenbefore registering the entry under the mutex so the release/acquire edge publishes the token before any callback can run.crates/victory-hle/src/modules/xasync.rs:unregister_monitor_waits_for_in_flight_callbacks(reproduces early return + context destruction during an in-flight callback onmain, verifies blocking wait and suppression of future notifications after fix).unregister_monitor_skips_unstarted_siblings_and_handles_reentrant_self_unregister(reproduces stale-snapshot sibling invocation onmain, and verifies reentrant self-unregister via vtable slot0xc0, nested submission, and dual-thread concurrent self-unregister racing an external waiter).Verification
cargo test -p victory-hle --locked xasync: 15 passed, 0 failed (plus 10 consecutive stress runs of the new monitor concurrency tests: 20/20 passed).cargo test -p victory-hle --lockedand--all-features --locked: 156 passed, 0 failed each.cargo clippy --workspace --all-targets --locked -- -D warnings(default and--all-features): clean.cargo test -p xtask --locked policy::tests::repository_passes_policy: passed.Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T15:09:49Z.
Review: APPROVED
Reviewed the diff and verified locally on Windows 11 x64:
Condvarwait inMonitors::unregistercleanly ensuresXTaskQueueUnregisterMonitorwaits for in-flight external callbacks while avoiding self-deadlock on reentrant self-unregistration and skipping unstarted sibling monitors.cargo test -p victory-hle --lib modules::xasync:: -- --test-threads=1: 15 passed, 0 failed (includingunregister_monitor_waits_for_in_flight_callbacksandunregister_monitor_skips_unstarted_siblings_and_handles_reentrant_self_unregister).cargo clippy -p victory-hle --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Imported from Marvisak/VIctory. Originally posted by Jackster on 2026-11-26T22:52:34Z.
Windows 11,
63bcca4+ this PR:cargo test -p victory-hle: lib 154 passed / 0 failed (unpatched63bcca4here: 152 passed; +2), plus the integration suite (1 passed).cargo clippy -p victory-hle --all-targets -- -D warnings: clean.The two new tests are
unregister_monitor_waits_for_in_flight_callbacksandunregister_monitor_skips_unstarted_siblings_and_handles_reentrant_self_unregister; the re-entrant self-unregister case completes without deadlock on this machine. Synthetic only. AI-assisted.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.