XGameRuntime: fix XAsync/XTaskQueue termination deadlocks, double-cleanup race, and XErrorReport lock #97
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/98"
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?
Imported from ivall/xcl. Originally posted by raylitebull on 2026-11-23T11:11:10Z.
Summary
Fixes deterministic deadlocks, a double-cleanup race, completion-ordering races, and port-state bugs in
crates/xcl/src/hle/xgameruntime/{xasync.rs, taskqueue.rs, api.rs}:Atomic termination check on submission (
taskqueue.rs,xasync.rs):terminated/inflightatomics withstate: Mutex<QueueState { terminated: bool, inflight: [usize; 2] }>+idle_cv: Condvarunder strict lock orderp.items/timer->q.state.submit_now/submit_delayednow atomically reject submissions once terminated without incrementinginflightor enqueuing work.XTaskQueueTerminate(q, true, ...)on aMODE_MANUALcompletion port drainedPORT_COMPLETION, then ran canceled delayed async work callbacks whoseXAsyncCompletere-submitted ontoPORT_COMPLETIONand leftinflight > 0forever. Also stops callbacks from running on already-terminatedMODE_THREAD_POOLqueues.XTaskQueueDispatch(q, port, INFINITE)wakeup on termination (taskqueue.rs):dispatch()checks!q.is_terminated()in its wait loop and returnsfalseonce empty and terminated.XTaskQueueTerminatecallscv.notify_all()on both ports so threads blocked onINFINITEwake promptly.XAsyncCompletepublish ordering, single-consumerOP_CLEANUP, and payload reclaim (xasync.rs):{done, cleaned}booleans with an explicitPhasestate machine (Pending -> Completing -> Done -> Consuming -> Consumed).XAsyncCompletetransitionsPending -> Completing, runsOP_CLEANUPunlocked whenresult_size == 0, writesFLAG_DONEand removessif cleaned, and only then publishesDone/Consumedbefore signaling waiters or submittingcompletion_cb(preventing premature destruction of stackXAsyncBlocks orFLAG_CONSUMEDclobbering).XAsyncGetResultatomically claimsDone -> Consumingunder lock so concurrentXAsyncGetResultcalls cannot runOP_CLEANUPtwice (eliminating double-free UB; losing callers receiveE_ILLEGAL_METHOD_CALL).begin_with_fixupsreclaimsBox<Payload>whenXAsyncBeginreturns an error.Per-port
XTaskQueueIsEmpty&XTaskQueueRegisterWaiterbounds/termination (taskqueue.rs):inflightper port ([usize; 2]) soXTaskQueueIsEmpty(q, PORT_COMPLETION)does not reportfalsewhen onlyPORT_WORKhas items.XTaskQueueRegisterWaitervalidatesport <= 1(E_INVALIDARGinstead of panicking insidewaiter_fired) andwaiter_firedignores terminated queues.XErrorReportmutex deadlock (api.rs):(callback, context)tuple out ofERROR_CBbefore invoking it so re-entrantXErrorSetCallback/XErrorReportcalls do not self-deadlock.Testing
All race and deadlock fixes include multi-iteration stress tests with barriers and short timeouts (would hang/fail on
origin/main, pass deterministically here):terminate_manual_completion_port_with_delayed_async_does_not_deadlock(50 iterations, 2s watchdog + already-terminated pool check)complete_finishes_cleanup_and_header_before_publishing_done(50 iterations zero-payload + 50 iterationsFLAG_CONSUMEDpreservation)concurrent_get_result_runs_cleanup_exactly_once(100 iterations × 4 threads on counting provider + 50 iterations × 4 threads onbegin_with)complete_synchronously_inside_begin_zero_and_nonempty_payload(syncXAsyncCompleteinsideXAsyncOp::BeginonMODE_IMMEDIATEqueue)begin_with_fixups_drops_payload_on_begin_failuredispatch_infinite_wakes_and_returns_false_on_terminate(50 iterations across both ports, 2s timeout)is_empty_tracks_work_and_completion_ports_independentlyregister_waiter_validates_port_and_ignores_terminated_queuexerror_report_allows_reentrant_set_callback_and_report(2s timeout)Verified after rebasing onto
origin/main(60599a9):cargo build --workspaceandcargo test --workspace -- --test-threads=4pass with 0 failures and 0 new warnings.Imported from ivall/xcl. Originally posted by ivall on 2026-11-23T11:25:43Z.
Merged together with #95–#102 and checked on Windows 11 with an RX 9070:
cargo test --workspacegreen with the retail samples (shader pack, console DLLs, texture archive), lib tests also withXCL_SPLIT_WAVE64=1; XDefiant (title screen, a scripted A reaches its "Xbox network unavailable" dialog), Unicorn Overlord and Minecraft run. Thanks!