XGameRuntime: XNetworking change registrations fire the initial callback #1227
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1237"
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 emirduran707 on 2026-11-24T13:34:06Z.
XNetworkingRegisterConnectivityHintChangedandXNetworkingRegisterPreferredLocalUdpMultiplayerPortChangedhanded out a token and never called the callback. Microsoft's documentation says both send a first callback when they register: the connectivity one "registers a "connectivity hint changed" notification and sends an initial notification callback", and the port one "always fires an initial notification callback". The network-initialization guidance builds on that: a title registers and then waits for the callback to reportnetworkInitialized. Under XCL such a title waited forever, or treated the network as not up.Both now write the token, then queue one callback on the queue's completion port through
xasync::submit_callback, asnotify_associationdoes for user-association changes. The connectivity callback carries the current hint fromXNetworkingGetConnectivityHint, and the port callback carriesDEFAULT_UDP_PORT. A null callback isE_INVALIDARG, as forXUserRegisterForDeviceAssociationChanged. If the queue refuses the callback, its record is freed and a warning is logged.What is in it
crates/xcl/src/hle/xgameruntime/networking.rs: the first callbacks; testregister_fires_an_initial_callback. It uses an immediate queue and checks that each callback runs once, withnetwork_initializedtrue and port 3074, and that a null callback is refused.Tested
cargo test --workspace -- --test-threads=4one2a4b1d: 742 passed, 0 failed, 41 ignored; no SKIPPED GPU testsleft: 0,right: 1; with it it passesRegression run
main:
this PR:
Not tested
XCL_NETWORK=0level (it comes fromXNetworkingGetConnectivityHint); not run together.AI use
🤖 Generated with Claude Code
Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T14:02:21Z.
Verification on RTX 3060 Ti (Windows 11), branch merged with current
main:Thanks!
upstream-sync referenced this pull request2026-11-24 19:06:45 +00:00
Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-26T04:17:25Z.
Re-verification on RTX 3060 Ti (Windows 11), new head merged with current
main:cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.Thanks!
— Devin
Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-11-30T08:42:51Z.
Nothing has happened on this PR for 3 days, so it is marked
staleand will be closed in 4 days. A push or a comment takes the mark off: rebase it ontomainif it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.Imported from ivall/xcl. Originally posted by Grimkaaah on 2026-11-30T12:05:24Z.
I compared this with the same registrations in my project. I checked it against the public XNetworking.h and the learn.microsoft.com pages for XNetworkingRegisterConnectivityHintChanged and XNetworkingUnregisterConnectivityHintChanged.
What I checked, all fine:
_In_.canceledis set.Findings:
Unregistering can't stop the initial callback. Both unregister functions still do nothing and return true (networking.rs:132 and :674). The initial callback now waits on the title's queue (a null queue means the process queue, a thread pool) until it is dispatched.
waitfalse, it returns false while a callback is running.context. The queued callback runs later with the freed context.Fix: keep a small table, token ->
Arc<{alive, threads running the callback, Condvar}>, and put the Arc inInitialNotification.alivebefore calling the title.alive.wait, it waits for a call that is running on another thread, but not for one on its own thread. The page says unregistering withwaitfrom inside the callback deadlocks.wait, it returns false while a call is running.This is how we do it.
A refused submit is ignored.
notify_initial(networking.rs:73-80) only logs it, and the register functions still return S_OK with a token. This happens with a null queue afterXTaskQueueSetCurrentProcessTaskQueue(nullptr), becausequeue_fromreturns None (E_INVALIDARG), and with a terminated queue (E_ABORT). The title then waits for a callback that never comes, which is the hang this PR fixes, with no error to show for it. Suggest returning the submit's HRESULT from the register function and not keeping the token. The docs don't say what the real runtime returns here; this is the choice we made.Not checked: we haven't built or run this PR. We didn't test a title that waits for the callback, and didn't check how it behaves together with #494.
AI use: Claude Code (Claude Opus 5.5) reviewed this by comparing it with the same mechanism in my project, Victory Grimkaaah.
Imported from ivall/xcl. Originally posted by BBareth on 2026-11-30T23:29:27Z.
merge-check claim
head:
e2a4b1d319Imported from ivall/xcl. Originally posted by BBareth on 2026-11-30T23:38:04Z.
merge-check
base:
d0832e614fhead:
e2a4b1d319merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2891 passed, 0 failed, 51 ignored; 28 SKIPPED lines, as on the base; failing: none. The base alone, run earlier today on the same commit: 2890 passed, 0 failed, 51 ignored, 28 SKIPPED lines (22 no wave64, 2 no heap windows, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 cannot disable the driver cache). The new test
register_fires_an_initial_callbackran and passed.review: no blocking finding; one file (+104/-9), every line read. The two register functions now queue one notification with the current value (UDP port 3074, the current connectivity hint) on the completion port of the queue they are given, write the token before queuing, and refuse a null callback with
E_INVALIDARG; a record the queue does not take is freed and a warning is logged. Two things to know, neither blocking: (1) the queued notification is not tied to the token, so a title that unregisters (even with wait = true) before its queue has dispatched it still gets the callback afterwards; with an immediate queue, as in the test, it runs inside the register call and this cannot happen; (2) a null callback was accepted before and is refused now. Not tested here: a retail title that waits for these callbacks.performance: none: one small allocation and one queued callback per registration.
verdict: safe to merge
hardware / tool and model: RTX 5070 Ti 16 GB, driver 621.24 (32.0.16.2124); i7-12700K; Windows 11 26200. Claude Code, Claude Fable 5.2 read the diff, ran the build and the suite and wrote this check.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T05:12:11Z.
Triage (#1607) – head
e2a4b1dNo open or merged PR makes the same change: main's
XNetworkingRegisterPreferredLocalUdpMultiplayerPortChanged/XNetworkingRegisterConnectivityHintChangedstill ignore the queue and callback.Overlap to know about: #1828 (tests only) asserts that both register functions return
S_OKwith a null callback, while this PR returnsE_INVALIDARGforcb == 0; whichever lands second has to reconcile that test.The callback signatures in
udp_port_changed_call/connectivity_hint_changed_callmatch the GDK ones, and the record is freed on both the run and the failed-submit paths.Recommendation: keep open.
AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
Imported from ivall/xcl. Originally posted by cj-tomlin on 2026-12-02T17:24:34Z.
Test report, head
e2a4b1d3190260b7a8799b916b5de5f7a5b69ab1merged ontomain5eaadb54c14e0fc29597b2a1689d7dbd83491303(clean, mergefdb0e78e).cargo build --workspace: ok.cargo test --workspace --no-fail-fast -- --test-threads=4: 2975 passed, 0 failed, 52 ignored; SKIPPED: 27 (22 no wave64, 2 no heap windows, 3 no D3D12 debug layer / d3d12SDKLayers.dll), the same 27 as plainmain(2974 passed, 0 failed, 52 ignored) on this machine.register_fires_an_initial_callbackpasses, and fails at its first call count (left 0, right 1) when I take the twonotify_initialcalls back out of the merged tree.Diff read (+104/-9,
xgameruntime/networking.rs), CPU only. Minor, not blocking: unregistering does not cancel an initial callback that is still queued, so on a non-immediate queue it can still run afterXNetworkingUnregister…Changed.Tool and model: Claude Code (Claude Opus 5.5) merged, built, ran the tests and drafted this; I checked it.
Imported from ivall/xcl. Originally posted by grathepic on 2026-12-02T22:33:43Z.
merge-check
base:
078358a058head:
e2a4b1d319merge: clean (
git diff --check origin/main..HEADclean)build:
cargo +1.98.1-x86_64-pc-windows-msvc build --offline --locked --workspace-> oktests:
cargo +1.98.1-x86_64-pc-windows-msvc test --offline --locked --workspace --no-fail-fast -- --test-threads=4(withXCL_DXC_DIR=C:\VulkanSDK\1.4.363.0\Bin,XCL_SAMPLE_*unset) -> 3007 passed, 0 failed, 52 ignored across 44 harnesses; SKIPPED: 27 notices (22no wave64, 2no heap windows, 1no GPU_UPLOAD heaps, 1no dxil.dll, 1cannot disable the driver cache (Windows Developer Mode required)); failing: none. New testhle::xgameruntime::networking::tests::register_fires_an_initial_callbackran and passed.title run: none (no retail title run on this machine; note this PR changes runtime code outside tests and the earlier
merge-checkalso lacks atitle run:past the loading screen)review: Full diff read (
+104/-9,crates/xcl/src/hle/xgameruntime/networking.rs). Blocking findings: (1) Use-after-free hazard on unregistration:XNetworkingRegisterPreferredLocalUdpMultiplayerPortChanged(networking.rs:118-130) andXNetworkingRegisterConnectivityHintChanged(networking.rs:676-690) now queue asynchronousInitialNotification<T>callbacks holding a rawctx: *mut c_voidviasubmit_callback, while bothXNetworkingUnregisterPreferredLocalUdpMultiplayerPortChanged(networking.rs:132) andXNetworkingUnregisterConnectivityHintChanged(networking.rs:692) remain unconditionaltrueno-ops. On any non-immediate queue (manual, serialized, thread-pool, ornullprocess queue), a title that callsUnregister(token, true)and freesctxcan have the queued or concurrently executing callback dereference the freedctx. Unlike XCL'snotify_association/XUserUnregisterForDeviceAssociationChangedpattern (xgameruntime/mod.rs:908-981) that the PR cites, there is no per-tokenCallState::{Queued, Running, Done}+Condvartracking to cancel queued callbacks, wait whenwait == true, or reportfalsewhenwait == falsewith a callback in flight. (2) Swallowed queue failures:notify_initial(networking.rs:73-80) logshr < 0(e.g.,E_ABORTon a terminated queue orE_INVALIDARGwhenqueueis null with no process task queue) and drops the error, so both register calls still returnS_OKwith a token even though the initial callback will never fire. (3) Test coverage gap:register_fires_an_initial_callbackonly exercisesMODE_IMMEDIATE, where the callback runs synchronously insideRegister, so neither post-unregister cancellation nor failed-submit handling is tested. (Also note the#1828null-callback test overlap noted in#1607triage.)performance: negligible (one small heap allocation and one queued callback per registration)
verdict: not yet: unregistration leaves queued/running initial callbacks uncanceled (use-after-free hazard on
ctxon non-immediate queues — track and cancel/wait per token likeXUserUnregisterForDeviceAssociationChangedinxgameruntime/mod.rs:908-981), failedsubmit_callbackresults should be propagated rather than returningS_OK(with manual-queue unregister and failed-submit tests), and a runtime PR needs atitle run:past the loading screen (#3041)hardware / tool and model: NVIDIA GeForce RTX 3080 (driver 32.0.16.2140), AMD Ryzen 7 5800X3D, Windows 11 (10.0.26300.0), Rust 1.98.1-x86_64-pc-windows-msvc, DXC 1.9.0.5512 (VulkanSDK, no dxil.dll) / Antigravity, Gemini 4 Neon
Imported from ivall/xcl. Originally posted by Pipodi on 2026-12-02T23:51:37Z.
merge-check
base:
078358a058head:
e2a4b1d319merge: clean (
git merge --no-ffonto078358a0, merge eca96dfa; xgameruntime/networking.rs +104/-9)build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 3007 passed, 0 failed, 52 ignored; failing: none (SKIPPED 25: 22 no wave64, 2 no heap windows, 1 cannot disable the driver cache). The new hle::xgameruntime::networking::tests::register_fires_an_initial_callback passes.
title run: retail title on NVIDIA GeForce RTX 4090, no save, into the in-engine intro cutscenes, 420 s (ended by XCL_RUN_SECONDS), same env and frame presses as a run of main
078358a0right after it. This PR: 336 s in the in-engine cutscenes (from the first 2 s window with over 5000 dispatches, at 95 s, to the end), 2.13 presents/s there, 2586 presents in the run, 0 device removals, 0 crashes (exit 0x0), peak video memory 21352 MB local, peak private 44 GB. Main078358a0: 334 s in the cutscenes (from 101 s), 2.03 presents/s, 2614 presents, 0 removals, exit 0x0, 21546 MB local, 43.6 GB. The first PR attempt was stopped by the warm gate (shader cache cold for the new binary), then one untimed warm-up run (exit 0x0) before the measured one. The log does not show whether the title registers either callback (nothing is logged when the initial callback is queued), so this run checks for regressions, not the fix. Title binary = merge result + local scripted-press commits (input only) and two local log-only diagnostics, the same stack as the main run.review: read the whole diff,
submit_callback/XTaskQueueSubmitCallbackand the device-association callbacks in xgameruntime/mod.rs. 1) Blocking, agreeing with the second check: both register functions now queue a callback that holds the title'sctx, butXNetworkingUnregisterPreferredLocalUdpMultiplayerPortChangedandXNetworkingUnregisterConnectivityHintChangedstaytrueno-ops. On any queue that does not run the completion port inline (a manual queue the title dispatches later, a thread-pool queue, the process queue), a title that unregisters and freesctxbefore the callback runs gets it called with a freedctx; withwait == truea running callback is not waited for either. XCL already keeps the opposite contract for the same kind of token inXUserUnregisterForDeviceAssociationChanged("No callback oftokenruns after this: one still queued is canceled (the title may free its context as soon as the call returns), and withwaita running one is waited for"), withCallStateQueued/Running/Done; the same for these two tokens (and a test on a manual queue: register, unregister, dispatch, no call) would fix it. Before this PR no callback was ever queued, so this is new exposure, though narrow: a title would have to unregister before its queue runs the initial callback. 2) Minor: when the callback cannot be queued (E_ABORTon a terminated queue), the register still returnsS_OKwith a token and only logs a warning; the record is freed, so no leak. 3) The callback cast and record lifetime are right: the record is freed in the call whether or not it is canceled, andconnectivity_hint_changed_callpasses a pointer to a hint that lives for the call. The test runs onMODE_IMMEDIATEonly.performance: none: one small allocation and one queued callback per registration, which a title makes once; nothing per frame. Presents and memory in the runs above are within the spread between runs of the same build here.
verdict: not yet: unregistering must cancel a still-queued initial callback and wait for a running one when
waitis true, asXUserUnregisterForDeviceAssociationChangeddoes, with a test on a non-immediate queue. The title run shows no regression on NVIDIA.hardware / tool and model: NVIDIA GeForce RTX 4090 24 GB, driver 621.40; Intel Core i9-13900K, 32 GB; Windows 11 build 26300; rustc 1.100.0, debug (tests) / release (title runs); Claude Code, Claude Opus 5.5
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.