Skip to content
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

Fix data races in the connection state changes tests #7871

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
-----------

### Internals
* None.
* Fix a thread sanitizer failure in the "unregister connection change listener during callback" test ([PR #7871](https://github.com/realm/realm-core/pull/7871)).

----------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ TEST_CASE("sync: Connection state changes", "[sync][session][connection change]"

auto token1 = session->register_connection_change_callback(
[&](SyncSession::ConnectionState, SyncSession::ConnectionState) {
listener1_call_cnt = listener1_call_cnt + 1;
++listener1_call_cnt;
});
session->unregister_connection_change_callback(token1);
// One call may have been in progress when unregistered
Expand All @@ -87,16 +87,18 @@ TEST_CASE("sync: Connection state changes", "[sync][session][connection change]"
}

SECTION("unregister connection change listener during callback") {
uint64_t token1;
std::atomic<int> listener1_call_cnt(0);
std::atomic<bool> listener2_called(false);
int listener1_call_cnt = 0;
auto session = sync_session(
user, "/connection-state-changes-3", [](auto, auto) {}, SyncSessionStopPolicy::AfterChangesUploaded);
token1 = session->register_connection_change_callback(
std::mutex mutex;
std::unique_lock lock(mutex);
uint64_t token1 = session->register_connection_change_callback(
[&](SyncSession::ConnectionState, SyncSession::ConnectionState) {
listener1_call_cnt = listener1_call_cnt + 1;
std::lock_guard lock(mutex);
++listener1_call_cnt;
session->unregister_connection_change_callback(token1);
});
lock.unlock();

EventLoop::main().run_until([&] {
return sessions_are_active(*session);
Expand All @@ -105,6 +107,7 @@ TEST_CASE("sync: Connection state changes", "[sync][session][connection change]"
return sessions_are_connected(*session);
});

bool listener2_called = false;
session->register_connection_change_callback([&](SyncSession::ConnectionState, SyncSession::ConnectionState) {
listener2_called = true;
});
Expand Down
Loading