-
Notifications
You must be signed in to change notification settings - Fork 576
[UIKit] Protect RegisterForTraitChanges observers from premature GC toggle-ref collection #26431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
linnkrb
wants to merge
4
commits into
dotnet:main
Choose a base branch
from
linnkrb:fix/trait-change-observable-toggleref
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ce35016
[UIKit] Protect RegisterForTraitChanges observers from premature GC t…
176f8db
Merge branch 'main' into fix/trait-change-observable-toggleref
linnkrb 32124a7
Merge branch 'main' into fix/trait-change-observable-toggleref
linnkrb 917c28e
Merge branch 'main' into fix/trait-change-observable-toggleref
linnkrb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reasoning here is incorrect:
MarkDirtyis used when a managed peer contains managed state, and mustn't be collected by the GC before the native object. It does not change the lifetime of the native object, only the managed object.The correct fix is to make sure the
IUITraitChangeObservableinstance isn't collected by the GC before callingUnregisterForTraitChangeson it. However, if you're callingUnregisterForTraitChanges, you must keep the instance around somewhere, which would prevent the GC from collecting it, so I'm guessing you're not callingUnregisterForTraitChanges?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks — that makes sense, and it points at the actual bug better than my patch does.
To answer directly: I checked, and at least the one
RegisterForTraitChangescall site I could find in dotnet/maui (SwitchHandler.iOS.cs,SwitchProxy) does callUnregisterForTraitChanges— inDisconnect(platformView), which runs fromDisconnectHandler. So it's not simply "no one calls Unregister."The gap is that
DisconnectHandlerisn't guaranteed to run before the platform view's managed peer is collected. We've hit this repeatedly in our own MAUI app (iOS): under GC pressure, a handler's platform view can be finalized without an explicitDisconnectHandler()call ever firing — we've had to build a whole set of patterns around it (window-null teardown guards,IDestructible.Destroy()hooks,GC.SuppressFinalizepinning on ~26 custom handlers) specifically because relying onDisconnect*running reliably isn't safe. If that's what's happening toUISwitch/SwitchProxyhere too,UnregisterForTraitChangessilently never runs, the closure-capturingSwitchProxygets collected while_UITraitChangeRegistrystill holds a now-dangling pointer to it, and the registry corruption surfaces later at unrelated call sites — which matches what we're seeing (UICollectionViewteardown, gesture-node updates,ScrollEdgeEffectView,UITextFieldconstruction, none of which touchRegisterForTraitChangesthemselves).Given that, I think the fix needs to live in
RegisterForTraitChanges/UnregisterForTraitChangesitself rather than at each call site: take a strongGCHandleon the observable when it registers, keyed by the returnedIUITraitChangeRegistration, and free it whenUnregisterForTraitChangesis called. That guarantees the object can't be collected while it's live in the registry — and if a caller'sDisconnect/Unregisterpath never runs (as above), the failure mode becomes a leak instead of a dangling pointer, which is a much safer place to be while any missing-unregister call sites get found and fixed properly.Happy to move the PR in that direction if that sounds right to you — want me to take a pass at it there instead of at the
RegisterForTraitChangescall sites?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that makes sense.
There's already a precedent for something similar, NSObject.AddObserver returns an object that must be disposed to stop observing, and if that object isn't disposed manually, then a warning is printed (because presumably the GC collected the object because the developer didn't keep a reference to it):
macios/src/Foundation/NSObject2.cs
Lines 1324 to 1366 in 10b38c6
One idea could be to do something similar: create a new internal class that implements the
IUITraitChangeRegistrationinterface:IUITraitChangeObservableinstance.IUITraitChangeRegistrationinstance returned from the native registerForTraitChanges APIRegisterForTraitChangescall.UnregisterForTraitChangeswould be needed, and handle getting passed the new internal class correctly (free the GCHandle, etc.)I'll have a look at doing this, it's not trivial.