fix(xgameruntime): release g_error_mutex before invoking XErrorCallback in XErrorReport #156

Closed
Andretuta wants to merge 1 commit from upstream-pr/156 into main
Andretuta commented 2026-11-24 02:31:18 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Andretuta on 2026-11-24T02:31:18Z.


Carried over from old PR 9887 by totonicpu in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch old-pr/9887, exactly that PR's last commit. All carried-over PRs are listed in #17.
Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it.
totonicpu: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one.


Original description (mentions and #N references are disabled; the numbers refer to the old repository):

Summary

Fixes a re-entrancy deadlock / lock-order inversion in error_report() (runtime/shims/xgameruntime.cpp:247-248):

  • g_error_mutex (non-recursive std::mutex) was held across the title's XErrorCallback invocation. If the callback re-entrantly calls XErrorReport or calls XErrorSetCallback (e.g., to clear/swap its handler), or acquires a title lock that another thread holds around XErrorSetCallback, it immediately deadlocks (on MSVC std::mutex, fast-failing with resource_deadlock_would_occur / 0xc0000409).
  • Snapshots {g_error_callback, g_error_context} as one consistent pair under g_error_mutex, releases the lock, and then invokes cb(hr, message, ctx).

Testing

  • Fail-first proof: Added a re-entrancy test in runtime/test/xgameruntime_test.cpp where the XErrorCallback calls XErrorReport recursively and clears itself via XErrorSetCallback(nullptr, nullptr). Before the fix, xgameruntime crashed immediately with Exit code 0xc0000409 (resource_deadlock_would_occur).
  • Build (Debug & RelWithDebInfo, MSVC /W4): 0 new warnings in xgameruntime.cpp and xgameruntime_test.cpp.
  • CTest (Debug & RelWithDebInfo): ctest -R '^xgameruntime$' --output-on-failure — 100% passed (0.23s RelWithDebInfo, 0.12s Debug).
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5464971323 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/156). Originally posted by [Andretuta](https://github.com/Andretuta) on 2026-11-24T02:31:18Z. --- > Carried over from **old PR 9887** by `totonicpu` in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch `old-pr/9887`, exactly that PR's last commit. All carried-over PRs are listed in #17. > Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it. > `totonicpu`: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one. --- Original description (mentions and `#N` references are disabled; the numbers refer to the old repository): ## Summary Fixes a re-entrancy deadlock / lock-order inversion in `error_report()` (`runtime/shims/xgameruntime.cpp:247-248`): - `g_error_mutex` (non-recursive `std::mutex`) was held across the title's `XErrorCallback` invocation. If the callback re-entrantly calls `XErrorReport` or calls `XErrorSetCallback` (e.g., to clear/swap its handler), or acquires a title lock that another thread holds around `XErrorSetCallback`, it immediately deadlocks (on MSVC `std::mutex`, fast-failing with `resource_deadlock_would_occur` / `0xc0000409`). - Snapshots `{g_error_callback, g_error_context}` as one consistent pair under `g_error_mutex`, releases the lock, and then invokes `cb(hr, message, ctx)`. ## Testing - **Fail-first proof:** Added a re-entrancy test in `runtime/test/xgameruntime_test.cpp` where the `XErrorCallback` calls `XErrorReport` recursively and clears itself via `XErrorSetCallback(nullptr, nullptr)`. Before the fix, `xgameruntime` crashed immediately with `Exit code 0xc0000409` (`resource_deadlock_would_occur`). - **Build (`Debug` & `RelWithDebInfo`, MSVC `/W4`):** 0 new warnings in `xgameruntime.cpp` and `xgameruntime_test.cpp`. - **CTest (`Debug` & `RelWithDebInfo`):** `ctest -R '^xgameruntime$' --output-on-failure` — 100% passed (0.23s RelWithDebInfo, 0.12s Debug).
fiestorra commented 2026-11-30 09:47:23 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-11-30T09:47:23Z.


Independent verification on this machine (Windows 11, RTX 4090, MSVC 19.44.35226), with a negative control that bites hard.

1. It applies. merge-tree clean; the commits cherry-pick onto main 4014af7fe without a conflict. Two files: runtime/shims/xgameruntime.cpp and runtime/test/xgameruntime_test.cpp.

2. With the branch, all eight tests pass:

ctest -R '^xgameruntime'
100% tests passed, 0 tests failed out of 8

3. The control - revert only the shim, keep the test:

git checkout origin/main -- runtime/shims/xgameruntime.cpp
cmake --build … --target xgameruntime_test
ctest -R '^xgameruntime'
88% tests passed, 1 tests failed out of 8
153 - xgameruntime (Exit code 0xc0000409)

0xC0000409 is STATUS_STACK_BUFFER_OVERRUN - the /GS cookie check firing on actual stack corruption, not a failed assertion and not a timeout. So the change is not cosmetic: without it, this test walks off the stack.

4. What the change is, and why that failure follows from it: the branch stops reading the callback and context inside the same locked region and takes a copy before calling out:

XErrorCallback cb = nullptr;
void* ctx = nullptr;
…
if (XErrorCallback cb = g_error_callback.load()) cb(hr, message, g_error_context.load());

Calling a user callback while holding the error mutex, and reading g_error_context again at call time, is what the control turns into a crash here.

What I tested and what I did not: the eight tests of that target, twice (branch green, shim reverted crash). I did not run the rest of the suite with the shim reverted, so I cannot say whether other tests would also have caught it - only that this one does.

Why this one is worth a reviewer's minute: the diff is 30 lines in a shim, it applies cleanly after five days, its tests are green, and the control converts a passing test into a stack-overrun crash. That is the strongest evidence I have produced on any of the 122 shortlist pull requests.

Written with an AI assistant.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7274687211 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/156#issuecomment-7274687211). Originally posted by [fiestorra](https://github.com/fiestorra) on 2026-11-30T09:47:23Z. --- Independent verification on this machine (Windows 11, RTX 4090, MSVC 19.44.35226), with a negative control that bites hard. **1. It applies.** `merge-tree` clean; the commits cherry-pick onto `main` `4014af7fe` without a conflict. Two files: `runtime/shims/xgameruntime.cpp` and `runtime/test/xgameruntime_test.cpp`. **2. With the branch, all eight tests pass:** ``` ctest -R '^xgameruntime' 100% tests passed, 0 tests failed out of 8 ``` **3. The control - revert only the shim, keep the test:** ``` git checkout origin/main -- runtime/shims/xgameruntime.cpp cmake --build … --target xgameruntime_test ctest -R '^xgameruntime' 88% tests passed, 1 tests failed out of 8 153 - xgameruntime (Exit code 0xc0000409) ``` **`0xC0000409` is `STATUS_STACK_BUFFER_OVERRUN`** - the `/GS` cookie check firing on actual stack corruption, not a failed assertion and not a timeout. So the change is not cosmetic: **without it, this test walks off the stack**. **4. What the change is, and why that failure follows from it:** the branch stops reading the callback and context inside the same locked region and **takes a copy before calling out**: ```cpp XErrorCallback cb = nullptr; void* ctx = nullptr; … if (XErrorCallback cb = g_error_callback.load()) cb(hr, message, g_error_context.load()); ``` Calling a user callback **while holding the error mutex**, and reading `g_error_context` again at call time, is what the control turns into a crash here. **What I tested and what I did not:** the eight tests of that target, twice (branch green, shim reverted crash). **I did not run the rest of the suite with the shim reverted**, so I cannot say whether other tests would also have caught it - only that this one does. **Why this one is worth a reviewer's minute:** the diff is **30 lines in a shim**, it applies cleanly after five days, its tests are green, **and the control converts a passing test into a stack-overrun crash**. That is the strongest evidence I have produced on any of the 122 shortlist pull requests. Written with an AI assistant.
MoonEyes commented 2026-11-30 19:29:31 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-30T19:29:31Z.


The independent Windows negative-control report is strong: it demonstrates the re-entrancy test fails with the old shim and passes with this change. Merge criteria are still incomplete, though: this PR is a draft and all current CI checks are cancelled. Please rebase/confirm a clean current base, rerun the Windows/runtime and CI checks, and mark it ready before merge.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7289480428 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/156#issuecomment-7289480428). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-30T19:29:31Z. --- The independent Windows negative-control report is strong: it demonstrates the re-entrancy test fails with the old shim and passes with this change. Merge criteria are still incomplete, though: this PR is a draft and all current CI checks are cancelled. Please rebase/confirm a clean current base, rerun the Windows/runtime and CI checks, and mark it ready before merge.
aslan2142 commented 2026-12-01 12:12:00 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T12:12:00Z.


Marking Ready for Review since no WIP/blocker reason for draft status is specified on this PR, so it can enter normal CI and review.

(AI-assisted triage.)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7310793057 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/156#issuecomment-7310793057). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T12:12:00Z. --- Marking Ready for Review since no WIP/blocker reason for draft status is specified on this PR, so it can enter normal CI and review. *(AI-assisted triage.)*
aslan2142 commented 2026-12-01 16:26:14 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T16:26:14Z.


Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7317603370 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/156#issuecomment-7317603370). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:26:14Z. --- Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!156
No description provided.