com_shim: nested CoUninitialize gives its host reference back; check TlsSetValue (RT-COM-NESTED-INIT-REFS) #2542

Merged
Tyrannys merged 1 commit from upstream-pr/2542 into main 2026-11-28 17:51:04 +00:00
Tyrannys commented 2026-11-28 01:36:17 +00:00 (Migrated from github.com)

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.

  • Host references piled up. Every nested CoInitializeEx takes its own host COM reference (host_inits_owned). CoUninitialize gave 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 nested CoUninitialize now 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.
  • TlsSetValue unchecked. The first init ignored TlsSetValue's result. If it failed, the state was not tracked, so no CoUninitialize would ever give the host reference back, yet the call returned S_OK. The init now undoes the host reference and returns the error.

Tested

  • com_shim_test gains 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 calling CoUninitialize until the apartment is gone.
  • On main, the reference check fails. With the fix, 44/44 checks pass.
  • The existing cases still pass: the STA mismatch case, and host-first teardown.
  • The TlsSetValue failure path has no test, because a test cannot make TlsSetValue fail 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

  • 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

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5535422231 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2542). Originally posted by [Tyrannys](https://github.com/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`. - **Host references piled up.** Every nested `CoInitializeEx` takes its own host COM reference (`host_inits_owned`). `CoUninitialize` gave 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 nested `CoUninitialize` now 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. - **`TlsSetValue` unchecked.** The first init ignored `TlsSetValue`'s result. If it failed, the state was not tracked, so no `CoUninitialize` would ever give the host reference back, yet the call returned `S_OK`. The init now undoes the host reference and returns the error. ### Tested - `com_shim_test` gains 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 calling `CoUninitialize` until the apartment is gone. - On main, the reference check fails. With the fix, 44/44 checks pass. - The existing cases still pass: the STA mismatch case, and host-first teardown. - The `TlsSetValue` failure path has no test, because a test cannot make `TlsSetValue` fail 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 - [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)
TheGeogeo commented 2026-11-28 03:31:41 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by TheGeogeo on 2026-11-28T03:31:41Z.


Independent review of b04fdc91c0564b19112bdc671a7f71e45e65fd32, merged locally with main 02f71ccfe (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 (ctest com_shim):
    • merge: 44 checks, 0 failures.
    • plain main: 42 checks, 0 failures.
    • The pull request's com_shim_test.cpp built against main's com_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.
    • The new test fails on main and passes with the fix.
  • 10 repeated runs of each binary:
    • merge: 0 of 10 failed.
    • PR test on main's shim: 10 of 10 failed.
    • The result is stable both ways.
  • silent_audio_test (ctest silent_audio), the other test target that links com_shim.cpp: 129 checks, 0 failures on both the merge and main.
  • gta6rt (which also compiles com_shim.cpp) builds on the merge with no warning or error in com_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

  • Every co_initialize_ex takes exactly one host reference: the first through owned_count = 1, and each nested one through ++state->host_inits_owned at runtime/shims/com_shim.cpp:112. So host_inits_owned == guest_depth holds throughout. The nested co_uninitialize that 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.
  • The TlsSetValue failure path reads GetLastError() before delete, releases the single reference it took (also on the RPC_E_CHANGED_MODE hold path), and never returns HRESULT_FROM_WIN32(0) (S_OK). As the description says, it has no test.

What did not run

  • The full runtime ctest suite: only the two targets that link com_shim.cpp ran.
  • Nothing with the game: the shim has no game-dependent test.

Side note (not this pull request)

On plain main, configuring runtime/ without Vulkan headers fails at generate time: title_hud_test links Vulkan::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 in cmake/vulkan_headers_missing.cmake says 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 main and passes with the fix. No blocker. Not a duplicate: no other open pull request touches runtime/shims/com_shim.cpp or its test.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7233146176 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2542#issuecomment-7233146176). Originally posted by [TheGeogeo](https://github.com/TheGeogeo) on 2026-11-28T03:31:41Z. --- Independent review of `b04fdc91c0564b19112bdc671a7f71e45e65fd32`, merged locally with `main` `02f71ccfe` (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` (ctest `com_shim`): - merge: 44 checks, 0 failures. - plain `main`: 42 checks, 0 failures. - The pull request's `com_shim_test.cpp` built against `main`'s `com_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. - The new test fails on `main` and passes with the fix. - 10 repeated runs of each binary: - merge: 0 of 10 failed. - PR test on `main`'s shim: 10 of 10 failed. - The result is stable both ways. - `silent_audio_test` (ctest `silent_audio`), the other test target that links `com_shim.cpp`: 129 checks, 0 failures on both the merge and `main`. - `gta6rt` (which also compiles `com_shim.cpp`) builds on the merge with no warning or error in `com_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** - Every `co_initialize_ex` takes exactly one host reference: the first through `owned_count = 1`, and each nested one through `++state->host_inits_owned` at `runtime/shims/com_shim.cpp:112`. So `host_inits_owned == guest_depth` holds throughout. The nested `co_uninitialize` that 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. - The `TlsSetValue` failure path reads `GetLastError()` before `delete`, releases the single reference it took (also on the `RPC_E_CHANGED_MODE` hold path), and never returns `HRESULT_FROM_WIN32(0)` (`S_OK`). As the description says, it has no test. **What did not run** - The full runtime ctest suite: only the two targets that link `com_shim.cpp` ran. - Nothing with the game: the shim has no game-dependent test. **Side note (not this pull request)** On plain `main`, configuring `runtime/` without Vulkan headers fails at generate time: `title_hud_test` links `Vulkan::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 in `cmake/vulkan_headers_missing.cmake` says 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 `main` and passes with the fix. No blocker. Not a duplicate: no other open pull request touches `runtime/shims/com_shim.cpp` or its test.
zmasi commented 2026-11-28 06:38:34 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T06:38:34Z.


#2542 | head b04fdc91c0 (merged cleanly with main 6dfca64280) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^com_shim$" --output-on-failure | red on main: com_shim_test fails (44 check(s), 1 failure(s): FAIL balanced nested pairs leave only the outer init's host reference — 100 balanced nested CoInitializeEx/CoUninitialize pairs left 101 host COM references active instead of 1) | green on PR+main: 1/1 CTest passes (com_shim 44/44 checks in 0.07s, releasing nested host_inits_owned > 1 on inner CoUninitialize and rolling back if TlsSetValue fails); check_no_game_data.py 0 violations | Windows 11, RTX 3090 (610.88), Vulkan SDK 1.4.363.1 | AI-assisted: yes

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7235426645 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2542#issuecomment-7235426645). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T06:38:34Z. --- #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_test` fails (`44 check(s), 1 failure(s)`: `FAIL balanced nested pairs leave only the outer init's host reference` — 100 balanced nested `CoInitializeEx`/`CoUninitialize` pairs left 101 host COM references active instead of 1) | green on PR+main: `1/1` CTest passes (`com_shim` `44/44` checks in `0.07s`, releasing nested `host_inits_owned > 1` on inner `CoUninitialize` and rolling back if `TlsSetValue` fails); `check_no_game_data.py` `0` violations | Windows 11, RTX 3090 (610.88), Vulkan SDK 1.4.363.1 | AI-assisted: yes
timbot103 commented 2026-11-28 14:36:59 +00:00 (Migrated from github.com)

Imported 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 shim co_uninit() calls native CoUninitialize again. Could the test probe with one native release, check for CO_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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6098420110 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2542#pullrequestreview-6098420110). Originally posted by [timbot103](https://github.com/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 shim `co_uninit()` calls native `CoUninitialize` again. Could the test probe with one native release, check for `CO_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.
lixonik commented 2026-11-28 15:01:36 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-28T15:01:36Z.

Review: APPROVED


Reviewed b04fdc91c and verified com_shim on Windows 11.

Code audit:

  • runtime/shims/com_shim.cpp: checking TlsSetValue failure and rolling back (delete created; ::CoUninitialize();) prevents leaked host references on TLS failure, and calling ::CoUninitialize() on nested co_uninitialize() whenever host_inits_owned > 1 keeps host COM references bounded across balanced inner pairs.
  • Non-blocking note: @timbot103's suggestion on com_shim_test.cpp:141 (reacquiring the probed outer host reference before the final shim co_uninit()) would make the test's final balance cleaner.

Verification (Windows 11, MSVC 19.44, Ninja Release):

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

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6098555999 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2542#pullrequestreview-6098555999). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-28T15:01:36Z. **Review:** APPROVED --- Reviewed `b04fdc91c` and verified `com_shim` on Windows 11. Code audit: - `runtime/shims/com_shim.cpp`: checking `TlsSetValue` failure and rolling back (`delete created; ::CoUninitialize();`) prevents leaked host references on TLS failure, and calling `::CoUninitialize()` on nested `co_uninitialize()` whenever `host_inits_owned > 1` keeps host COM references bounded across balanced inner pairs. - Non-blocking note: `@timbot103`'s suggestion on `com_shim_test.cpp:141` (reacquiring the probed outer host reference before the final shim `co_uninit()`) would make the test's final balance cleaner. Verification (Windows 11, MSVC 19.44, Ninja Release): - Built `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).
MoonEyes commented 2026-11-28 17:51:01 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7243334488 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2542#issuecomment-7243334488). Originally posted by [MoonEyes](https://github.com/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.
OreoFR commented 2026-11-28 22:04:46 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-28T22:04:46Z.


Verified by OreoFR

  • Base: main c1ecf04ed + PR head b04fdc91c; green run in a merge package of 11 PRs, no new ctest failure vs main
  • Test: ctest -R "^com_shim$"
  • Red on main: 1 failure of 44 (balanced nested pairs leave only the outer init's reference)
  • Green with the PR: OK
  • Verdict: READY
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7246480628 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2542#issuecomment-7246480628). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-28T22:04:46Z. --- **Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `b04fdc91c`; green run in a merge package of 11 PRs, no new ctest failure vs main - Test: `ctest -R "^com_shim$"` - Red on main: 1 failure of 44 (balanced nested pairs leave only the outer init's reference) - Green with the PR: OK - Verdict: READY - 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 reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!2542
No description provided.