Skip to content

Commit

Permalink
Handle SyncSession outliving the associated RealmCoordinator
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoyne committed Aug 12, 2021
1 parent 01daf60 commit a79de16
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
15 changes: 15 additions & 0 deletions src/realm/object-store/impl/realm_coordinator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ void RealmCoordinator::create_sync_session()
return;

open_db();
if (m_sync_session)
return;
m_sync_session = m_config.sync_config->user->sync_manager()->get_session(m_db, *m_config.sync_config);

std::weak_ptr<RealmCoordinator> weak_self = shared_from_this();
Expand Down Expand Up @@ -418,6 +420,19 @@ void RealmCoordinator::open_db()
if (m_db)
return;

if (m_config.sync_config) {
// If we previously opened this Realm, we may have a lingering sync
// session which outlived its RealmCoordinator. If that happens we
// want to reuse it instead of creating a new DB.
auto existing_session = m_config.sync_config->user->sync_manager()->get_existing_session(m_config.path);
if (existing_session) {
m_sync_session = existing_session;
m_db = SyncSession::Internal::get_db(*existing_session);
m_sync_session->revive_if_needed();
return;
}
}

bool server_synchronization_mode = m_config.sync_config || m_config.force_sync_history;
try {
if (m_config.immutable() && m_config.realm_data) {
Expand Down
5 changes: 5 additions & 0 deletions src/realm/object-store/sync/sync_session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ class SyncSession : public std::enable_shared_from_this<SyncSession> {
{
session.nonsync_transact_notify(version);
}

static std::shared_ptr<DB> get_db(SyncSession& session)
{
return session.m_db;
}
};

// Expose some internal functionality to testing code.
Expand Down
14 changes: 9 additions & 5 deletions test/object-store/sync/session/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,15 @@ TEMPLATE_TEST_CASE("sync: stop policy behavior", "[sync]", RegularUser)
});
}

// SECTION("transitions back to Active if the session is revived") {
// auto session2 = sync_manager->get_session(config.path, *config.sync_config);
// REQUIRE(session->state() == SyncSession::PublicState::Active);
// REQUIRE(session2 == session);
// }
SECTION("transitions back to Active if the session is revived") {
std::shared_ptr<SyncSession> session2;
{
auto realm = Realm::get_shared_realm(config);
session2 = user->sync_manager()->get_existing_session(config.path);
}
REQUIRE(session->state() == SyncSession::PublicState::Active);
REQUIRE(session2 == session);
}

SECTION("transitions to Inactive if a fatal error occurs") {
std::error_code code =
Expand Down

0 comments on commit a79de16

Please sign in to comment.