-
Notifications
You must be signed in to change notification settings - Fork 165
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
Ensure that all native callbacks are setup always #2331
Conversation
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.
Looks ok to me, at least for the parts I can understand :)
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.
Thank you for the long intro in the PR, it's helped getting a bit of context. I tried to follow all the points you wrote and they seem to be reflected in code.
I'm rather confident that this PR is going to be a good candidate for the next PR review, at the very list to tell some of the background behind the discovery of the reasons for the hangs in Unity.
8d0b231
to
8c285c8
Compare
Needs realm/realm-core#4635 before it's merged. |
56a196f
to
d8d04c3
Compare
d8d04c3
to
6bb026d
Compare
This is another fun episode of "why does the Unity Editor hang". Here's a list of the high level changes:
should_compact_callback
from the native configuration and moved the static initialization toRealmHandle.Initialize
. We were always passing a pointer to the same managed method, so figured it makes sense to only initialize once.RealmConfiguration.CreateRealm
now disposes theMigration
instance and the GCHandle referencing theShouldCompactDelegate
in a finally block after creating a Realm instance. This is an alternative fix for the leak reported in Fix memory leak. #1788.SharedRealmHandle
/ObjectHandle
. Previously those exposed an API where you needed to pass anAction<Schema>
and now everything is handled internally and they just return theSchema
that would have otherwise been passed to the callback.NativeCommon.Initialize
now invokes all handle initializers to install the managed callbacks. This is done to avoid bugs where the user creates anApp
, logs a user in, but doesn't open a Realm instance from managed code. This would then result in a bug where the native sync manager would open the metadata Realm to store the user's information but would then fail to do that because the synchronization context hooks haven't been setup, so it'll be calling into the void trying to check if there's a synchronization context on the background thread.AppHandle
's andSharedRealmHandle
's static constructors will callNativeCommon.Initialize
which should ensure there's no way to call into native without first initializing the callbacks.ApiKeysCallback
fromSyncUserHandle
toAppHandle
to streamline the callback setup (it was the only callback setup by theSyncUserHandle
).TaskCompletionSource<T>
argument, would create aGCHandle
for it and then rely on the callback to free it. This was both annoying to consume and error prone (there was one case where we forgot to free a handle correctly). Instead, now all async APIs will encapsulate thetcs
creation, invoking the native API, awaiting thetcs.Task
and then freeing the GCHandle in a finally block (these are most of the changes inAppHandle
andSyncUserHandle
).App::clear_cached_apps
when unloading the AppDomain.In terms of the actual reason for the hangs, they were a couple: