[PR #156] fix(xgameruntime): release g_error_mutex before invoking XErrorCallback in XErrorReport #208

Open
opened 2026-11-26 20:17:08 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/156
Author: @Andretuta
Created: 11/24/2026
Status: 🔄 Open

Base: main ← Head: old-pr/9887


📝 Commits (1)

  • 52a8807 fix(xgameruntime): release g_error_mutex before invoking XErrorCallback in XErrorReport

📊 Changes

2 files changed (+49 additions, -2 deletions)

View changed files

📝 runtime/shims/xgameruntime.cpp (+8 -2)
📝 runtime/test/xgameruntime_test.cpp (+41 -0)

📄 Description

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).

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/156 **Author:** [@Andretuta](https://github.com/Andretuta) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `old-pr/9887` --- ### 📝 Commits (1) - [`52a8807`](https://github.com/ubiquitarianism/leonida/commit/52a88078219ba07a8dac12f49794c6e6df724942) fix(xgameruntime): release g_error_mutex before invoking XErrorCallback in XErrorReport ### 📊 Changes **2 files changed** (+49 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `runtime/shims/xgameruntime.cpp` (+8 -2) 📝 `runtime/test/xgameruntime_test.cpp` (+41 -0) </details> ### 📄 Description > 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). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#208
No description provided.