Skip to content

Commit 8a68a7f

Browse files
committed
Simplify the SessionWrapper lifecycle a bit
Initializing a sync::Session was a multi-step process of creating the Session with a config, configuring some addition things via mutation functions, and then calling bind(). We can simplify this a bit by pushing everything into the config struct and binding inside the wrapper's constructor, eliminating the constructed-but-not-initialized state and letting us make more of the members const.
1 parent fd05f55 commit 8a68a7f

File tree

16 files changed

+747
-1171
lines changed

16 files changed

+747
-1171
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
### Internals
1919
* Work around a bug in VC++ that resulted in runtime errors when running the tests in a debug build (#[7741](https://github.com/realm/realm-core/issues/7741)).
20+
* Refactor `sync::Session` to eliminate the bind() step of session creation ([#7609](https://github.com/realm/realm-core/pull/7609)).
21+
* Add ScopeExitFail which only calls the handler if exiting the scope via an uncaught exception ([#7609](https://github.com/realm/realm-core/pull/7609)).
2022

2123
----------------------------------------------
2224

src/realm/db.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,10 +1189,9 @@ void DB::open(const std::string& path, const DBOptions& options)
11891189
SlabAlloc::DetachGuard alloc_detach_guard(alloc);
11901190
alloc.note_reader_start(this);
11911191
// must come after the alloc detach guard
1192-
auto handler = [this, &alloc]() noexcept {
1192+
auto reader_end_guard = make_scope_exit([this, &alloc]() noexcept {
11931193
alloc.note_reader_end(this);
1194-
};
1195-
auto reader_end_guard = make_scope_exit(handler);
1194+
});
11961195

11971196
// Check validity of top array (to give more meaningful errors
11981197
// early)

src/realm/object-store/sync/impl/sync_client.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ struct SyncClient {
123123
std::unique_ptr<sync::Session> make_session(std::shared_ptr<DB> db,
124124
std::shared_ptr<sync::SubscriptionStore> flx_sub_store,
125125
std::shared_ptr<sync::MigrationStore> migration_store,
126-
sync::Session::Config config)
126+
sync::Session::Config&& config)
127127
{
128128
return std::make_unique<sync::Session>(m_client, std::move(db), std::move(flx_sub_store),
129129
std::move(migration_store), std::move(config));

src/realm/object-store/sync/sync_session.cpp

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,7 @@ void SyncSession::become_active()
108108
}
109109

110110
// when entering from the Dying state the session will still be bound
111-
if (!m_session) {
112-
create_sync_session();
113-
m_session->bind();
114-
}
111+
create_sync_session();
115112

116113
// Register all the pending wait-for-completion blocks. This can
117114
// potentially add a redundant callback if we're coming from the Dying
@@ -896,6 +893,8 @@ void SyncSession::create_sync_session()
896893
SyncConfig& sync_config = *m_config.sync_config;
897894
REALM_ASSERT(sync_config.user);
898895

896+
std::weak_ptr<SyncSession> weak_self = weak_from_this();
897+
899898
sync::Session::Config session_config;
900899
session_config.signed_user_token = sync_config.user->access_token();
901900
session_config.user_id = sync_config.user->user_id();
@@ -912,8 +911,8 @@ void SyncSession::create_sync_session()
912911

913912
if (sync_config.on_sync_client_event_hook) {
914913
session_config.on_sync_client_event_hook = [hook = sync_config.on_sync_client_event_hook,
915-
anchor = weak_from_this()](const SyncClientHookData& data) {
916-
return hook(anchor, data);
914+
weak_self](const SyncClientHookData& data) {
915+
return hook(weak_self, data);
917916
};
918917
}
919918

@@ -954,46 +953,41 @@ void SyncSession::create_sync_session()
954953
m_server_requests_action = sync::ProtocolErrorInfo::Action::NoAction;
955954
}
956955

957-
m_session = m_client.make_session(m_db, m_flx_subscription_store, m_migration_store, std::move(session_config));
958-
959-
std::weak_ptr<SyncSession> weak_self = weak_from_this();
960-
961-
// Set up the wrapped progress handler callback
962-
m_session->set_progress_handler([weak_self](uint_fast64_t downloaded, uint_fast64_t downloadable,
963-
uint_fast64_t uploaded, uint_fast64_t uploadable,
964-
uint_fast64_t snapshot_version, double download_estimate,
965-
double upload_estimate, int64_t query_version) {
956+
session_config.progress_handler = [weak_self](uint_fast64_t downloaded, uint_fast64_t downloadable,
957+
uint_fast64_t uploaded, uint_fast64_t uploadable,
958+
uint_fast64_t snapshot_version, double download_estimate,
959+
double upload_estimate, int64_t query_version) {
966960
if (auto self = weak_self.lock()) {
967961
self->handle_progress_update(downloaded, downloadable, uploaded, uploadable, snapshot_version,
968962
download_estimate, upload_estimate, query_version);
969963
}
970-
});
964+
};
971965

972-
// Sets up the connection state listener. This callback is used for both reporting errors as well as changes to
973-
// the connection state.
974-
m_session->set_connection_state_change_listener(
975-
[weak_self](sync::ConnectionState state, std::optional<sync::SessionErrorInfo> error) {
976-
using cs = sync::ConnectionState;
977-
ConnectionState new_state = [&] {
978-
switch (state) {
979-
case cs::disconnected:
980-
return ConnectionState::Disconnected;
981-
case cs::connecting:
982-
return ConnectionState::Connecting;
983-
case cs::connected:
984-
return ConnectionState::Connected;
985-
}
986-
REALM_UNREACHABLE();
987-
}();
988-
// If the OS SyncSession object is destroyed, we ignore any events from the underlying Session as there is
989-
// nothing useful we can do with them.
990-
if (auto self = weak_self.lock()) {
991-
self->update_connection_state(new_state);
992-
if (error) {
993-
self->handle_error(std::move(*error));
994-
}
966+
session_config.connection_state_change_listener = [weak_self](sync::ConnectionState state,
967+
std::optional<sync::SessionErrorInfo> error) {
968+
using cs = sync::ConnectionState;
969+
ConnectionState new_state = [&] {
970+
switch (state) {
971+
case cs::disconnected:
972+
return ConnectionState::Disconnected;
973+
case cs::connecting:
974+
return ConnectionState::Connecting;
975+
case cs::connected:
976+
return ConnectionState::Connected;
995977
}
996-
});
978+
REALM_UNREACHABLE();
979+
}();
980+
// If the OS SyncSession object is destroyed, we ignore any events from the underlying Session as there is
981+
// nothing useful we can do with them.
982+
if (auto self = weak_self.lock()) {
983+
self->update_connection_state(new_state);
984+
if (error) {
985+
self->handle_error(std::move(*error));
986+
}
987+
}
988+
};
989+
990+
m_session = m_client.make_session(m_db, m_flx_subscription_store, m_migration_store, std::move(session_config));
997991
}
998992

999993
void SyncSession::update_connection_state(ConnectionState new_state)

0 commit comments

Comments
 (0)