gdk rt: fix XTaskQueue handle duplication, pool dispatch, monitors, close, and manual terminate wakeup (6 conformance scenarios) #307
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
library
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/omonkeyz--xbox-emulator!307
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/307/head"
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?
@Bexx1107 on GitHub · 2026-11-23 17:30 UTC · original
Bexx1107:fix/xtq-port-dispatch-handles→main· +309 −131 · 2 files · 2 commits · closed without mergingLibrary / area:
compat-common, GDK runtime task queues (crates/compat/compat-common/src/gdk/rt.rs), conformance table (crates/compat/compat-common/tests/gdk_xtaskqueue_conformance.rs)Exports changed (exports.txt rows): none
Source of the behaviour: existing conformance scenarios in
crates/compat/compat-common/tests/support/xtq.rs(verified against local PCxgameruntime.dllviagdk_xtaskqueue_conformance_real) and the Win32 Thread Pool API (TrySubmitThreadpoolCallback/CallbackMayRunLong). Rewritten cleanly in Rust; no game/console files or binaries included.Scoped to avoid overlapping open PRs
#210,#270, and#271(does not alterAState/XAsyncwait/completion state,XTaskQueueRegisterWaiter/UnregisterWaiter, or two-phase termination, and does not editdocs/BLOCKERS.md).Changes (
src/gdk/rt.rs)verify_duplicate_queue_handle): track open handles in a global handle table keyed by unique heap-allocated handle tokens (alloc_handle/close_handle) referencingArc<Queue>.XTaskQueueDuplicateHandlereturns a distinct handle per call,XTaskQueueGetCurrentProcessTaskQueue/XTaskQueueSetCurrentProcessTaskQueuemanage distinct handle tokens cleanly, and invalid pointers fail validation without raw dereferencing.verify_queue_monitor_has_correct_ports): store monitors keyed byQueue::id()with(target_port_ptr, handle, Weak<Queue>)and include the submitted port insubmit_tagged/submit_marker.Port::notifyinvokes all registered monitors attached to thatPort, passing each queue its own live handle and registered port.verify_thread_pool_callbacks_can_block,verify_dispatch_on_pool_port): replace the fixed 2–8 workerPoolwith Win32TrySubmitThreadpoolCallback+CallbackMayRunLong(falling back tostd::thread::spawnif Win32 submission fails) drainingPort::itemsviaPort::drain_one, and allowq_dispatchto pop from all non-IMMEDIATEports (MANUAL,THREAD_POOL,SERIALIZED).INFINITEdispatch wakeup on terminate (verify_multithread_manual_dispatch): addPort::terminate()(called only whenqq.owns) and break out ofPort::dispatchwhenitemsis empty andself.terminatedis set, while composite termination leaves the underlying shared port running.verify_close_keeps_queued_callbacks): stop callingcancel_all()insidePort::release()so closing the last handle lets already-queued and delayed callbacks complete non-canceled before idle serialized workers exit. Updatedport_tests::serialized_worker_exits_after_last_user_releases_the_portand addedport_tests::released_serialized_port_runs_queued_callbacks_then_exits.Rows removed from
DIFFERS(6, 13 remain)verify_close_keeps_queued_callbacksverify_dispatch_on_pool_portverify_duplicate_queue_handleverify_multithread_manual_dispatchverify_queue_monitor_has_correct_portsverify_thread_pool_callbacks_can_blockTests (after final edit)
cargo test -p compat-common --lib gdk::rt::port_tests: 5 passed, 0 failedcargo test -p compat-common --test gdk_xtaskqueue_conformance: 5 consecutive debug runs passed (61 passed, 0 failed each) + release run passed (61 passed, 0 failed). (Note: an earlier pre-rerun batch hit the pre-existingverify_simple_async_callXAsyncGetStatus(wait)race once on run 2;verify_simple_async_callpassed 20/20 standalone — that wait-vs-callback ordering is addressed separately in open PR#210.)cargo test -p compat-common --test gdk_xtaskqueue_conformance_real: 61 passed, 0 failed (debug and release)cargo test -p compat-common --lib: 148 passed, 0 failed, 1 ignoredpython .github/scripts/pr_gate.py --base main --head HEAD:2 file(s) checked, 0 problem(s)cargo test -p compat-commonpasses for the targets listed aboveNo game or console files and no binaries included
No fake success; anything not possible is left
missingwith a note@QuentinDanblon on GitHub · 2026-11-23 18:08 UTC · original
This overlaps #270 (mine) in
rt.rs(q_terminate,submit_marker,flush_timers,resolve), so I checked both against a public reference: microsoft/libHttpClient (MIT) ships a full XTaskQueue/XAsync implementation and its unit tests, at commit c5007fd9. It is notxgameruntime.dll, so where it disagrees with a PC measurement the measurement wins; here they agree.Where this PR matches the reference:
USE_UNIQUE_HANDLES, testVerifyDuplicateQueueHandle).VerifyQueueMonitorHasCorrectPorts).One point to adjust,
Dispatch(INFINITE)exiting at termination: the reference exits when the dispatching queue's port context reachesTerminatedwith its port empty (L836-L923), which happens only after the termination marker has run on that port. If the exit flag is raised whenXTaskQueueTerminateis called, a manual queue dispatched by a single thread can leave before the marker runs, and the termination callback never fires (testVerifyManualDispatchAtTermination). A dispatch on a composite handle should also end with the composite, not with the shared port.On the termination order itself, #270's two-phase model (work port refuses at once with
E_ABORT, completion port accepts until the work marker has run, delayed entries and waiters run canceled at the end) is what the reference does (L2254-L2318, L2359-L2410) and what #270 measured on the PC runtime. #270 also confirms from the reference its two points it had left unverified (waiter handle not duplicated;WAIT_ABANDONEDignored and not rearmed, L1902-L1916).So a merge order that keeps both: #270 first, then this PR rebased so the dispatch exit keys on the port context status that #270 sets, and
release()still drops wait registrations (no callback) but nothing else. If it helps, I can do that rebase on top of #270 in a branch you can pull, or adapt #270 to whatever you prefer. In #270, waiters should then be keyed by the queue identity rather than the handle, since handles become distinct per duplicate here.@Bexx1107 on GitHub · 2026-11-23 18:23 UTC · original
Thanks @QuentinDanblon for checking this against
libHttpClientand catching thatDispatch(INFINITE)edge case!Pushed follow-up commit
558b6ffto fix it before the rebase:Queuenow holds per-queue per-port exit flags (port_done: [Arc<AtomicBool>; 2]), flipped only when each port's termination marker runs (forwardon the work port,deliver/doneon the completion port) and wakes waiting dispatchers viaPort::wake(), whilePort::terminatedremains for submit rejection and canceled execution.q_dispatchpasses&qq.port_done[port as usize]so a manual queue's completion dispatcher waits until the completion marker runs, and terminating a composite endsDispatch(INFINITE)on that composite without affecting dispatchers on the shared base port.port_tests:manual_completion_infinite_dispatch_waits_for_termination_markerandcomposite_infinite_dispatch_exits_when_composite_terminates_without_stopping_base.Checked on
558b6ff:cargo test -p compat-common --lib gdk::rt::port_tests: 7 passed, 0 failedcargo test -p compat-common --test gdk_xtaskqueue_conformance: 5 debug runs + 1 release run, 61 passed, 0 failed eachcargo test -p compat-common --test gdk_xtaskqueue_conformance_real: 61 passed, 0 failedcargo test -p compat-common --lib: 150 passed, 0 failed, 1 ignoredpython .github/scripts/pr_gate.py --base main --head HEAD: 2 file(s) checked, 0 problem(s)Your merge plan makes total sense — let's land
#270first, and I'm happy to rebase#307onto#270once it's in (keying dispatch exit on#270's port-context status and keeping#270's wait-registration cleanup inrelease()). Keying#270's waiters byQueue::id()instead of the handle pointer aligns both cleanly.@QuentinDanblon on GitHub · 2026-11-23 18:33 UTC · original
Thanks,
558b6ffmatches the reference on that point. Agreed on the order. On main a duplicated handle is the same pointer as the queue, so #270's waiters keyed by handle are correct there;Queue::id()only exists once this PR lands, so the re-key fits best in your rebase onto #270. If you would rather I do it in #270 first with a stand-in identity, say so and I will push it.@Bexx1107 on GitHub · 2026-11-23 18:52 UTC · original
Sounds good — leave #270 as-is and I'll handle the waiter re-key to Queue::id() when rebasing #307 onto #270 once it lands.
@omonkeyz on GitHub · 2026-11-25 13:49 UTC · original
Applied on main in
ba8e0e5(cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.Pull request closed