Skip to content

[runtime/src] Track native ownership for user-type peers - #27401

Open
rolfbjarne wants to merge 8 commits into
mainfrom
dev/rolf/togglable-wrapper-types
Open

rolfbjarne wants to merge 8 commits into
mainfrom
dev/rolf/togglable-wrapper-types

Conversation

@rolfbjarne

Copy link
Copy Markdown
Member

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

rolfbjarne and others added 7 commits November 19, 2026 15:24
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>
Copilot AI balanced review requested due to automatic review settings November 19, 2026 15:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread runtime/coreclr-bridge.m Resolved
@rolfbjarne rolfbjarne added backport-to-net11.0 Add this to issues or pull requests that should be backported to .NET 11 backport-to-net10.0 Add this to issues or pull requests that should be backported to .NET 10 labels Nov 19, 2026
…upported

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

Copy link
Copy Markdown
Member Author

@dalexsoto review

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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 dalexsoto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The peer-ownership and GC integration changes look good to me. The documented retainCount tradeoff and the stated scope limitations are understood.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-to-net10.0 Add this to issues or pull requests that should be backported to .NET 10 backport-to-net11.0 Add this to issues or pull requests that should be backported to .NET 11 copilot

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants