You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[runtime] Avoid weak-reference deadlocks during fork. Fixes #26844 - #26863
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.
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
🤖 💡 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.
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.
…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>
…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.
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
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.
Summary
Resolving an Objective-C weak reference takes a side-table lock before calling our
retainWeakReferenceoverride. For dynamically registered types, reading the managed wrapper's finalization flags then callsobjc_getAssociatedObject, which takes the association lock. Objective-C's fork preparation acquires those locks in the opposite order, so concurrent UI layout andProcess.Start()can deadlock.Cache
NSObjectDataalongside 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 duringcopyWithZone, 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