Skip to content

[runtime] Avoid weak-reference deadlocks during fork. Fixes #26844 - #26863

Merged
rolfbjarne merged 4 commits into
mainfrom
dev/rolf/fix-26844-weak-reference-deadlock
Oct 15, 2026
Merged

rolfbjarne merged 4 commits into
mainfrom
dev/rolf/fix-26844-weak-reference-deadlock

Conversation

@rolfbjarne

Copy link
Copy Markdown
Member

Summary

Resolving an Objective-C weak reference takes a side-table lock before calling our retainWeakReference override. For dynamically registered types, reading the managed wrapper's finalization flags then calls objc_getAssociatedObject, which takes the association lock. Objective-C's fork preparation acquires those locks in the opposite order, so concurrent UI layout and Process.Start() can deadlock.

Cache NSObjectData alongside GC handles and flags in the existing native dictionary, so the weak-reference callback no longer needs the associated-object API. Preserve the cached data when a GC handle is temporarily cleared, including during copyWithZone, and remove the entry during native deallocation before its address can be reused. Debug logging on this path also avoids Objective-C class queries.

Deallocation cleanup checks the startup-configured dynamic-registration support flag before taking the dictionary mutex, avoiding unnecessary locking when dynamic registration is disabled without an unsynchronized dictionary-pointer read. NativeAOT excludes this cleanup at compile time.

Fixes #26844

🤖 Pull request created by Copilot

rolfbjarne and others added 3 commits October 14, 2026 10:09
Objective-C resolves weak references while holding a side-table lock. Looking up NSObjectData through objc_getAssociatedObject in retainWeakReference then takes the association lock, inverting the lock order used by Objective-C fork preparation and deadlocking against concurrent Process.Start calls.

Cache NSObjectData alongside the dynamic registrar GCHandle and flags in the existing native dictionary. Preserve the data when a handle is temporarily cleared, including during copyWithZone, and remove the entry during native deallocation before the native address can be reused. Avoid Objective-C queries in the debug logging on this callback path as well.

Add a bounded regression that looks up NSObjectData while another thread holds the association lock, plus coverage for GCHandle updates, temporary clearing, and deallocation cleanup. The lock regression fails against the baseline and passes with this change. Dynamic and static CoreCLR weak-reference coverage passes, as does managed-wrapper resurrection. Three independent forced-fork repro launches each progressed continuously for 180 seconds; the matching baseline hung after 24 seconds.

Fixes #26844

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 5134b10c-37f3-4817-a039-555ae20a9caf
@rolfbjarne
rolfbjarne requested a review from dalexsoto as a code owner October 14, 2026 08:24
Copilot AI balanced review requested due to automatic review settings October 14, 2026 08:24

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

Low-level fork lock ordering and native deallocation behavior require final human and CI validation.

1 open finding
What changed in this PR

Prevents Objective-C weak-reference deadlocks during process forks by caching NSObjectData in the synchronized native GC-handle dictionary.

Changes:

  • Cache and clean up NSObjectData without association-lock access.
  • Preserve cached data across temporary GC-handle changes.
  • Add regression tests for lock independence, updates, and deallocation.
File Description
runtime/​trampolines.m Implements caching, synchronization, and cleanup.
tests/​test-libraries/​libtest.m Adds an association-lock test helper.
tests/​test-libraries/​libtest.h Declares the native helper.
tests/​monotouch-test/​ObjCRuntime/​NativeRuntimeTest.cs Adds regression tests.

🧠 Review effort: Balanced

Comment thread tests/test-libraries/libtest.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 Oct 14, 2026
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 14, 2026 08:40

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

Native runtime locking and deallocation changes warrant human review, and direct copyWithZone coverage is still missing.

0 open findings

1 resolved since last review
Previously missed (1)

In code that hasn't changed since last review

Low severity Add end-to-end copyWithZone regression test

tests/​monotouch-test/​ObjCRuntime/​NativeRuntimeTest.cs:79

🤖 💡 Testing — This test manually reproduces the two xamarinSetGCHandle calls, but it does not exercise either copyWithZone trampoline named by the regression. It would still pass if a future change stopped clearing/restoring the handle in xamarin_copyWithZone_trampoline1/2. Please add an end-to-end copy test for a dynamically registered copyable type and assert that the original object's cached NSObjectData survives the copy.

Rule: Bug fixes need regression tests

🧠 Review effort: Balanced

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ API diff for current PR / commit

NET (empty diffs)

✅ API diff vs stable

NET (empty diffs)

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: b7af6dec85933f4aa9bbd66575824e5b4f1529b6 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne

Copy link
Copy Markdown
Member Author

@dalexsoto review

@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 complete four-file source review found no high-confidence blocker. Weak retention avoids the Objective-C association-lock path while cached NSObjectData survives temporary handle clears and both copy trampolines, and native destruction removes the entry before address reuse. Synchronization, registrar/NativeAOT guards, native/managed ABI and the asynchronous test-helper output/cleanup paths are consistent.

This is source approval, not an executed-test or green-CI claim. The current-head iOS attempt's recorded failure is a simulator preflight launch refusal; its externally started rerun is still pending. The underlying Busy cause is not established.

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

🚀 [CI Build #b7af6de] Test results 🚀

Test results

✅ All tests passed on VSTS: test results.

🎉 All 221 tests passed 🎉

Tests counts

✅ assembly-processing: All 1 tests passed. Html Report (VSDrops) Download
✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): All 1 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (tvOS): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 5 tests passed. Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 4 tests passed. Html Report (VSDrops) Download
✅ introspection: All 4 tests passed. Html Report (VSDrops) Download
✅ linker (iOS): All 21 tests passed. Html Report (VSDrops) Download
✅ linker (MacCatalyst): All 21 tests passed. Html Report (VSDrops) Download
✅ linker (macOS): All 21 tests passed. Html Report (VSDrops) Download
✅ linker (tvOS): All 21 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 20 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 20 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 20 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 20 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ sharpie: All 1 tests passed. Html Report (VSDrops) Download
✅ windows: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

macOS tests

✅ Tests on macOS Sonoma (14): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sequoia (15): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Tahoe (26): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Golden Gate (27): All 5 tests passed. Html Report (VSDrops) Download

Linux Build Verification

✅ Linux build succeeded

Pipeline on Agent
Hash: b7af6dec85933f4aa9bbd66575824e5b4f1529b6 [PR build]

@rolfbjarne
rolfbjarne merged commit b0614b7 into main Oct 15, 2026
56 checks passed
@rolfbjarne
rolfbjarne deleted the dev/rolf/fix-26844-weak-reference-deadlock branch October 15, 2026 12:58
@rolfbjarne rolfbjarne added the ready-to-review This PR is ready to review/merge. label Oct 15, 2026
rolfbjarne added a commit that referenced this pull request Oct 27, 2026
…rk. Fixes #26844 (#27052)

## Summary

Resolving an Objective-C weak reference takes a side-table lock before calling our `retainWeakReference` override. For dynamically registered types, reading the managed wrapper's finalization flags then calls `objc_getAssociatedObject`, which takes the association lock. Objective-C's fork preparation acquires those locks in the opposite order, so concurrent UI layout and `Process.Start()` can deadlock.

Cache `NSObjectData` alongside GC handles and flags in the existing native dictionary, so the weak-reference callback no longer needs the associated-object API. Preserve the cached data when a GC handle is temporarily cleared, including during `copyWithZone`, and remove the entry during native deallocation before its address can be reused. Debug logging on this path also avoids Objective-C class queries.

Deallocation cleanup checks the startup-configured dynamic-registration support flag before taking the dictionary mutex, avoiding unnecessary locking when dynamic registration is disabled without an unsynchronized dictionary-pointer read. NativeAOT excludes this cleanup at compile time.

Fixes #26844

🤖 Pull request created by Copilot

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Rolf Bjarne Kvinge <rokvin@microsoft.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Backport of #26863.

---------

Co-authored-by: Rolf Bjarne Kvinge <rokvin@microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
rolfbjarne added a commit that referenced this pull request Oct 28, 2026
…rk. Fixes #26844 (#27051)

## Summary

Resolving an Objective-C weak reference takes a side-table lock before calling our `retainWeakReference` override. For dynamically registered types, reading the managed wrapper's finalization flags then calls `objc_getAssociatedObject`, which takes the association lock. Objective-C's fork preparation acquires those locks in the opposite order, so concurrent UI layout and `Process.Start()` can deadlock.

Cache `NSObjectData` alongside GC handles and flags in the existing native dictionary, so the weak-reference callback no longer needs the associated-object API. Preserve the cached data when a GC handle is temporarily cleared, including during `copyWithZone`, and remove the entry during native deallocation before its address can be reused. Debug logging on this path also avoids Objective-C class queries.

Deallocation cleanup checks the startup-configured dynamic-registration support flag before taking the dictionary mutex, avoiding unnecessary locking when dynamic registration is disabled without an unsynchronized dictionary-pointer read. NativeAOT excludes this cleanup at compile time.

Fixes #26844

🤖 Pull request created by Copilot

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Rolf Bjarne Kvinge <rokvin@microsoft.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Backport of #26863.
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 ready-to-review This PR is ready to review/merge.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Intense NSView.Layout + Process.Start() in the background leads to app hang

4 participants