HLE: wait for in-flight monitor callbacks in XTaskQueueUnregisterMonitor #3263
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!3263
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-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?
@fuzeofficlal on GitHub · 2026-11-26 14:58 UTC · original
fuzeofficlal:fix/hle-xtaskqueue-unregister-monitor-sync→main· +422 −11 · 1 files · 1 commits · openSummary
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.Queue::deliver previously cloned the monitor list out of its mutex and invoked callbacks without tracking active invocations, while XTaskQueueUnregisterMonitor only removed the token from the list and returned immediately. Under the documented GDK contract, a title may free a monitor's context as soon as XTaskQueueUnregisterMonitor returns, causing (1) heap use-after-free when an in-flight monitor callback on another thread is still running, and (2) heap use-after-free when a sibling monitor unregistered mid-notification is still invoked from a stale snapshot. Track per-monitor removal and active thread frames inside Monitors { entries, done }: - notify() snapshots registered tokens, re-verifies each token is still active right before invoking it (skipping monitors unregistered meanwhile), and uses a drop guard to clear the active thread entry and wake waiters even on unwind. - unregister() marks the monitor removed so no new invocation can start, deletes it immediately when idle, skips blocking when called reentrantly from a thread currently inside that same monitor callback (avoiding self-deadlock and dual-thread self-unregister deadlock), and otherwise blocks on a Condvar until every in-flight invocation has returned. - XTaskQueueRegisterMonitor writes *token before publishing the monitor under the mutex so any immediately invoked callback observes its own token race-free.@Dukestar69 on GitHub · 2026-11-26 15:09 UTC · original · approved these changes
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).
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.