Skip to content

Prevent reentrant Python apartment teardown during native callbacks - #205

Merged
leileizhang (lei9444) merged 5 commits into
mainfrom
lei9444-guard-reentrant-apartment-close
Oct 8, 2026
Merged

leileizhang (lei9444) merged 5 commits into
mainfrom
lei9444-guard-reentrant-apartment-close

Conversation

@lei9444

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

Copy link
Copy Markdown
Contributor

Problem

Closing the final Python RoApartment from a synchronous native callback can run RoUninitialize while the outer native call still owns temporary COM references. A real PropertySet.MapChanged handler reproduced 0xC0000005 on x64 and ARM64. This is distinct from the ordinary owner-after-apartment-exit lifetime issue addressed by #198.

Fix

Track per-thread native callback depth at the shared with_python_callback entry. Reject a final managed close before owner drain and RoUninitialize, preserving #198's managed apartment and ownership model. Named guards remain active for retry after both the callback and outer native call return; non-final nested closes still balance normally.

Dropped guards retain one initialization lease for explicit owner-thread recovery through RoApartment.recover_pending(). Callback-deferred leases and failed-owner-cleanup leases stay distinct: retry_pending_apartment_close() cannot consume a callback-deferred lease, and neither helper can consume the same lease twice. Explicit wrong-thread close leaves the apartment active; foreign-thread Drop queues recovery with a native diagnostic, without attaching Python or calling COM on that thread. Unavailable teardown state fails closed. Pending async work is not cancelled implicitly, and native uninitialization is never automatically deferred to callback unwind.

The diff is limited to six Python binding, documentation, and test files. No JavaScript runtime, codegen, XML/JSON behavior, coverage thresholds, or release workflows are changed.

Validation

At exact head 44b7da9aa376e92061f51ad71cdcc73d4a60977e:

Separate follow-up

A separately reported owner-drain reentrancy issue—an object's __del__ calls ro_initialize() during final cleanup, and the outer close overwrites the new initialization count—is being independently reproduced and fixed. It was reported on both main and this head, not as a #205 merge regression. This PR does not claim to solve arbitrary finalizer reentrancy during owner drain.

Leave this PR open and unmerged for human review.

Reject final apartment close during synchronous native callbacks and preserve anonymously dropped contexts for explicit same-thread recovery. Cover generated PropertySet MapChanged callbacks in isolated STA/MTA subprocesses.

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

github-actions Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Mixed-language test coverage

Workflow status: ❌ Failed

Layer Lines Functions Branches/regions
Rust, including native .pyd/.node 87.08% 82.62% 86.8% regions
Python aggregate 72.35% n/a 39.49% branches
Python runtime 98.17% n/a 94.57% branches
Generated Python WinRT projections 71.1% n/a 33.61% branches
Generated Python WinRT implementations 71.97% n/a 46.3% branches
JavaScript aggregate 17.99% 24.18% 56.01% branches
JavaScript runtime 44.27% 45.76% 78.99% branches
Generated WinRT projections 16.57% 18.26% 52.17% branches
Generated WinRT implementations 45.99% 59.19% 60.97% branches
Generated Classic COM projections 11.52% 23.97% 53.4% branches

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

Reject foreign-thread apartment operations before native teardown. Retain foreign drops in an owner-recoverable queue without attaching Python, and fail closed during owner TLS teardown. Cover both architecture callback paths and exceptional queue states.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
leileizhang (lei9444) and others added 3 commits October 8, 2026 17:18
Reconcile #198's managed apartment owner drain and callback shutdown gate with fail-before-uninitialize reentrant protection and separate retryable pending leases.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Retain the independently merged WinRT projection and CI updates while keeping the #205 callback-close fix based on the current main.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Use fallible owner-thread pending storage in Drop, falling back to the nonblocking recovery queue rather than panicking on a reentrant borrow. Exercise recovery and COM model reuse.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@lei9444
leileizhang (lei9444) merged commit faece2d into main Oct 8, 2026
32 of 33 checks passed
@lei9444
leileizhang (lei9444) deleted the lei9444-guard-reentrant-apartment-close branch October 8, 2026 14:20
leileizhang (lei9444) added a commit that referenced this pull request Oct 8, 2026
Resolve the #205 runtime integration without weakening callback-close checks or owner-thread recovery. Reserve and restore the exact local or foreign cleanup-failure lease before Python reentry, retaining the retry guard and new initializations acquired during drain. Extend real local/foreign recovery and native-balance regression coverage.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7c6c22be-5bc5-4ef3-a9e9-aff640a91c27
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