Repository navigation
XGameRuntime: a task queue's termination callback runs on its completion port - #1154
Conversation
|
Independent Windows 11 build 26300 check, Ryzen 7 9800X3D, Rust 1.99.1 MSVC, debug. I queued a completion on a parent, then terminated a child asynchronously while both shared that manual completion port.
Both revisions used AI-assisted. Codex (GPT-6, exact variant unavailable) wrote and ran the scratch fixture and drafted this report. |
|
Independent verification on current Machine: Windows 11, Ryzen 9 7900X; Rust stable MSVC, debug.
AI use: test runs by Devin (Cognition, SWE-2); no code changes. |
|
Rerun on the new head
Same machine, debug. The report below is for NVIDIA run with the whole suite, since Not tested lists NVIDIA. Tested
Not tested
AI use
|
|
On Tested: Windows 11, debug, CPU only. Idle queue (ThreadPool / Manual), a pump thread blocked in dispatch, then
From the code: Terminate wakes the dispatcher, which returns false because the queue is terminated and empty, so the pump ends before the callback is queued. The PR's test waits for the entry and then dispatches once with a zero timeout. Suggested (not tested): an infinite dispatch must not return false on a terminated queue until its termination entry has run. Test (append to taskqueue.rs)#[cfg(test)]
mod pump_probe {
use super::*;
#[test]
fn infinite_completion_pump_gets_the_termination_callback() {
unsafe extern "system" fn terminated(ctx: *mut c_void) {
(*(ctx as *const std::sync::atomic::AtomicBool)).store(true, std::sync::atomic::Ordering::SeqCst);
}
let mut missed = 0;
for _ in 0..200 {
unsafe {
let mut h = std::ptr::null_mut();
assert_eq!(XTaskQueueCreate(MODE_THREAD_POOL, MODE_MANUAL, &mut h), S_OK);
let ran = std::sync::atomic::AtomicBool::new(false);
let hp = h as usize;
let pump = std::thread::spawn(move || while XTaskQueueDispatch(hp as *mut c_void, PORT_COMPLETION, u32::MAX) {});
std::thread::sleep(Duration::from_millis(5));
assert_eq!(XTaskQueueTerminate(h, false, &ran as *const _ as *mut c_void, terminated as usize), S_OK);
pump.join().unwrap();
std::thread::sleep(Duration::from_millis(50));
if !ran.load(std::sync::atomic::Ordering::SeqCst) { missed += 1; }
// drain so that `ran` outlives the callback
let t = Instant::now() + Duration::from_secs(2);
while !ran.load(std::sync::atomic::Ordering::SeqCst) && Instant::now() < t { XTaskQueueDispatch(h, PORT_COMPLETION, 1); }
XTaskQueueCloseHandle(h);
}
}
assert_eq!(missed, 0, "missed {missed} of 200");
}
}AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this comment. I checked the points before posting. |
…ion port XTaskQueueTerminate(wait = false) had a worker of the shared pool call the title's termination callback directly. The GDK docs say the callback "will be invoked from the completion thread at the end of termination", and their example terminates a queue with a manual completion port from WM_CLOSE and calls DestroyWindow in the callback, relying on it running from the UI thread's own XTaskQueueDispatch. libHttpClient likewise queues a termination entry on the completion port and notifies the queue's monitors. Once in-flight work and completions have drained, the callback is now submitted to the completion port through the same path as other completion callbacks (which notifies monitors), allowed past the terminated check. On a manual port it runs from the title's XTaskQueueDispatch, on a thread-pool port on the pool. QueueState tracks pending async termination callbacks so a completion-port XTaskQueueDispatch with a timeout (such as INFINITE) waits for the termination entry before returning false. With wait = true the callback still runs on the calling thread before Terminate returns: on a manual port nobody may dispatch while the caller waits, which the docs warn may deadlock. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Assisted-by: Antigravity (Gemini 4 Neon)
ea68eaa to
cc8fbcd
Compare
|
Fixed in
Reproduced the failure with AI use: Antigravity with Gemini 4 Neon wrote the fix, test, and this reply. |
|
Re-verification on RTX 3060 Ti (Windows 11), new head merged with current
Thanks! — Devin |
…cl-dev#1154, verbatim Windows + Linux twin) Upstream xcl-dev#1154 (live pr1154head=cc8fbcd6, still OPEN, merge-base false): docs say the Terminate callback is invoked from the completion thread, but XCL ran it on a pool worker. Windows hle/xgameruntime/taskqueue.rs is the upstream diff applied verbatim (git apply clean, +114/-10): termination_cb_pending field, submit/submit_now terminating split (lets the callback through once terminated, notify_all on manual ports), completion-port Dispatch waits while pending > 0, Terminate wait=false queues run_terminated to PORT_COMPLETION (wait=true still runs inline on the caller). Two upstream tests verbatim. Linux live twin hle/xtaskqueue_linux.rs mirrors the production hunks (win64 ABI; no pc() legs exist on the twin): same field, wrapper + submit(terminating), dispatch pending-wait, Terminate-via-port + run_terminated (extern win64). One Linux-shaped twin test (manual completion port, wait=false, callback must not run before Dispatch and must run on the dispatch thread). Failing-first: new twin test FAILED pre-fix (callback ran on pool worker ThreadId(4) before any Dispatch), green post-fix. Gates: cargo check -p xcl --tests + cargo check --workspace 0 errors; lib 651/0/1 t=4 (+1); twin 6/6; diff --check clean; lock audit clean (named guards; dispatch wait binds state in a block dropping before the body; test lock is a poll-only temporary in the while condition). Title fresh-root: done failures 12, all D3D12.X GPU-only, 0 non-GPU, ExitProcess(0xc). Retail fresh-root: SIGSEGV game_scarlett_master.exe+0x2237320 fault 0x0 exit 139, stubs 20, xgr exactly XAsync/XNetworking/XUser - HOLDS, M1-RE TERMINAL.
, verbatim Windows + Linux twin) Live pr805head=e88ee4d1 still OPEN (merge-base false); Windows hle/xgameruntime/taskqueue.rs hunks hand-applied byte-identical (cherry-pick -n conflicted at the shared test-site tail: steps-49/50 tests moved the append anchor; production + test hunks verified identical to the head by direct diff, only line numbers drift). Fidelity: git diff pr805head shows only steps-49/50 drift (xcl-dev#1276/xcl-dev#1681 timer, xcl-dev#1154 terminate); pool block + mpsc test import + new test byte-identical. Production: fixed clamp(2,8) mpsc pool -> Pool{Mutex<PoolState>,Condvar, base} + MAX_WORKERS=256 + IDLE_EXIT 30s + worker loop + spawn wake-idle-else-grow with rollback + notify on spawn failure. Linux live twin hle/xtaskqueue_linux.rs mirrors the production hunks (ABI-neutral pool; win64 ABI untouched) + 1 Linux-shaped twin test (16 blockers + releaser through wait=true Terminate, extern win64). Failing-first: twin test FAILED pre-fix (7 vs 16, releaser starved on the fixed 8-worker pool), green post-fix. Gates: both cargo checks 0 errors; lib 652/0/1 t=4 (+1); diff --check clean; lock audit clean (named g/ng guards, explicit drop before job). Title fresh-root 12 unique FAILs all D3D12.X GPU-only 0 non-GPU (harness FAILs by design, M2 baseline). Retail fresh-root SIGSEGV game_scarlett_master.exe +0x2237320 fault 0x0 unmapped exit 139 stubs 20 xgr exactly XAsync/XNetworking/XUser unimplemented [] - HOLDS, M1-RE TERMINAL.
|
NVIDIA and AMD full-suite run, to go with the reports above by fromthemoonx, since none of them covers AMD. Tested exact head Tested
Not tested
AI use
|
|
merge-check claim |
|
merge-check |
|
merge-check claim |
|
merge-check |
XTaskQueueTerminatewithwait = falseran the title's termination callback on XCL's own worker thread, as soon as the queue's work had finished. Microsoft's documentation ofXTaskQueueTerminatesays the callback is invoked from the completion thread at the end of termination. Its example for a manual completion port relies on that: the callback is queued to the completion port, so it already runs on the title's UI thread. A title that dispatches its completion port by hand could get the callback on a thread it does not expect, while that thread may still be touching the queue.Now, with
wait = false, the callback is submitted to the queue's completion port once the pending work and completions are done. The port's monitors are notified, so on a manual port the callback runs from the title's ownXTaskQueueDispatch. While an async termination callback is still pending submission, a completion-portXTaskQueueDispatchwith a timeout (includingINFINITE) waits for it instead of returningfalseearly. Withwait = truethe callback still runs on the calling thread beforeTerminatereturns: queuing it there could hang a caller whose manual port nobody dispatches while it waits.What is in it
crates/xcl/src/hle/xgameruntime/taskqueue.rs:submit_nowgoes through a privatesubmitthat can let the termination entry past the terminated check.QueueState::termination_cb_pendingkeeps a manual completion-portdispatchwaiting until the async termination entry has been queued.run_terminatedruns the queued callback.termination_callback_runs_from_a_manual_completion_port_dispatchandinfinite_completion_pump_receives_termination_callback.Tested
cargo test --workspace -- --test-threads=4on cc8fbcd: 743 passed, 0 failed, 41 ignored; no SKIPPED GPU testscargo test -p xcl --lib taskqueue -- --test-threads=4: 12 passed, 0 failedinfinite_completion_pump_receives_termination_callbackfailed before the pending-termination wait (pump exited before termination callback in 49 of 50 runs); passes 50/50 afterRegression run
main:
this PR:
Not tested
wait = false(the smoke title's run is a regression check only).AI use