Prevent reentrant Python apartment teardown during native callbacks - #205
Open
leileizhang (lei9444) wants to merge 2 commits into
Open
leileizhang (lei9444) wants to merge 2 commits into
leileizhang (lei9444) wants to merge 2 commits into
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: ✅ Passed
|
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 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
RoApartmentinside a synchronous native callback can callRoUninitializewhile the outer native call still owns temporary COM references. A realPropertySet.MapChangedhandler reproduced0xC0000005when the native call unwound; this predates #198.Fix
Track synchronous native-to-Python callback depth per OS thread. A final
RoApartment.close()/__exit__()or matched manualro_uninitialize()inside a callback now raises an actionable, retryableRuntimeErrorbefore native teardown; non-final nested closes remain balanced. The caller may retry after the callback and outer native call return and retained references are released.If an anonymous guard is dropped during that callback, retain its initialization for explicit same-thread
RoApartment.recover_pending()rather than auto-closing it on unwind. Explicit wrong-thread close leaves state unchanged; foreign implicit Drop only queues an owner-thread-recoverable token and emits a native diagnostic—no foreign-thread COM cleanup or Python attachment.Verification and integration
Isolated STA/MTA
PropertySet.MapChangedchildren reproduced the baseline crash; the fix passes 26 bounded callback/lifetime cases per architecture on x64 and ARM64, generated Python WinRT E2E, native binding regressions, and exact-head Build, mixed-language coverage, and Python-release checks. Optional WinUI fixture coverage is separate.This PR is independent of #198 and does not resolve #189's ordinary object-outliving-apartment crash. If either PR merges first, reconcile the managed initialization counts and pending-close recovery paths, then rerun combined native and generated Python tests before landing the other. Human review is still required.