Skip to content
This repository was archived by the owner on Nov 8, 2021. It is now read-only.

Fix some async open issues #806

Merged
merged 9 commits into from
Jun 20, 2019
Merged

Fix some async open issues #806

merged 9 commits into from
Jun 20, 2019

Conversation

tgoyne
Copy link
Member

@tgoyne tgoyne commented Jun 19, 2019

This includes changes needed to get the realm-js and realm-cocoa tests passing using the new async open, and fixes a bug which hits Cocoa which wasn't caught by those tests.

AsyncOpenTask now takes more control over its lifetime: the callback holds a strong reference to the task until it's called so that retaining the AsyncOpenTask externally is optional, and cleans itself up after calling the callback passed to start() so that the SDK code doesn't have to worry about that. Some thread-safety problems around using m_session from multiple threads have been fixed by using AtomicSharedPtr<>, and calling cancel() while it happens to be processing the callback will no longer crash.

The state dir for async open/resync is now a directory named after the Realm rather than a single shared directory, since it turns out that's what the sync code expects. Sharing a single directory resulted in async opening more than one Realm at a time breaking.

The AsyncOpenTask callback is now passed a ThreadSafeReference<Realm> rather than std::shared_ptr<Realm>. The TSR can be resolved to an actual Realm by passing it to Realm::get_shared_realm(). This avoids creating the EventLoopSignal for the Realm on the background thread (which doesn't work with the libuv implementation because libuv event loops aren't thread-safe), and lets the JS SDK skip closing and re-opening the Realm back on the main thread. This also happened to reveal that the background-opened Realm wasn't really in a valid state, so a few fixes were needed there.

Async open failed to check that the history schema for existing Realm files was compatible before creating the sync session, which resulted in an exception being thrown on the sync client thread that did not get funneled back to the appropraite place.

tgoyne and others added 8 commits June 19, 2019 15:34
When we open the Realm on the background thread, rather than bind it to that
thread, leave it in an unbound state and pass it to the callback as a
ThreadSafeReference<Realm>, which the SDK can then bind to a thread at a later
time.

This works around an issue in the libuv version of EventLoopSignal where
creating it on a background thread doesn't work, and makes async open work
better with Realm caching without requiring that the SDK reopen the Realm on
the destination thread, which is a potentially somewhat expensive operation.
The state directory needs to be Realm-specific and not a global directory
shared by all Realms, so the previous version broke if more than one Realm
tried to use it at a time.
@tgoyne tgoyne added the T-Bug label Jun 19, 2019
@tgoyne tgoyne requested review from cmelchior and ironage June 19, 2019 23:10
@tgoyne tgoyne self-assigned this Jun 19, 2019
@tgoyne tgoyne merged commit 8cd7b40 into master Jun 20, 2019
@tgoyne tgoyne deleted the tg/async-open-fixes branch June 20, 2019 17:38
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants