Skip to content

xgameruntime: XErrorSetCallback sets the callback and its context as one pair (RT-258) - #3963

Open
traeb0n wants to merge 1 commit into
ItsNotRexed:mainfrom
traeb0n:xerror-callback-pair
Open

traeb0n wants to merge 1 commit into
ItsNotRexed:mainfrom
traeb0n:xerror-callback-pair

Conversation

@traeb0n

@traeb0n traeb0n commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Item: RT-258 (claim: #3796 (comment)), found by @shdann (finding).

What and why

error_set_callback stored the context, then the callback, in two separate atomics, and error_report loaded them one after the other. With (cbA, ctxA) registered and another thread re-registering (cbB, ctxB), a report between the two stores called cbA(hr, message, ctxB).

The pair is now one ErrorHandler {callback, context}, written and read under a mutex. error_report copies it under the lock and calls the callback after releasing it, so a callback may report or register again without deadlocking. Reports are rare (XCurl's errors), so the lock costs nothing that matters. I used a mutex rather than a 16-byte std::atomic, which can need cx16 on the clang-cl cross-build.

Test

xgameruntime_test: one thread switches the registration between two (callback, context) pairs 200,000 times while the test thread reports, and each callback checks that it got its own context.

Tested ec4ef29 on its base main aa2988c: Windows 11, i7-14700K, MSVC 14.44, RelWithDebInfo:

  • ctest -E loader -j 4: 141/141 passed, xgameruntime included.
  • The same test built against main's xgameruntime.cpp fails every time:
      30115 of 186605 reports paired a callback with the other context
      34394 of 228053 reports paired a callback with the other context
      60756 of 201685 reports paired a callback with the other context
    FAIL XErrorReport never calls one registration's callback with another's context
    
    With this PR: 0 every time.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

…one pair (RT-258)

error_set_callback stored the context, then the callback, in two atomics, and
error_report loaded them one after the other. A report between the two stores
called the old callback with the new context. The pair now sits in one struct
read and written under a mutex; the callback runs after the lock is released,
so it may report or register again.

xgameruntime_test switches the registration between two (callback, context)
pairs 200,000 times on one thread while another reports, and each callback
checks it got its own context. With main's shim 15-30% of reports were mixed
(30115 of 186605, 34394 of 228053, 60756 of 201685 in three runs); with this,
none. Found by @shdann.

AI-assisted (Claude Code).

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

mrp1no commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Checked ec4ef29's shim against an extra race test, on Windows 11 with MSVC 19.52 (RelWithDebInfo, CPU only). It's the test from my closed duplicate #4002: two threads re-register pair A (callback A, context A) or pair B 200,000 times each, while two threads call XErrorReport 200,000 times each, and every callback checks it received its own context.

shim wrong pairings out of 400,000 (3 runs)
main 106,864 / 159,397 / 132,902
this PR 0 / 0 / 0

So the fix holds under contention. If you want the check in-tree, it's 40 lines in xgameruntime_test.cpp and I'm happy to send it. AI-assisted (Claude Code).

@FIocker

FIocker commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Independent Windows re-entry check of ec4ef295 with MSVC 19.44, AI-assisted. I added a synthetic case in which the first IXErrorImpl callback registers a second callback with a distinct context, then reports a nested error. Each callback ran once and both reports returned S_OK. The extended xgameruntime_test passed all 108 checks in five consecutive runs, including the existing callback pairing test. The identical test aborts with Windows status 0xc0000409 against ead8072d from #3927, which holds the mutex while invoking the callback. I have a test-only patch for the re-entry case.

@evilclanker9000

Copy link
Copy Markdown
Contributor

Tested ec4ef29 on Windows 11 (26200), MSVC (VS 2022 Build Tools) RelWithDebInfo, with RTX 5080 (driver 616.92) and AMD Radeon iGPU gfx1036 (driver 26.10.1) each selected in turn (LEONIDA_VK_DEVICE, VK_LOADER_LAYERS_DISABLE=~implicit~; the test itself needs no GPU): ctest -R "^xgameruntime$" — pass, 1/1 per run, xgameruntime_test: OK (107 checks).

No negative control run.

AI-assisted.

@fladit123

Copy link
Copy Markdown
Contributor

Hi! I'm a new contributor working with AI assistance on Windows with an NVIDIA RTX 4060.

I noticed that RT-258 already has ongoing implementations, so I don't want to duplicate the fix.

I was wondering whether a deterministic regression test for the XError callback/context race would be useful.

The idea would be to control thread interleaving during callback registration and reporting, ensuring that a callback never receives the context belonging to another registration.

I could also check reentrant callback behavior and potential deadlocks.

Would you be interested in an additional test contribution? If so, should I work against this PR or coordinate a separate change?

I would coordinate the work before submitting anything. No game files would be used.

Thanks!

@ricardotanaka

Copy link
Copy Markdown

Also tested ec4ef295 (merged onto main aa2988cc) on AMD Radeon RX 7600 (RDNA3), AMD driver 26.11.2, Windows 11 26200.9457, MSVC 19.44 RelWithDebInfo: pass, same as the checks above. xgameruntime passes, also with LEONIDA_VK_VALIDATION=1; full ctest 153/153.

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

@cynbun

cynbun commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

@traeb0n, a heads-up from a code read of main 8575d36ed (not a claim; this stays yours):

AI-assisted (Claude Code).

cc @shayderrr

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.

7 participants