Async callback and handle safety in xgameruntime, gameinput_no_sdl and xapu_queue (RT-ASYNC-CALLBACK-HANDLE-SAFETY) #2545

Merged
leonida/mandy
Tyrannys merged 1 commit from rt-async-callback-handle-safety into main 2026-11-28 17:50:41 +00:00

Pull request successfully merged and closed

Tyrannys commented 2026-11-28 01:40:26 +00:00

Claimed in the coordination issue as: RT-ASYNC-CALLBACK-HANDLE-SAFETY (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7231782130)

What and why

This fixes item 5 of kevincommits' Finding 19 in #1608 (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7231649637): three places where a callback or a handle could be used after its owner let it go.

  • xgameruntime.cpp (XNetworking). Say an initial notification's callback unregisters itself with wait=true, on the callback's own thread. The unregister then waited for the registration to end, which only happens after the callback returns, so it waited forever. The registration now records the thread running its callback, and an unregister from that thread returns at once with true. The registration still ends when the callback returns. Also, net_register used a throwing new across the ABI; it is new (std::nothrow) now.
  • gameinput_no_sdl.cpp. The GameInputBlockingEnumeration report ran outside deliver_mu. So UnregisterCallback on another thread could return while the callback was still running, and the caller could free the callback's context mid-call. The report now runs under deliver_mu and marks the thread as delivering, the same way deliver() runs callbacks. So another thread's unregister waits for it, and the callback can still unregister itself.
  • xapu_queue.cpp.
    • Handles were never checked, so a second XApuDisconnect freed the connection again and closed its event twice. Connections are now kept in a live set under a std::shared_mutex. Every call checks its handle and holds the lock shared for the whole call, and a disconnect takes it exclusive. So a stale or foreign handle gets E_INVALIDARG (or a count of 0), and a disconnect waits for calls in flight.
    • An activate or deactivate changed the stream before the result was queued, so running out of memory left the command half applied. The result's slot is now taken first.
    • A freed handle whose address a new connection reuses still looks live. Raw pointers cannot tell the two apart.

xgameruntime.cpp is also edited by our #2520, in other functions; the two merge cleanly. #2497 edits gameinput.cpp, not gameinput_no_sdl.cpp.

Tested

New checks:

Test New check On main
xgameruntime_test A port-changed callback that unregisters itself with wait=true returns. The queue is dispatched on a separate thread, so a hang shows as a failed check. fails
gameinput_no_sdl_test (script mode) While a blocking-enumeration callback is held open on one thread, UnregisterCallback from another thread has not returned after 200 ms. It returns true once the callback ends. A blocking callback can also unregister itself. fails
xapu_chad_test A second disconnect, a disconnected handle and a foreign handle are all refused. crashes with heap corruption (0xc0000374)

With the fix, ctest -R "^xgameruntime$|gameinput_no_sdl|xapu" passes 6/6.

GPUs tested: profile M: NVIDIA GeForce RTX 4070 SUPER, 12 GB, desktop, driver 621.01, Windows 11 (CPU-only tests)

AI-assisted: yes (Claude Code)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

🤖 Generated with Claude Code

Claimed in the coordination issue as: RT-ASYNC-CALLBACK-HANDLE-SAFETY (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7231782130) ### What and why This fixes item 5 of kevincommits' Finding 19 in #1608 (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7231649637): three places where a callback or a handle could be used after its owner let it go. - **`xgameruntime.cpp` (XNetworking).** Say an initial notification's callback unregisters itself with `wait=true`, on the callback's own thread. The unregister then waited for the registration to end, which only happens after the callback returns, so it waited forever. The registration now records the thread running its callback, and an unregister from that thread returns at once with `true`. The registration still ends when the callback returns. Also, `net_register` used a throwing `new` across the ABI; it is `new (std::nothrow)` now. - **`gameinput_no_sdl.cpp`.** The `GameInputBlockingEnumeration` report ran outside `deliver_mu`. So `UnregisterCallback` on another thread could return while the callback was still running, and the caller could free the callback's context mid-call. The report now runs under `deliver_mu` and marks the thread as delivering, the same way `deliver()` runs callbacks. So another thread's unregister waits for it, and the callback can still unregister itself. - **`xapu_queue.cpp`.** - Handles were never checked, so a second `XApuDisconnect` freed the connection again and closed its event twice. Connections are now kept in a live set under a `std::shared_mutex`. Every call checks its handle and holds the lock shared for the whole call, and a disconnect takes it exclusive. So a stale or foreign handle gets `E_INVALIDARG` (or a count of 0), and a disconnect waits for calls in flight. - An activate or deactivate changed the stream before the result was queued, so running out of memory left the command half applied. The result's slot is now taken first. - A freed handle whose address a new connection reuses still looks live. Raw pointers cannot tell the two apart. `xgameruntime.cpp` is also edited by our #2520, in other functions; the two merge cleanly. #2497 edits `gameinput.cpp`, not `gameinput_no_sdl.cpp`. ### Tested New checks: | Test | New check | On main | | --- | --- | --- | | `xgameruntime_test` | A port-changed callback that unregisters itself with `wait=true` returns. The queue is dispatched on a separate thread, so a hang shows as a failed check. | fails | | `gameinput_no_sdl_test` (script mode) | While a blocking-enumeration callback is held open on one thread, `UnregisterCallback` from another thread has not returned after 200 ms. It returns `true` once the callback ends. A blocking callback can also unregister itself. | fails | | `xapu_chad_test` | A second disconnect, a disconnected handle and a foreign handle are all refused. | crashes with heap corruption (0xc0000374) | With the fix, `ctest -R "^xgameruntime$|gameinput_no_sdl|xapu"` passes 6/6. GPUs tested: profile M: NVIDIA GeForce RTX 4070 SUPER, 12 GB, desktop, driver 621.01, Windows 11 (CPU-only tests) AI-assisted: yes (Claude Code) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: no translator change) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
zmasi commented 2026-11-28 06:38:33 +00:00

#2545 | head 9d054cd8557d96bdba0263e1ad6278bed60a3583 (merged cleanly with main 6dfca642807631bbe41d6925f152529f99c71ce2) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^(xapu_chad|xgameruntime|xgameruntime_user|xgameruntime_offline)$" --output-on-failure and ctest --test-dir runtime/build-nosdl -C RelWithDebInfo -R "^(gameinput_no_sdl|gameinput_no_sdl_script)$" --output-on-failure (-DLEONIDA_SDL3=OFF) | red on main: xapu_chad_test crashes with 0xC0000374 (STATUS_HEAP_CORRUPTION) on a second XApuDisconnect(apu, 0x20); xgameruntime_test fails (1 of 185 checks failed: self-unregistering connectivity callback with wait=true deadlocks waiting on itself); gameinput_no_sdl_test --input-script fails (1 failure(s): UnregisterCallback from another thread returns before the running GameInputBlockingEnumeration callback finishes) | green on PR+main: 4/4 CTests pass in default build (xapu_chad 70/70, xgameruntime 185/185, xgameruntime_user, xgameruntime_offline) and 2/2 CTests pass in -DLEONIDA_SDL3=OFF build (gameinput_no_sdl, gameinput_no_sdl_script, 0 failures); check_no_game_data.py 0 violations. Follow-up note (non-blocking for this PR): runtime/shims/gameinput.cpp:2656-2660 (LEONIDA_SDL3=ON) still invokes GameInputBlockingEnumeration callbacks without holding s.deliver_mu / s.delivering_thread or re-checking active/registration across multiple connected devices | Windows 11, RTX 3090 (610.88), Vulkan SDK 1.4.363.1 | AI-assisted: yes

#2545 | head 9d054cd8557d96bdba0263e1ad6278bed60a3583 (merged cleanly with main 6dfca642807631bbe41d6925f152529f99c71ce2) | `ctest --test-dir runtime/build -C RelWithDebInfo -R "^(xapu_chad|xgameruntime|xgameruntime_user|xgameruntime_offline)$" --output-on-failure` and `ctest --test-dir runtime/build-nosdl -C RelWithDebInfo -R "^(gameinput_no_sdl|gameinput_no_sdl_script)$" --output-on-failure` (`-DLEONIDA_SDL3=OFF`) | red on main: `xapu_chad_test` crashes with `0xC0000374` (`STATUS_HEAP_CORRUPTION`) on a second `XApuDisconnect(apu, 0x20)`; `xgameruntime_test` fails (`1 of 185 checks failed`: self-unregistering connectivity callback with `wait=true` deadlocks waiting on itself); `gameinput_no_sdl_test --input-script` fails (`1 failure(s)`: `UnregisterCallback` from another thread returns before the running `GameInputBlockingEnumeration` callback finishes) | green on PR+main: `4/4` CTests pass in default build (`xapu_chad` `70/70`, `xgameruntime` `185/185`, `xgameruntime_user`, `xgameruntime_offline`) and `2/2` CTests pass in `-DLEONIDA_SDL3=OFF` build (`gameinput_no_sdl`, `gameinput_no_sdl_script`, `0` failures); `check_no_game_data.py` `0` violations. Follow-up note (non-blocking for this PR): `runtime/shims/gameinput.cpp:2656-2660` (`LEONIDA_SDL3=ON`) still invokes `GameInputBlockingEnumeration` callbacks without holding `s.deliver_mu` / `s.delivering_thread` or re-checking `active`/registration across multiple connected devices | Windows 11, RTX 3090 (610.88), Vulkan SDK 1.4.363.1 | AI-assisted: yes
lixonik approved these changes 2026-11-28 08:51:56 +00:00
lixonik left a comment

Reviewed the diff and tested merged onto main (493867366) with Ninja Release (MSVC).

The fixes across gameinput_no_sdl.cpp (re-entrant deliver_mu (std::recursive_mutex) + saving/restoring delivering_thread around blocking enumeration), xapu_queue.cpp, and xgameruntime.cpp are clean and well covered by the added unit tests.

Tests run

  • ctest --output-on-failure -R "^(gameinput_no_sdl|xapu_chad|xgameruntime)$": 3/3 passed (xapu_chad 0.03 s, xgameruntime 0.14 s, gameinput_no_sdl 0.03 s).

AI-assisted (Antigravity, Gemini).

Reviewed the diff and tested merged onto `main` (`493867366`) with Ninja Release (MSVC). The fixes across `gameinput_no_sdl.cpp` (re-entrant `deliver_mu` (`std::recursive_mutex`) + saving/restoring `delivering_thread` around blocking enumeration), `xapu_queue.cpp`, and `xgameruntime.cpp` are clean and well covered by the added unit tests. ### Tests run - `ctest --output-on-failure -R "^(gameinput_no_sdl|xapu_chad|xgameruntime)$"`: **3/3 passed** (`xapu_chad` 0.03 s, `xgameruntime` 0.14 s, `gameinput_no_sdl` 0.03 s). AI-assisted (Antigravity, Gemini).
MoonEyes commented 2026-11-28 17:50:38 +00:00

Red/green: On main xapu_chad_test crashes with 0xC0000374 on a second XApuDisconnect and xgameruntime_test fails 1 check (a self-unregistering callback with wait=true deadlocks), and with the PR ctest -R "^(xapu_chad|xgameruntime)$" passes 2/2.
Independent tester (OreoFR, RTX 4070, Windows 11): PR head merged with the other PRs of its batch on main c1ecf04ed, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.

Red/green: On main xapu_chad_test crashes with 0xC0000374 on a second XApuDisconnect and xgameruntime_test fails 1 check (a self-unregistering callback with wait=true deadlocks), and with the PR ctest -R "^(xapu_chad|xgameruntime)$" passes 2/2. Independent tester (OreoFR, RTX 4070, Windows 11): PR head merged with the other PRs of its batch on main c1ecf04ed, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.
OreoFR commented 2026-11-28 22:04:52 +00:00

Verified by OreoFR

  • Base: main c1ecf04ed + PR head 9d054cd85; green run in a merge package of 11 PRs, no new ctest failure vs main
  • Test: ctest -R "^(xapu_chad|xgameruntime)$"
  • Red on main: xapu_chad crashes (0xc0000374, heap corruption); xgameruntime 1 failure (a callback unregistering itself with wait=true waits on itself)
  • Green with the PR: 2/2 OK
  • Verdict: READY (gameinput_no_sdl_test not built: that target only exists without SDL3, so that file is not checked)
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

AI-assisted (Claude Code, Claude Opus 5.5).

**Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `9d054cd85`; green run in a merge package of 11 PRs, no new ctest failure vs main - Test: `ctest -R "^(xapu_chad|xgameruntime)$"` - Red on main: xapu_chad crashes (0xc0000374, heap corruption); xgameruntime 1 failure (a callback unregistering itself with wait=true waits on itself) - Green with the PR: 2/2 OK - Verdict: READY (gameinput_no_sdl_test not built: that target only exists without SDL3, so that file is not checked) - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
Sign in to join this conversation.
No description provided.