HLE: report queue termination after running callbacks #544
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!544
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-544"
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?
@Quat3rnion on GitHub · 2026-11-24 04:42 UTC · original
Quat3rnion:victory-queue-terminate→main· +357 −13 · 1 files · 2 commits · openWhat
XTaskQueueTerminatecalled its completion callback immediately even when adispatcher had already taken a callback off the queue and was still running
it. The title could then free state the callback still used. The queue now
counts in-flight callbacks:
wait=trueblocks until they finish, andwait=falsereturns immediately with the completion callback deferred to athread that waits. Pending callbacks drained during termination go through
the same count, so completion is reported exactly once and only after work
has stopped.
An AI tool wrote part of this change.
Tested
cargo test --workspace: 165 passed, 0 failed, 3 ignored, stable over three runs. A new regression blocks a dispatched callback on a flag, callsTerminate(false), checks the termination callback has not run, releases the worker and checks it is reported once afterwards.cargo check --target x86_64-pc-windows-gnu --workspace --testspasses. No Windows host here.cargo xtask policypasses (150 files ok). No game files or data from them were added.Checklist
cargo xtask policypasses. No game files or data from them were added.@geiyiliyabumo on GitHub · 2026-11-24 11:45 UTC · original
Independent verification & code review on Windows 11 Pro (build 26100, Rust
1.98.1), cleanly merged ontomain(ffe310e):cargo xtask policy: PASS (154 files ok)cargo clippy -p victory-hle --all-targets --locked -- -D warnings: PASScargo clippy -p victory-hle --all-targets --features diagnostics --locked -- -D warnings: PASScargo test -p victory-hle --locked: 95 passed + 1 integration test (no_dll_planting), 0 failed (including newtermination_waits_for_a_running_callback)Concurrency findings (
crates/victory-hle/src/modules/xasync.rs)wait == trueis ignored whencallbackisNone:if wait { queue.wait_idle(); ... }is placed insideif let Some(callback) = callback. CallingXTaskQueueTerminate(q, true, null, None)(a common synchronous drain pattern) returns immediately without waiting for in-flight callbacks. Movingif wait { queue.wait_idle(); }ahead ofif let Some(callback) = callbackfixes this.Mode::Manualcallbacks are tracked inqueue.running:Mode::ThreadPool(POOL.work),Mode::Serialized(its own worker loop), andMode::Immediateall calltask.run(false)directly without going throughqueue.run(), soXTaskQueueTerminatecan still report completion while non-manual callbacks are running.port.take()andqueue.run(): InXTaskQueueDispatch,port.take()releasesport.tasksbeforequeue.run()incrementsqueue.running. IfXTaskQueueTerminateruns in that gap, it drains an emptyport.tasks, observesrunning == 0, and fires the termination callback before the popped task even starts. Incrementingrunningbefore releasingport.tasks(or tracking in-flight count onPort) closes that window.AI-assisted; synthetic tests only, no game files.
@fromthemoonx on GitHub · 2026-11-24 11:48 UTC · original
One case to add to @geiyiliyabumo's list. Tested
2a55463merged ontomainffe310e; Windows 11 Pro 26300, Ryzen 7 5700X, Rust 1.98.1x86_64-pc-windows-msvc.Terminate(wait = true)from a callback on the same queue never returns. The dispatched callback is counted inrunningbyqueue.run, andwait_idlethen waits forrunningto reach 0, which includes itself. A throwaway test (not pushed) submits a callback that callsXTaskQueueTerminateon its own queue and dispatches it from another thread:XTaskQueueTerminate(q, true, null, Some(done))wait_idleXTaskQueueTerminate(q, true, null, None)waitis skipped without a callback (point 1 above)So once point 1 is fixed, both forms deadlock. On
mainthe call returns, sincewaitis ignored there. A game may well terminate a queue from one of its own completion callbacks (for example when an async operation finishes and its owner is torn down). One option is to note the dispatching thread's queue in a thread-local insidequeue.run, and not wait for that one callback whenTerminateis called from it.The race between
port.take()and therunningincrement that I'd also noticed is point 3 above, so I won't repeat it.AI-assisted (Claude Code); synthetic test only, no game data.
@Quat3rnion on GitHub · 2026-11-24 14:26 UTC · original
@geiyiliyabumo @fromthemoonx Thanks for the detailed concurrency review. New head
f8b3cfa9fixes all four points:wait=truenow waits before thecallbackcheck, so it also drains whencallbackisNone.Trackershared by everyTaskcounts callbacks from all modes (manual, thread pool, serialized and immediate), not just manual dispatch.Task::begin()runs while the port lock is held (inPort::takeand while draining on terminate), closing the take-to-run window.wait_idle()ignores callbacks running on the calling thread via a thread-local scope, soTerminate(wait=true)from inside a queue callback returns instead of deadlocking; a scope guard also ends the slot on unwind.Added regressions for wait-without-callback, termination from an own callback, and thread-pool callback tracking.
cargo test --workspace --no-fail-fast: 222 passed, 0 failed (11 xasync tests). Would appreciate a re-check.@Quat3rnion on GitHub · 2026-11-24 14:27 UTC · original
One more edge case covered in the same head: nested callbacks on one thread.
wait_idle()now leaves a slot for each callback running on the calling thread (not just one), with a regressionterminate_from_nested_callbacks_does_not_wait_on_itself. 12 xasync tests pass.@geiyiliyabumo on GitHub · 2026-11-24 14:33 UTC · original
Re-verified new head
f861613merged ontomainffe310eon Windows 11 x64 MSVC:Checks Run
cargo xtask policy: PASS (154 files ok)cargo test -p victory-hle --locked -- xasync: PASS (12/12tests)cargo clippy -p victory-hle --all-targets --locked -- -D warnings: FAIL (3 errors)Findings to Fix Before Merge
1. Three
clippy -D warningsCI blockerscrates/victory-hle/src/modules/xasync.rs:104:clippy::ptr_eq— replace**p == self as *const Trackerwithstd::ptr::eq(**p, self)(and same inCallbackScope::drop).crates/victory-hle/src/modules/xasync.rs:1550and:1657:clippy::undocumented_unsafe_blocks— add// SAFETY:comments above bothunsafe { (*context).0.load(Ordering::SeqCst) }loops in the new tests.2. Two remaining subtle deadlock cases in
Tracker/CallbackScopeThe overall
Tracker+Task::begin()under port lock + thread-localIN_CALLBACKdesign is much cleaner and fixes all 4 previous issues, but two edge cases in the new tracking code can still hang:Tracker::end()only notifies when*running == 0(xasync.rs:90-96):Q(own = 1), Thread B is simultaneously running another callback onQ(running = 2), and Thread A callsXTaskQueueTerminate(Q, true, ...).wait_idle()waiting for*n <= own(1). When Thread B finishes,*runningdrops2 -> 1, which skipsif *running == 0 { self.idle.notify_all(); }, leaving Thread A parked forever.self.idle.notify_all()whenever*runningis decremented (not only at0).CallbackScope::dropremoves all matching entries viaretain(xasync.rs:127-131):IN_CALLBACK = [T, T]), when the inner callback returns,v.borrow_mut().retain(|p| *p != ptr)strips both entries. If the outer callback subsequently callsXTaskQueueTerminate(Q, true, ...),ownevaluates to0whilerunning == 1, deadlocking on itself.if let Some(i) = v.iter().rposition(|p| std::ptr::eq(*p, ptr)) { v.remove(i); }).AI-assisted; synthetic tests only, no game files.
@Marvisak on GitHub · 2026-11-25 00:24 UTC · original
Automated triage for the maintainers.
Needs rebase. This PR does not merge cleanly into current
main(ac2525d). Conflicting files:crates/victory-hle/src/modules/xasync.rs.The content has not been reviewed yet; it will be once the branch is rebased onto
main.Generated by Claude Code
View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.