Skip to content

Fix Python WinUI teardown with explicit process module ownership - #156

Merged
leileizhang (lei9444) merged 2 commits into
mainfrom
lei9444-python-winui-teardown
Sep 16, 2026
Merged

leileizhang (lei9444) merged 2 commits into
mainfrom
lei9444-python-winui-teardown

Conversation

@lei9444

Copy link
Copy Markdown
Contributor

Fixes #116.

Summary

Prevent Python WinUI teardown from releasing Controls' static XAML references through an already-unloaded Microsoft.UI.Xaml.dll.

  • Add a named, typed process-lifetime module owner, enabled only by Python's actual Microsoft.UI.Xaml.* activation and XAML Application composition paths.
  • Acquire ordinary loader references from a live typed factory's vtable, validate module identity/path, and deduplicate publication. Transfer successful WinUI fallback LoadLibrary references into the same owner instead of accumulating them per activation; release failed and competing candidates.
  • Handle metadata-provider activation that loads Controls before XAML by resolving the real Application factory before returning the provider factory, without constructing an Application or entering its message loop.
  • Add native SDK factory regressions, an unchanged-sample lifetime runner, and test-only ownership accounting and a shutdownable Tokio fixture using the production apartment hooks.

Lifecycle contract

After Python first uses WinUI, the ordinary path retains one owned XAML DLL reference until process termination. A required fallback retains at most one reference per validated implementation module. This deliberately retains the module mapping and its static state, not application-held COM objects or factories.

Windows, events, projected values, and successful apartment initialization calls still receive normal cleanup. There is no PIN flag, early atexit unload, skipped RoUninitialize, new public Python API, second production executor, or sample nesting change. JS and ordinary WinRT/COM keep their existing activation policy; ordinary imports/Uri usage do not load XAML or CoreMessaging.

In-process unloading/hot replacement of the retained WinUI implementation is not supported. This is distinct from updating application UI/XAML, and does not assert that WinUI lacks thread-level shutdown or that arbitrary native hosts bypassing dynwinrt are fixed.

Validation

Validated on Windows ARM64 build 28020 with CPython 3.13.15, freshly built production/test wheels, regenerated wrappers, WinUI metadata 2.3.0 and Foundation 2.3.5. The actual selected runtime was Microsoft.WindowsAppRuntime.2_2.4.0.0_arm64__8wekyb3d8bbwe (XAML 3.2.0.2511, Controls 3.2.3.2608).

  • Original, byte-unchanged Hello World run.ps1 -Python <ARM64 Python> -Smoke -Major 2 -Minor 3: actual exit 0, also independently reproduced during review.
  • Verified all 14 projected native releases precede STA teardown; managed/external MTA threads remain alive past STA exit, then actually uninitialize and join. Controls unloads while XAML remains callable.
  • PE-entrypoint native tracing confirms Controls detaches before XAML: during the last managed MTA's COM cleanup, then XAML during final process shutdown. The real default-async process-exit case also completes with Controls-before-XAML final detach and no AV.
  • The test-only Tokio fixture starts and synchronously stops/joins real async and blocking-pool threads. The unchanged default static Tokio runtime has no shutdown API; its successful process-exit case is not reported as a join.
Checks Result
Core activation/composition/ownership 17 passed
Production Python lifetime cases 7 passed
Instrumented lifecycle/native factory cases 11 passed
Ordinary Python apartment/projection/async/typing 35 passed
WinRT/codegen snapshots and related projections 15 passed
JS WinRT, Classic COM, imports, native WinUI scheduling 20 passed
JS public type suites, Rust formatting, diff checks Passed
x64 and i686 core compilation Passed; live execution tested on ARM64 only

Public Python exports and stubs, JS source, codegen source, and the Hello World application/launch scripts are unchanged.

Retain validated WinUI implementation module ownership for Python until process termination, deduplicate activation fallback references, and preserve normal COM object and apartment cleanup. Add targeted native ownership, MTA, and Tokio teardown regressions without changing public Python or JavaScript APIs.

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

github-actions Bot commented Sep 16, 2026 •

Copy link
Copy Markdown

Mixed-language test coverage

Workflow status: ✅ Passed

Layer Lines Functions Branches/regions
Rust, including native .pyd/.node 85.71% 80.87% 85.23% regions
Python aggregate 71.06% n/a 37.9% branches
Python runtime 99.12% n/a 97.37% branches
Generated Python WinRT projections 70.05% n/a 27.07% branches
Generated Python WinRT implementations 72.29% n/a 47.04% branches
JavaScript aggregate 22.14% 25.21% 57.88% branches
JavaScript runtime 44.27% 45.76% 78.99% branches
Generated WinRT projections 22.97% 18.71% 54.94% branches
Generated WinRT implementations 48.66% 59.76% 61.14% branches
Generated Classic COM projections 11.92% 23.97% 54.2% branches

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

Let the Windows loader resolve the full dependency path and compare retained HMODULE values instead of case-sensitive path spellings. Keep rejecting distinct runtime modules and add real loader regressions for filename and directory casing.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@lei9444
leileizhang (lei9444) merged commit 84ac92e into main Sep 16, 2026
28 of 29 checks passed
@lei9444
leileizhang (lei9444) deleted the lei9444-python-winui-teardown branch September 16, 2026 09:27
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.

Python WinUI smoke crashes during immediate XAML teardown on ARM64

2 participants