com_shim: nested CoUninitialize gives its host reference back; check TlsSetValue (RT-COM-NESTED-INIT-REFS) #2542
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2542"
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:36:17Z.
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
Imported from ubiquitarianism/leonida. Originally posted by TheGeogeo on 2026-11-28T03:31:41Z.
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.Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T06:38:34Z.
#2542 | head
b04fdc91c0(merged cleanly with main6dfca64280) |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: yesImported from ubiquitarianism/leonida. Originally posted by timbot103 on 2026-11-28T14:36:59Z.
Review: COMMENTED
Static code audit against
b04fdc91c+ec740bcc3. The shim releases one host reference at each nested balance and rolls back its first host reference if the TLS write fails.One test issue at
runtime/test/com_shim_test.cpp:141. The native probe already releases the outer COM reference, then the final shimco_uninit()calls nativeCoUninitializeagain. Could the test probe with one native release, check forCO_E_NOTINITIALIZED, then reacquire that reference before the final shim balance? The old shim would still fail the apartment check.The existing tests exercise nested init/uninit, an extra guest uninit, STA/MTA mismatch, and host-first teardown. The TLS write failure was reviewed statically and has no test coverage.
AI-assisted.
Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-28T15:01:36Z.
Review: APPROVED
Reviewed
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).
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T17:51:01Z.
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.Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-28T22:04:46Z.
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).