Skip to content

Commit

Permalink
WIP: Files
Browse files Browse the repository at this point in the history
  • Loading branch information
gagik committed Jul 31, 2024
1 parent 819bb98 commit 953e657
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/realm/object-store/sync/sync_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,25 @@ void SyncManager::close_all_sessions()
get_sync_client().wait_for_session_terminations();
}


void SyncManager::clear_connection_change_callbacks()
{
// force_close() will call unregister_session(), which requires m_session_mutex,
// so we need to iterate over them without holding the lock.
decltype(m_sessions) sessions;
{
util::CheckedLockGuard lk(m_session_mutex);
m_sessions.swap(sessions);
}

for (auto& [_, session] : sessions) {
session->clear_connection_change_callbacks();
}

get_sync_client().wait_for_session_terminations();
}


void SyncManager::set_sync_route(std::string sync_route, bool verified)
{
REALM_ASSERT(!sync_route.empty()); // Cannot be set to empty string
Expand Down
2 changes: 2 additions & 0 deletions src/realm/object-store/sync/sync_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class SyncManager : public std::enable_shared_from_this<SyncManager> {
// Immediately closes any open sync sessions for this sync manager
void close_all_sessions() REQUIRES(!m_mutex, !m_session_mutex);

void clear_connection_change_callbacks() REQUIRES(!m_mutex, !m_session_mutex);

// Force all the active sessions to restart
void restart_all_sessions() REQUIRES(!m_mutex, !m_session_mutex);

Expand Down
18 changes: 17 additions & 1 deletion src/realm/object-store/sync/sync_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ void SyncSession::do_restart_session(util::CheckedUniqueLock)
become_active();
}

void SyncSession::clear_connection_change_callbacks()
{
m_connection_change_notifier.clear_callbacks();
}

void SyncSession::do_become_inactive(util::CheckedUniqueLock lock, Status status,
bool cancel_subscription_notifications)
{
Expand All @@ -224,8 +229,10 @@ void SyncSession::do_become_inactive(util::CheckedUniqueLock lock, Status status

// Send notifications after releasing the lock to prevent deadlocks in the callback.
if (old_state != new_state) {
m_connection_change_notifier.invoke_callbacks(old_state, connection_state());
// m_connection_change_notifier.invoke_callbacks(old_state, connection_state());
}
m_connection_change_notifier.clear_callbacks();


if (status.is_ok())
status = Status(ErrorCodes::OperationAborted, "Sync session became inactive");
Expand Down Expand Up @@ -1662,6 +1669,15 @@ void SyncSession::ConnectionChangeNotifier::remove_callback(uint64_t token)
}
}

void SyncSession::ConnectionChangeNotifier::clear_callbacks()
{
std::lock_guard<std::mutex> lock(m_callback_mutex);

m_callbacks.clear();
m_callback_count = 0;
m_callback_index = npos; // Reset callback index since all callbacks are removed
}

void SyncSession::ConnectionChangeNotifier::invoke_callbacks(ConnectionState old_state, ConnectionState new_state)
{
std::unique_lock lock(m_callback_mutex);
Expand Down
3 changes: 3 additions & 0 deletions src/realm/object-store/sync/sync_session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ class SyncSession : public std::enable_shared_from_this<SyncSession> {
uint64_t add_callback(std::function<ConnectionStateChangeCallback> callback);
void remove_callback(uint64_t token);
void invoke_callbacks(ConnectionState old_state, ConnectionState new_state);
void clear_callbacks();

private:
struct Callback {
Expand Down Expand Up @@ -445,6 +446,8 @@ class SyncSession : public std::enable_shared_from_this<SyncSession> {
void do_restart_session(util::CheckedUniqueLock)
REQUIRES(m_state_mutex, !m_connection_state_mutex, !m_config_mutex);

void clear_connection_change_callbacks() REQUIRES(!m_connection_state_mutex);

// do_become_inactive is called from both become_paused()/become_inactive() and does all the steps to
// shutdown and cleanup the sync session besides setting m_state.
void do_become_inactive(util::CheckedUniqueLock, Status, bool) RELEASE(m_state_mutex)
Expand Down

0 comments on commit 953e657

Please sign in to comment.