Fix COM wrapper weak-handle defensive copy in ReferenceTrackerNativeObjectWrapper#129668
Open
Copilot wants to merge 3 commits into
Open
Fix COM wrapper weak-handle defensive copy in ReferenceTrackerNativeObjectWrapper#129668Copilot wants to merge 3 commits into
Copilot wants to merge 3 commits into
Conversation
Contributor
|
Tagging subscribers to this area: @dotnet/interop-contrib |
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix potential double free in ComWrappers implementation
Fix COM wrapper weak-handle defensive copy in ReferenceTrackerNativeObjectWrapper
Jun 21, 2026
jkotas
approved these changes
Jun 21, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses a correctness issue in CoreLib’s COM wrapper tracking path by ensuring _nativeObjectWrapperWeakHandle.Free() mutates the actual stored GCHandle field (avoiding readonly-field defensive copies) so cleanup becomes reliably idempotent.
Changes:
- Changed
ReferenceTrackerNativeObjectWrapper._nativeObjectWrapperWeakHandlefromreadonlyto mutable to preventGCHandle.Free()from operating on a defensive copy. - Ensures subsequent cleanup paths (e.g., re-release/finalizer flows) can observe the freed state.
Comment on lines
+670
to
+672
| private int _trackerObjectDisconnected; // Atomic boolean, so using int. | ||
| private readonly bool _releaseTrackerObject; | ||
| internal readonly GCHandle _nativeObjectWrapperWeakHandle; | ||
| internal GCHandle _nativeObjectWrapperWeakHandle; |
Member
There was a problem hiding this comment.
I agree with this. These sorts of gotchas crop up and we've no tests that can help us know we've done something wrong.
Comment on lines
+671
to
+672
| private readonly bool _releaseTrackerObject; | ||
| internal readonly GCHandle _nativeObjectWrapperWeakHandle; | ||
| internal GCHandle _nativeObjectWrapperWeakHandle; |
Member
There was a problem hiding this comment.
It is very difficult to add deterministic test that verifies there is no GC handle double-free.
Co-authored-by: jkotas <6668460+jkotas@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.
This change addresses a COM wrapper lifetime bug where
GCHandle.Free()could execute on a defensive copy of_nativeObjectWrapperWeakHandle, making cleanup non-idempotent under finalization/release interleavings.The fix is surgical: remove
readonlysoFree()updates the actual field state.What changed
System.Runtime.InteropServices.ComWrappers.ReferenceTrackerNativeObjectWrapper, changed:internal readonly GCHandle _nativeObjectWrapperWeakHandle;internal GCHandle _nativeObjectWrapperWeakHandle;Why this matters
GCHandleis a mutable struct.readonlyfield can operate on a defensive copy.Release()/finalizer race handling, this can lead to staleIsAllocatedstate and duplicate free behavior.Code example