Repository navigation
Fix Python IBuffer byte creation across apartment teardown - #232
Merged
Merged
Conversation
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>
Mixed-language test coverageWorkflow status: ✅ Passed
|
shisan (qiutongMS)
approved these changes
Oct 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix the native access violation when
DynWinRTValue.from_bytes()is used in successiveRoApartmentscopes. 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 JavaScriptfromBuffer; 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):
Before the fix, the second
from_bytesterminates the process with0xC0000005, rather than raising a Python exception. Fresh-process controls using rebuilt CPython 3.12 x64 wheels gave:from_bytes, separate apartments0xC0000005on second callBuffer.from_bytes, separate apartments0xC0000005on second callBuffer(capacity=3), separate apartmentsRoot cause and fix
copy_to_ibuffercalledwindows0.62.2'sBuffer::Create, which retains an agileIBufferFactoryin a staticFactoryCache. Native instrumentation confirmed that the factory's vtable occupied committed image memory before the firstRoUninitialize, but that address wasMEM_FREEafterward. The second call reused the cached factory and faulted reading itsCreateslot (vtable + 0x30on x64). The symbolized stack passed throughBuffer::Create,FactoryCache::call, andcopy_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
RoApartmentsemantics. Tests assertCoGetApartmentTypereturnsCO_E_NOTINITIALIZEDafter 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.
python -m pytest bindings\py\tests\test_ibuffer_apartment.py bindings\py\tests\test_phase1.py -k 'ibuffer or bytes or apartment': 21 passed.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 oni686-pc-windows-msvc.cargo test -p dynwinrt --lib ibuffer::tests: 8 passed;cargo test -p dynwinrt-codegen --test ibuffer_projection_test: 3 passed.cargo check -p jswinrt_rs --target i686-pc-windows-msvcpasses.cargo fmt --all -- --checkandgit diff --checkpass.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.