Repository navigation
Prevent reentrant Python apartment teardown during native callbacks - #205
Merged
leileizhang (lei9444) merged 5 commits intoOct 8, 2026
Merged
Conversation
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>
Mixed-language test coverageWorkflow status: ❌ Failed
|
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>
This was referenced Sep 30, 2026
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>
Kai Tao (vanzue)
approved these changes
Oct 8, 2026
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
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.
Problem
Closing the final Python
RoApartmentfrom a synchronous native callback can runRoUninitializewhile the outer native call still owns temporary COM references. A realPropertySet.MapChangedhandler reproduced0xC0000005on 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_callbackentry. Reject a final managed close before owner drain andRoUninitialize, 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:676998b8fails with the identical counts, without Prevent reentrant Python apartment teardown during native callbacks #205. Earlier green checks on the independent Fix duplicate completion registration in generated progress promises #206–Fix Python XML nullable result declarations #209 heads did not test this combined-main tree. The baseline discrepancy is being addressed separately without weakening the gate; this PR is not all-green.Separate follow-up
A separately reported owner-drain reentrancy issue—an object's
__del__callsro_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.