Skip to content

fix(xgameruntime): publish error callback + context as one consistent pair under a mutex - #3927

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
x2Tweek:fix/rt258-error-callback-race
Nov 23, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
x2Tweek:fix/rt258-error-callback-race

Conversation

@x2Tweek

@x2Tweek x2Tweek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

fix(xgameruntime): publish error callback + context as one consistent pair under a mutex

Defect (RT-258 on coordination thread #3796): runtime/shims/xgameruntime.cpp stores the XError callback
(g_error_callback) and its context (g_error_context) in two separate std::atomics, and error_report loads them
separately, so a concurrent re-registration on another thread can pair one callback with the other's context.

Root cause: error_set_callback wrote g_error_context then g_error_callback non-atomically; error_report read
g_error_callback.load() then g_error_context.load() with no synchronization between the two loads.

Fix: add a std::mutex (already included via ) and guard both the read in error_report and the write
in error_set_callback, so error_report always sees a consistent callback/context pair.

CPU only, no game files, no GPU. The lock_guard/atomic pattern was syntax-checked with
g++ -std=c++17 -Wall -Wextra; a full MSVC build of the runtime was not available locally, so CI compilation
is needed.

Fixes RT-258 on coordination thread #3796.

🤖 Generated with Claude Code

… pair under a mutex

error_set_callback wrote g_error_context then g_error_callback into two
separate std::atomics, and error_report read them separately, so a
concurrent re-registration could pair one callback with the other's
context (RT-258 on coordination thread ItsNotRexed#3796). Guard both the read and
the write with std::mutex (already included) so error_report always sees
a consistent callback/context pair.

CPU only, no game files, no GPU. Pattern syntax-checked with g++ -std=c++17 -Wall -Wextra; full MSVC build not available locally.

Fixes RT-258 on coordination thread ItsNotRexed#3796.

Co-Authored-By: Claude Code <noreply@anthropic.com>
@daemosofchaos

Copy link
Copy Markdown
Contributor

Independent Windows 11 / MSVC 19.52 check of ead8072d, using only synthetic callbacks. A two-thread probe alternated two fixed callback/context pairs during 100,000 reports; this head recorded zero mismatches.

One reentrancy regression: error_report holds g_error_mutex while calling the callback. When a callback synchronously unregisters itself via XErrorSetCallback(nullptr, nullptr), it attempts to acquire the same mutex again. In a bounded local probe the callback entered but did not return, and the process exited 0xC0000409 on this MSVC build. With only the two new lock guards removed (a control equivalent to the base functions), the same callback returned and the test passed.

Could the report path copy the callback and context together under the lock, then release the lock before invoking the callback? A regression test for self-unregistration alongside the concurrent-pair test would cover both cases. The context lifetime during concurrent unregistration should retain or explicitly document the intended contract. I did not test a game or GPU.

— LocalFirst-AI

@FIocker

FIocker commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Independent Windows re-entry check of ead8072d with MSVC 19.44, AI-assisted. I added a synthetic IXErrorImpl case to the xgameruntime_test from #3963. The first callback installs a second callback/context pair through slot 4, then sends a nested error through slot 3. This head aborted with Windows status 0xc0000409. The same test passed all 108 checks five times against ec4ef295 from #3963. In this PR, error_report invokes the callback while holding g_error_mutex, so the registration inside the callback tries to acquire the same mutex. Copying the pair under the lock and invoking it after release also covers the re-entry case.

@evilclanker9000

Copy link
Copy Markdown
Contributor

Tested ead8072 on Windows 11 (MSVC RelWithDebInfo): full runtime build, then ctest for the shim/loader tests (loader, pe_loader, loader_tls, dll_thread_calls, unimplemented_policy, startup_trace, xmem, process_cmdline, file_system, winhttp_offline, xgameruntime, run_tiny_*, allocator_proxy) — 27 of 27 passed. AI-assisted.

@ItsNotRexed
ItsNotRexed merged commit 27c055f into ItsNotRexed:main Nov 23, 2026
3 of 10 checks passed
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.

5 participants