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

Open
github-bot wants to merge 1 commit from github-bot/gh-156 into main AGit
Owner

@⁠Andretuta on GitHub · 2026-11-24 02:31 UTC · original
ubiquitarianism:old-pr/9887 → main · +49 −2 · 2 files · 1 commits · draft

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).
**@⁠Andretuta** on GitHub · 2026-11-24 02:31 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/156) `ubiquitarianism:old-pr/9887` → `main` · +49 −2 · 2 files · 1 commits · draft > 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).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:46:46 +00:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/156/head:github-bot/gh-156'
git switch 'github-bot/gh-156'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-156'
git switch 'github-bot/gh-156'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-156'
git switch 'github-bot/gh-156'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-156'
git switch 'main'
git merge --squash 'github-bot/gh-156'
git switch 'main'
git merge --ff-only 'github-bot/gh-156'
git switch 'main'
git merge 'github-bot/gh-156'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.