From 6a270f536281c091ed1780d530b5ae3790f03419 Mon Sep 17 00:00:00 2001 From: gefydev Date: Sun, 22 Nov 2026 19:46:17 -0300 Subject: [PATCH] Task queues: terminating cancels delayed callbacks before the termination callback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit XTaskQueueTerminate canceled the callbacks queued on the queue's ports, but not delayed ones still waiting in the timer heap. Those count as in flight only once due, so the termination callback ran first and the delayed callback ran (canceled) up to its delay later – when a title may already have freed what it passes as the callback's context. They are now taken out of the timer heap and invoked canceled before the termination callback, as libHttpClient's CancelPendingEntries does when a port terminates. --- crates/xcl/src/hle/xgameruntime/taskqueue.rs | 35 ++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/crates/xcl/src/hle/xgameruntime/taskqueue.rs b/crates/xcl/src/hle/xgameruntime/taskqueue.rs index 92cf196..1914724 100644 --- a/crates/xcl/src/hle/xgameruntime/taskqueue.rs +++ b/crates/xcl/src/hle/xgameruntime/taskqueue.rs @@ -524,6 +524,22 @@ pub unsafe extern "system" fn XTaskQueueTerminate(h: *mut c_void, wait: bool, ct qi.queue.execute(qi.item); } } + // Delayed callbacks of `q` still waiting for their time are canceled too, before the + // termination callback (libHttpClient: CancelPendingEntries when a port terminates). They + // only count as in flight once due, so the wait below would not hold the callback for them, + // and they would run (canceled) after it – when the title may already have freed its context. + let delayed: Vec = { + let (m, cv) = timer(); + let mut heap = m.lock().unwrap(); + let (mine, keep): (Vec, Vec) = std::mem::take(&mut *heap).into_vec().into_iter().partition(|t| Arc::ptr_eq(&t.queue, &q)); + *heap = BinaryHeap::from(keep); + cv.notify_all(); + mine + }; + for t in delayed { + t.queue.inflight.fetch_add(1, Ordering::AcqRel); + t.queue.execute(t.item); + } let ctx = ctx as usize; // raw pointers are not Send let finish = move || { let (m, cv) = &q.idle; @@ -743,6 +759,25 @@ mod tests { } } + #[test] + fn terminate_cancels_delayed_callbacks_before_its_own_callback() { + static ORDER: std::sync::Mutex> = std::sync::Mutex::new(Vec::new()); + unsafe extern "system" fn delayed(_ctx: *mut c_void, canceled: bool) { + ORDER.lock().unwrap().push(if canceled { "delayed, canceled" } else { "delayed, ran" }); + } + unsafe extern "system" fn terminated(_ctx: *mut c_void) { + ORDER.lock().unwrap().push("terminated"); + } + unsafe { + let mut h = std::ptr::null_mut(); + assert_eq!(XTaskQueueCreate(MODE_THREAD_POOL, MODE_THREAD_POOL, &mut h), S_OK); + assert_eq!(XTaskQueueSubmitDelayedCallback(h, PORT_WORK, 60_000, std::ptr::null_mut(), delayed as usize), S_OK); + assert_eq!(XTaskQueueTerminate(h, true, std::ptr::null_mut(), terminated as usize), S_OK); + assert_eq!(*ORDER.lock().unwrap(), ["delayed, canceled", "terminated"]); + XTaskQueueCloseHandle(h); + } + } + #[test] fn composite_queue_termination_preserves_shared_parent_port_work() { struct Rec {