Async callback and handle safety in xgameruntime, gameinput_no_sdl and xapu_queue (RT-ASYNC-CALLBACK-HANDLE-SAFETY) #2545
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2545"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by Tyrannys on 2026-11-28T01:40:26Z.
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 withwait=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 withtrue. The registration still ends when the callback returns. Also,net_registerused a throwingnewacross the ABI; it isnew (std::nothrow)now.gameinput_no_sdl.cpp. TheGameInputBlockingEnumerationreport ran outsidedeliver_mu. SoUnregisterCallbackon 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 underdeliver_muand marks the thread as delivering, the same waydeliver()runs callbacks. So another thread's unregister waits for it, and the callback can still unregister itself.xapu_queue.cpp.XApuDisconnectfreed the connection again and closed its event twice. Connections are now kept in a live set under astd::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 getsE_INVALIDARG(or a count of 0), and a disconnect waits for calls in flight.xgameruntime.cppis also edited by our #2520, in other functions; the two merge cleanly. #2497 editsgameinput.cpp, notgameinput_no_sdl.cpp.Tested
New checks:
xgameruntime_testwait=truereturns. The queue is dispatched on a separate thread, so a hang shows as a failed check.gameinput_no_sdl_test(script mode)UnregisterCallbackfrom another thread has not returned after 200 ms. It returnstrueonce the callback ends. A blocking callback can also unregister itself.xapu_chad_testWith 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
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a: no translator change)🤖 Generated with Claude Code
Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T06:38:33Z.
#2545 | head
9d054cd855(merged cleanly with main6dfca64280) |ctest --test-dir runtime/build -C RelWithDebInfo -R "^(xapu_chad|xgameruntime|xgameruntime_user|xgameruntime_offline)$" --output-on-failureandctest --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_testcrashes with0xC0000374(STATUS_HEAP_CORRUPTION) on a secondXApuDisconnect(apu, 0x20);xgameruntime_testfails (1 of 185 checks failed: self-unregistering connectivity callback withwait=truedeadlocks waiting on itself);gameinput_no_sdl_test --input-scriptfails (1 failure(s):UnregisterCallbackfrom another thread returns before the runningGameInputBlockingEnumerationcallback finishes) | green on PR+main:4/4CTests pass in default build (xapu_chad70/70,xgameruntime185/185,xgameruntime_user,xgameruntime_offline) and2/2CTests pass in-DLEONIDA_SDL3=OFFbuild (gameinput_no_sdl,gameinput_no_sdl_script,0failures);check_no_game_data.py0violations. Follow-up note (non-blocking for this PR):runtime/shims/gameinput.cpp:2656-2660(LEONIDA_SDL3=ON) still invokesGameInputBlockingEnumerationcallbacks without holdings.deliver_mu/s.delivering_threador re-checkingactive/registration across multiple connected devices | Windows 11, RTX 3090 (610.88), Vulkan SDK 1.4.363.1 | AI-assisted: yesImported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-28T08:51:56Z.
Review: APPROVED
Reviewed the diff and tested merged onto
main(493867366) with Ninja Release (MSVC).The fixes across
gameinput_no_sdl.cpp(re-entrantdeliver_mu(std::recursive_mutex) + saving/restoringdelivering_threadaround blocking enumeration),xapu_queue.cpp, andxgameruntime.cppare 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_chad0.03 s,xgameruntime0.14 s,gameinput_no_sdl0.03 s).AI-assisted (Antigravity, Gemini).
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T17:50:38Z.
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.Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-28T22:04:52Z.
Verified by OreoFR
c1ecf04ed+ PR head9d054cd85; green run in a merge package of 11 PRs, no new ctest failure vs mainctest -R "^(xapu_chad|xgameruntime)$"AI-assisted (Claude Code, Claude Opus 5.5).