com_shim: nested CoUninitialize gives its host reference back; check TlsSetValue (RT-COM-NESTED-INIT-REFS) #2542
No reviewers
No assignees
Labels
No labels
bug
dependencies
documentation
enhancement
missing claim
needs rebase
needs-triage
question
ready-for-agent
submodules
No project
No milestone
Dependencies
No dependencies set
Notifications
1 participant
Due date
No due date set.
Reference
leonida/mandy!2542
Loading…
Reference in a new issue
No description provided.
Delete branch "rt-com-nested-init-refs"
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?
Pull request successfully merged and closed
Claimed in the coordination issue as: RT-COM-NESTED-INIT-REFS (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7231782130)
What and why
This fixes item 1 of kevincommits' Finding 19 in #1608 (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7231649637), in
runtime/shims/com_shim.cpp.CoInitializeExtakes its own host COM reference (host_inits_owned).CoUninitializegave them all back only at the outermost call. So a thread with an outer init that runs balanced inner init/uninit pairs, such as a worker loop calling a COM helper, gained one host reference per pair until the outer balance. A nestedCoUninitializenow gives back its own reference. The first init's reference still stays until the guest balances, so host-first teardown (case 3 of the test) still works.TlsSetValueunchecked. The first init ignoredTlsSetValue's result. If it failed, the state was not tracked, so noCoUninitializewould ever give the host reference back, yet the call returnedS_OK. The init now undoes the host reference and returns the error.Tested
com_shim_testgains case 4: 100 balanced nested pairs under an outer init. The outer init must stay live, and exactly one host reference may be left. The test counts references by callingCoUninitializeuntil the apartment is gone.TlsSetValuefailure path has no test, because a test cannot makeTlsSetValuefail on a valid slot.GPUs tested: profile M: NVIDIA GeForce RTX 4070 SUPER, 12 GB, desktop, driver 621.01, Windows 11 (CPU-only test)
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
Independent review of
b04fdc91c0564b19112bdc671a7f71e45e65fd32, merged locally withmain02f71ccfe(clean merge, no conflict).Machine: Windows 11 Pro 10.0.26300, MSVC from Visual Studio 18 2026 (CMake 4.3.1-msvc1, generator "Visual Studio 18 2026", x64, RelWithDebInfo). CPU-only tests.
What ran
com_shim_test(ctestcom_shim):main: 42 checks, 0 failures.com_shim_test.cppbuilt againstmain'scom_shim.cpp: 44 checks, 1 failure,FAIL balanced nested pairs leave only the outer init's host reference. "an outer init stays live across 100 balanced nested pairs" passes there too.mainand passes with the fix.main's shim: 10 of 10 failed.silent_audio_test(ctestsilent_audio), the other test target that linkscom_shim.cpp: 129 checks, 0 failures on both the merge andmain.gta6rt(which also compilescom_shim.cpp) builds on the merge with no warning or error incom_shim.cpp.git diff --check origin/main...HEAD: clean.python -I scripts/check_no_game_data.py runtime/shims/com_shim.cpp runtime/test/com_shim_test.cpp: clean.Reading the change
co_initialize_extakes exactly one host reference: the first throughowned_count = 1, and each nested one through++state->host_inits_ownedatruntime/shims/com_shim.cpp:112. Sohost_inits_owned == guest_depthholds throughout. The nestedco_uninitializethat now releases one reference keeps that invariant. The first init's reference still goes only at the guest's outer balance, so case 3 (host-first teardown) is unchanged and still passes.TlsSetValuefailure path readsGetLastError()beforedelete, releases the single reference it took (also on theRPC_E_CHANGED_MODEhold path), and never returnsHRESULT_FROM_WIN32(0)(S_OK). As the description says, it has no test.What did not run
com_shim.cppran.Side note (not this pull request)
On plain
main, configuringruntime/without Vulkan headers fails at generate time:title_hud_testlinksVulkan::Headers(runtime/CMakeLists.txt:858), and that target only exists when the headers are found. "CMake Error at CMakeLists.txt:858 (target_link_libraries): Target "title_hud_test" links to: Vulkan::Headers". The warning incmake/vulkan_headers_missing.cmakesays such a build should still produce the CPU tests. I configured with-DLEONIDA_VULKAN_HEADERS_FETCH=ON(the pinned headers) to get past it.No game data in the change or in this comment.
AI-assisted: Claude Code (Claude Sonnet 5.5)
Status: OK to merge. The gain is real but small: host COM references no longer pile up across balanced nested pairs. The new test fails on
mainand passes with the fix. No blocker. Not a duplicate: no other open pull request touchesruntime/shims/com_shim.cppor its test.#2542 | head b04fdc91c0564b19112bdc671a7f71e45e65fd32 (merged cleanly with main 6dfca642807631bbe41d6925f152529f99c71ce2) |
ctest --test-dir runtime/build -C RelWithDebInfo -R "^com_shim$" --output-on-failure| red on main:com_shim_testfails (44 check(s), 1 failure(s):FAIL balanced nested pairs leave only the outer init's host reference— 100 balanced nestedCoInitializeEx/CoUninitializepairs left 101 host COM references active instead of 1) | green on PR+main:1/1CTest passes (com_shim44/44checks in0.07s, releasing nestedhost_inits_owned > 1on innerCoUninitializeand rolling back ifTlsSetValuefails);check_no_game_data.py0violations | Windows 11, RTX 3090 (610.88), Vulkan SDK 1.4.363.1 | AI-assisted: yesReviewed
b04fdc91cand verifiedcom_shimon Windows 11.Code audit:
runtime/shims/com_shim.cpp: checkingTlsSetValuefailure and rolling back (delete created; ::CoUninitialize();) prevents leaked host references on TLS failure, and calling::CoUninitialize()on nestedco_uninitialize()wheneverhost_inits_owned > 1keeps host COM references bounded across balanced inner pairs.@timbot103's suggestion oncom_shim_test.cpp:141(reacquiring the probed outer host reference before the final shimco_uninit()) would make the test's final balance cleaner.Verification (Windows 11, MSVC 19.44, Ninja Release):
com_shim_test.ctest -R '^com_shim$': 1/1 passed (0.03 s).python -I scripts/check_no_game_data.py: passed.AI-assisted (Antigravity, Gemini).
Red/green: On main com_shim_test fails 1 of 44 checks (100 balanced nested CoInitializeEx/CoUninitialize pairs keep extra host references), and with the PR ctest -R "^com_shim$" passes.
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.
Verified by OreoFR
c1ecf04ed+ PR headb04fdc91c; green run in a merge package of 11 PRs, no new ctest failure vs mainctest -R "^com_shim$"AI-assisted (Claude Code, Claude Opus 5.5).