Skip to content

Fix Python IBuffer byte creation across apartment teardown - #232

Merged
leileizhang (lei9444) merged 1 commit into
mainfrom
lei9444-buffer-apartment-crash-fix
Oct 30, 2026
Merged

leileizhang (lei9444) merged 1 commit into
mainfrom
lei9444-buffer-apartment-crash-fix

Conversation

@lei9444

@lei9444 leileizhang (lei9444) commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fix the native access violation when DynWinRTValue.from_bytes() is used in successive RoApartment scopes. The change is confined to the shared IBuffer creation helper, with isolated Rust/Python regressions and usage documentation. It also covers the helper used by JavaScript fromBuffer; no generated API or apartment lifecycle behavior changes.

Reproduction

With the Python runtime wheel installed, run this in a fresh Python process (no generated bindings are needed):

from dynwinrt import DynWinRTValue, RoApartment

for _ in range(2):
    with RoApartment():
        value = DynWinRTValue.from_bytes(b"abc")
        value.release()
        del value

Before the fix, the second from_bytes terminates the process with 0xC0000005, rather than raising a Python exception. Fresh-process controls using rebuilt CPython 3.12 x64 wheels gave:

Case Before fix After fix
Raw from_bytes, separate apartments 0xC0000005 on second call Exit 0
Generated Buffer.from_bytes, separate apartments 0xC0000005 on second call Exit 0
Two raw calls in one apartment Exit 0 Exit 0
Generated Buffer(capacity=3), separate apartments Exit 0 Exit 0

Root cause and fix

copy_to_ibuffer called windows 0.62.2's Buffer::Create, which retains an agile IBufferFactory in a static FactoryCache. Native instrumentation confirmed that the factory's vtable occupied committed image memory before the first RoUninitialize, but that address was MEM_FREE afterward. The second call reused the cached factory and faulted reading its Create slot (vtable + 0x30 on x64). The symbolized stack passed through Buffer::Create, FactoryCache::call, and copy_to_ibuffer.

Acquire a call-local factory through windows_core::factory::<Buffer, IBufferFactory>() instead. This uses the same activation loader without the static cache, invokes the SDK-declared factory vtable, preserves HRESULT/null-output validation, and releases the factory after creation. Byte copying, length/capacity, input validation, and object ownership are unchanged.

This does not retain an apartment, skip releases, or alter RoApartment semantics. Tests assert CoGetApartmentType returns CO_E_NOTINITIALIZED after each complete scope exit. The Rust regression has its own executable so other tests' process-lifetime MTA cannot hide the bug; Python cases each run in a subprocess.

Validation

Local environment: Windows 11 x64 build 26300, CPython 3.12.10, Node 24.13.0. Before/after tests used freshly built runtime wheels normally installed in separate virtual environments, not an editable source tree.

  • New Python regression: all six separate-apartment cases crashed before the fix; all 12 cases pass afterward. Covers STA/MTA, same/separate scopes, explicit release/GC/apartment cleanup, empty/binary data, and bytearray copy isolation.
  • python -m pytest bindings\py\tests\test_ibuffer_apartment.py bindings\py\tests\test_phase1.py -k 'ibuffer or bytes or apartment': 21 passed.
  • Original four subprocess controls above plus 36 real generated Buffer/IBuffer/capacity-constructor cases: all exit 0 after the fix.
  • cargo test -p dynwinrt --test ibuffer_apartment: native crash before the fix, pass afterward. The same regression also passes live on i686-pc-windows-msvc.
  • cargo test -p dynwinrt --lib ibuffer::tests: 8 passed; cargo test -p dynwinrt-codegen --test ibuffer_projection_test: 3 passed.
  • Fresh x64 JS native build: 3 Node smoke tests passed, covering existing implicit activation, Buffer/Uint8Array copies, deterministic release, and non-IBuffer errors. cargo check -p jswinrt_rs --target i686-pc-windows-msvc passes.
  • cargo fmt --all -- --check and git diff --check pass.

Local wheels/native builds used the development profile. ARM64, other CPython versions, optimized builds, and the full codegen matrix were not run locally. Other activation caches are unchanged.

Use a call-local Buffer factory instead of the windows static factory cache, whose vtable can be unloaded by RoUninitialize. Cover isolated STA/MTA lifetimes and preserve byte-copy and owner-release behavior.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown

Mixed-language test coverage

Workflow status: ✅ Passed

Layer Lines Functions Branches/regions
Rust, including native .pyd/.node 86.67% 82.16% 86.3% regions
Python aggregate 70.97% n/a 38.22% branches
Python runtime 98.28% n/a 95% branches
Generated Python WinRT projections 68.86% n/a 30.46% branches
Generated Python WinRT implementations 72% n/a 46.3% branches
JavaScript aggregate 18.38% 24.85% 55.94% branches
JavaScript runtime 41.73% 41.53% 78.33% branches
Generated WinRT projections 17.27% 19.36% 52.51% branches
Generated WinRT implementations 45.99% 59.19% 60.97% branches
Generated Classic COM projections 11.38% 23.88% 53.1% branches

View workflow run and download full HTML/LCOV/XML reports

@lei9444
leileizhang (lei9444) merged commit 71871e9 into main Oct 30, 2026
33 checks passed
@lei9444
leileizhang (lei9444) deleted the lei9444-buffer-apartment-crash-fix branch October 30, 2026 04:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants