Skip to content

xgameruntime_user: registered user change callbacks are kept and called through their task queue - #8422

Closed
tedin7 wants to merge 4 commits into
ProjectLeonida:mainfrom
tedin7:c2-xuser-change-events
Closed

tedin7 wants to merge 4 commits into
ProjectLeonida:mainfrom
tedin7:c2-xuser-change-events

Conversation

@tedin7

@tedin7 tedin7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

What

Deliver XUser change events to registered callbacks on their task queues and support unregistration.

Why

Slots 33–34 need to retain registrations and dispatch offline user changes when raised. This change was AI-assisted.

Tested

On p2, /root/fast_test_exe.sh /root/c2-xuce xgameruntime_user_test: origin/main shim with a no-op test hook failed 2 of 47 checks; the initial change passed 47 of 47. Review fixes based on 1691100: /root/fast_test_exe.sh /root/c2-xuce2 xgameruntime_user_test passed 58 of 58, including a disabled process queue, queued events unregistered with either wait setting, and nested dispatch unregistration. Windows executable under Wine; no GPU or driver used.

@ItsNotRexed: an exe PR, as you asked.

Claim: #8344 (comment)

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

At 1dc4af7, events raised after a callback unregisters itself still allocate notifications and fill its queue until the callback returns. On a manual completion queue, 4,096 such events require 4,096 extra dispatches. None of those dispatches calls the user callback.

The unregister call marks the registration inactive. The running callback keeps the entry in g_tokens. Under the same mutex, raise_user_change copies that inactive entry into its snapshot. It then allocates and submits a UserNotification for it. The dispatch path checks active only after the item leaves the queue.

Please include only active registrations when taking the snapshot under g_tokens_mutex. Keep the dispatch-time check for work submitted before unregister. With this change, my probe reports one user callback and zero extra dispatches. All three registered user tests pass on Windows 11.

Manual-queue reproduction

Insert this block just before XTaskQueueCloseHandle(manual); in runtime/test/xgameruntime_user_test.cpp. Build xgameruntime_user_test and run ctest -C RelWithDebInfo -R "^xgameruntime_user$" -V. On the PR head all existing checks pass, but the new queue check fails.

    {
    struct Probe {
        void* user;
        Unregister remove;
        XTaskQueueRegistrationToken token{};
        int calls{};
    } probe{user, at<Unregister>(user, 34)};
    auto self_remove = [](void* ctx, XUserLocalId, uint32_t) {
        auto& p = *static_cast<Probe*>(ctx);
        ++p.calls;
        p.remove(p.user, p.token, false);
        for (int i = 0; i < 4096; ++i)
            rt::shims::xgameruntime_user_raise_change_for_test(0);
    };
    check(at<Register>(user, 33)(user, manual, &probe, self_remove, &probe.token) == S_OK, "register probe");
    rt::shims::xgameruntime_user_raise_change_for_test(0);
    XTaskQueueDispatch(manual, XTaskQueuePort::Completion, 0);
    int extra = 0;
    while (XTaskQueueDispatch(manual, XTaskQueuePort::Completion, 0)) ++extra;
    std::printf("probe: callbacks=%d extra dispatches=%d\n", probe.calls, extra);
    check(probe.calls == 1 && extra == 0, "no queue work for events raised after unregister");
    }

AI-assisted.

@tupacfan04 tupacfan04 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The core registration/dispatch logic looks sound (58 checks, CI queued), but the PR's new blocking-wait and queue-lifetime behavior is not exercised, and two messages still describe the old behavior.

  1. The blocking wait is never entered. runtime/shims/xgameruntime_user.cpp:372-383:
registration->active = false;
if (wait && !inside_user_dispatch(registration.get()))
    g_tokens_cv.wait(lock, [&] { return registration->running == 0; });
const bool done = registration->running == 0;

Every wait = true call in the test avoids the wait: event_token is unregistered after its callback ran (running == 0); the queued cases are unregistered before dispatch; the nested case unregisters the outer token from inside the outer callback's dispatch (inside_user_dispatch true, so the wait is skipped, and the test only checks the immediate false). A lost wakeup, wrong predicate or missing notify would pass all 58 checks. Add a callback blocked on an event on a worker/ThreadPool-dispatched queue and a second thread calling unregister(token, true); assert it blocks until the callback is released, then returns true. Also check deferred completion of the nested token: a later unregister(nested.outer, true) should return true and a subsequent raise should be silent; a broken erase in UserNotification::run would currently pass.

  1. The process queue and queue-handle lifetime are not tested. All deliveries use Manual queues, and the test keeps the handles open until every registration is gone, so XTaskQueueDuplicateHandle and the UserRegistration destructor's XTaskQueueCloseHandle are never the last handle. The process-queue test only covers the disabled queue. Add a registration with the default ThreadPool process queue (or an explicit queue), close the caller's handle, raise and verify delivery, and/or check XTaskQueueUninitialize after all registrations are gone.

  2. Stale docs/test message. docs/runtime_imports.md:377 still says change callbacks are “only tracked as tokens,” and :383 says “Unregistering a change callback is never pending, so it returns true for any token.” Both are false now: callbacks are delivered, and the code returns false when a callback is running with wait = false or when called from inside dispatch. runtime/test/xgameruntime_user_test.cpp:166-169 still says “false would mean still pending, which never happens offline,” while the new nested test at :225 expects false. Update all three.

Synthetic only; no game files. AI-assisted (OpenCode).

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Re-tested dcf6cff on Windows 11. The fix resolves my queue-growth finding. In the original manual-queue probe, the callback unregisters itself with wait=false and raises 4,096 events before it returns. At 1dc4af7, the probe records one callback and 4,096 extra dispatches. At dcf6cff, it records one callback and zero extra dispatches.

I built xgameruntime_user_test in RelWithDebInfo and ran ctest -C RelWithDebInfo --timeout 30 -R "^xgameruntime_user" -V. All three variants passed all 70 checks with the probe included. This includes the new tests that unregister from a separate thread while the callback runs.

AI-assisted.

@comtek21516

Copy link
Copy Markdown

Tested dcf6cff46 on main at 867d922f0, in one tree with #8669, #8670, #8690, #8770, #8784 (six clean merges): pass.

For this change. xgameruntime_user (with the new callback cases), xgameruntime_user_gamertag and xgameruntime_user_gamertag_long pass natively and under Wine.

#8673 could not be put in the same tree: it conflicts with this PR in runtime/test/xgameruntime_user_test.cpp, so whichever lands second needs a rebase there.

Setup. Built with MSVC 14.52 (Visual Studio 18), Windows SDK 10.0.26100, Vulkan SDK 1.4.363.1, Ninja RelWithDebInfo on Windows Server 2025 (no Vulkan driver there), then the same binaries run through scripts/run_wine_tests.py under wine-11.0 (WineHQ stable, xvfb-run) on a Quadro RTX 4000 (Turing), NVIDIA 615.71.09, Ubuntu 22.04.5.

passed failed skipped
native Windows, this tree 147 4 124
Wine on the GPU machine, main 867d922f0 210 12 54
Wine on the GPU machine, this tree 210 12 54

Under Wine the tree is test by test the same as main. The four native failures are gameinput_host, d3d12_sparse_queue_family, vk_bindless_ps_tex and vk_bindless_ps_imgatomic, which fail the same way on main on that machine (#7452). The 12 under Wine are the same 12 as on main (host comparisons Wine cannot answer, and d3d12_xdxr_rt, d3d12_indirect_rt, d3d12_indirect_dispatch_count).

Because the six were built together, there is no per-PR negative control in this run: a regression would have shown as a difference from main, and there is none. No game files used. AI-assisted (Claude Code).

@DeSunShine

Copy link
Copy Markdown
Contributor

Tested dcf6cff46 merged into main 6312c057f on NVIDIA GeForce RTX 4090 (driver 621.24), Windows 11 Pro 26200, MSVC (VS Build Tools 18) + Ninja, Vulkan SDK 1.4.363: pass.

  • ctest -R xgameruntime --output-on-failure: 8/8 passed (xgameruntime, _user, _gamesave, _store, _offline, _offline_hdr, _user_gamertag, _user_gamertag_long).

AI-assisted (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants