Repository navigation
[runtime/src] Track native ownership for user-type peers - #27401
rolfbjarne wants to merge 8 commits into
Conversation
Peers with an associated native GCHandle now participate in reference tracking, so ownership obtained by promoting an Objective-C weak reference keeps the original managed peer alive. Use a separate implicit flag that is cleared when the native handle changes, leaving explicit MarkDirty registration untouched. Synchronize user-type releases with the GC retain-count check. This is a partial fix for #27374. Weak-reference promotion can still race between the GC reference check and finalization, and a stale native ivar can prevent a replacement peer from registering. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
For user types, releasing the managed ref clears HasManagedRef instead of nulling out the handle, and the toggleref callback then returns before calling retainCount. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Describe how duplicate peers and weak promotion can affect tracking, and clarify which flag the release waypoint reads. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Limit the tracking comments to CoreCLR and NativeAOT, and remove the native-retain control case which does not exercise implicit tracking. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
Implicit tracking can dispatch retainCount to managed code during a GC callback that forbids managed entry, risking hangs or aborts for affected NSObject subclasses.
1 open issue
What changed in this PR
Adds implicit native-ownership tracking for user-type NSObject peers on CoreCLR and NativeAOT. This partially addresses #27374 by preserving managed peer identity and state when native ownership comes through weak-reference promotion.
Changes:
- Tracks peers that own their native GCHandle association, clearing implicit tracking when their handle changes.
- Extends GC retain-count checks and release synchronization to implicitly tracked peers.
- Adds lifetime and replacement-handle regression tests and updates preserved-API baselines.
| File | Description |
|---|---|
| tests/monotouch-test/ObjCRuntime/NativeWeakTest.cs | Tests weak-promotion ownership and replacement-peer collection. |
| tests/dotnet/UnitTests/expected/TVOS-CoreCLR-R2R-preservedapis.txt | Adds the implicit-tracking flag to the tvOS R2R baseline. |
| tests/dotnet/UnitTests/expected/TVOS-CoreCLR-Interpreter-preservedapis.txt | Updates the tvOS interpreter baseline. |
| tests/dotnet/UnitTests/expected/MacCatalyst-CoreCLR-R2R-preservedapis.txt | Updates the Mac Catalyst R2R baseline. |
| tests/dotnet/UnitTests/expected/MacCatalyst-CoreCLR-Interpreter-preservedapis.txt | Updates the Mac Catalyst interpreter baseline. |
| tests/dotnet/UnitTests/expected/iOS-CoreCLR-R2R-preservedapis.txt | Updates the iOS R2R baseline. |
| tests/dotnet/UnitTests/expected/iOS-CoreCLR-Interpreter-preservedapis.txt | Updates the iOS interpreter baseline. |
| src/Foundation/NSObject2.cs | Manages implicit tracking according to GCHandle ownership and handle changes. |
| runtime/xamarin/main.h | Defines the matching native tracking flag. |
| runtime/runtime.m | Extends release synchronization to user types and documents ownership behavior. |
| runtime/coreclr-bridge.m | Includes implicitly tracked peers in GC retention checks. |
Review effort: Balanced
…upported Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
|
@dalexsoto review |
There was a problem hiding this comment.
🔵 Needs a closer look
The expanded GC-time retain-count checks and release synchronization require human review and Apple-platform CoreCLR/NativeAOT concurrency testing, unavailable in this review environment, to establish lifetime and deadlock safety.
No open issues 🎉
1 resolved since last review
Review effort: Balanced
dalexsoto
left a comment
There was a problem hiding this comment.
The peer-ownership and GC integration changes look good to me. The documented retainCount tradeoff and the stated scope limitations are understood.

Adds an implicit toggle-ref flag for user-type peers on CoreCLR and NativeAOT when they own their native GCHandle association. This keeps peer state alive when native code acquires a reference through objc_loadWeakRetained, which bypasses the retain trampoline.
The flag is set only when this peer installs or already owns the association, and is cleared when its handle changes. Explicit MarkDirty registration and normal retain/release strong handles remain unchanged.
This is a partial contribution to #27374. The in-GC weak-promotion race and replacement peers blocked by a stale GCHandle association remain limitations.
🤖 Pull request created by Copilot