Skip to content

xgameruntime: the XError callback and its context are one registration (RT-258) - #4002

Closed
mrp1no wants to merge 1 commit into
ItsNotRexed:mainfrom
mrp1no:rt-258-xerror-callback-pair
Closed

mrp1no wants to merge 1 commit into
ItsNotRexed:mainfrom
mrp1no:rt-258-xerror-callback-pair

Conversation

@mrp1no

@mrp1no mrp1no commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

RT-258, claimed in #3918 (claim), @shdann's finding.

What was wrong

runtime/shims/xgameruntime.cpp kept the XErrorSetCallback callback and its context in two std::atomics. error_report loaded them one after the other, so a report racing a re-registration on another thread could pair one registration's callback with the other's context.

The change

  • One registration: the callback and context form one ErrorCallback pair under a std::mutex, and XErrorSetCallback replaces it whole.
  • Reporting: XErrorReport copies the pair under the lock and calls the callback outside it, so a callback may report again or re-register without deadlocking.

Tested

Windows 11 Pro 26300, MSVC 19.52, RelWithDebInfo, CPU only:

  • xgameruntime_test has a new check. Two threads re-register pair A or pair B 200,000 times each while two threads report 200,000 times each, and each callback checks it received its own context:
    • with this change: 0 wrong of 400,000, in three runs out of three, and the rest of xgameruntime_test still passes.
    • Negative control: with the old shim, 106,864, 159,397 and 132,902 of 400,000 callbacks got the other pair's context, and the test exits 1.
  • No new warnings in the changed files. The remaining C4456 at xgameruntime_test.cpp:532 is already on main.

No game files. AI-assisted (Claude Code).

🤖 Generated with Claude Code

…read as one pair (RT-258)

XErrorSetCallback stored the callback and its context in two separate
atomics, and XErrorReport loaded them separately. A report racing a
re-registration on another thread could therefore call one callback with
the other registration's context (found by @shdann). The pair now lives
under one mutex: XErrorSetCallback replaces it whole, and XErrorReport
copies it under the lock and calls the callback outside it, so a callback
may report again or re-register.

xgameruntime_test now re-registers two pairs from two threads while two
other threads report 400,000 times, and checks every callback got its own
context. With the old shim, 106,864 to 159,397 of the 400,000 callbacks got
the other pair's context in three runs. With this change none did.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ray3242

ray3242 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

RT-258 already has two open fixes. #3963 is from @traeb0n, who claimed RT-258 first and whom the coordinator confirmed as owner. #3927 is from @x2Tweek, and an independent Windows/MSVC check of it is posted there. You claimed RT-258 at 22:49 UTC, after both had opened, and the coordinator asked you to review one of them instead (#3918).

A trial git merge of this PR (c19e1dec) onto #3963 (ec4ef295) conflicts in runtime/shims/xgameruntime.cpp and runtime/test/xgameruntime_test.cpp. #3927 (ead8072d) onto #3963 also conflicts in xgameruntime.cpp. Your 400,000-iteration race check could go to #3963 as a review or as a test.

One correction for the thread: the coordinator's note swaps the authors. #3927 is @x2Tweek's and #3963 is @traeb0n's.

I did not build or run these runtime tests. AI-assisted.

@mrp1no

mrp1no commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

You're right, @ray3242, sorry. I claimed RT-258 without checking for open PRs, and #3963 (@traeb0n, the owner) and #3927 were already up. I'm closing this as a duplicate. In case it helps whichever one lands: the multi-thread check here (two threads re-registering two pairs while two report, 400,000 calls) caught 106k-159k mismatched pairings on the old shim in three of three runs. I'll post it on #3963 as a review note rather than as a third fix.

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.

2 participants