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 a lock order inversion in tests #6666

Merged
merged 1 commit into from
May 25, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

### Internals
* Simplify the implementation of query expression nodes which have a btree leaf cache.
* Fix a lock order inversion hit by object store tests running on linux. The cycle required test-specific code and so is not applicable to non-tests.

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

Expand Down
29 changes: 14 additions & 15 deletions src/realm/object-store/impl/realm_coordinator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,24 +644,26 @@ void RealmCoordinator::unregister_realm(Realm* realm)
}
}

// Thread-safety analsys doesn't reasonably handle calling functions on different
// Thread-safety analysis doesn't reasonably handle calling functions on different
// instances of this type
void RealmCoordinator::clear_cache() NO_THREAD_SAFETY_ANALYSIS
{
std::vector<std::shared_ptr<Realm>> realms_to_close;
std::vector<std::shared_ptr<RealmCoordinator>> coordinators;
{
std::lock_guard<std::mutex> lock(s_coordinator_mutex);

for (auto& weak_coordinator : s_coordinators_per_path) {
auto coordinator = weak_coordinator.second.lock();
if (!coordinator) {
continue;
if (auto coordinator = weak_coordinator.second.lock()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small thing, before we were checking if the coordinator was valid before to adding it to the lists of coordinators, it seems that now we can assume that there will always be a valid instance to use.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same check just written slightly differently.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah, the assignment is done inside the if.. my bad

coordinators.push_back(coordinator);
}
coordinators.push_back(coordinator);
}
s_coordinators_per_path.clear();
}

coordinator->m_notifier = nullptr;
for (auto& coordinator : coordinators) {
coordinator->m_notifier = nullptr;

std::vector<std::shared_ptr<Realm>> realms_to_close;
{
// Gather a list of all of the realms which will be removed
util::CheckedLockGuard lock(coordinator->m_realm_mutex);
for (auto& weak_realm_notifier : coordinator->m_weak_realm_notifiers) {
Expand All @@ -671,14 +673,11 @@ void RealmCoordinator::clear_cache() NO_THREAD_SAFETY_ANALYSIS
}
}

s_coordinators_per_path.clear();
// Close all of the previously cached Realms. This can't be done while
// locks are held as it may try to re-lock them.
for (auto& realm : realms_to_close)
realm->close();
}
coordinators.clear();

// Close all of the previously cached Realms. This can't be done while
// s_coordinator_mutex is held as it may try to re-lock it.
for (auto& realm : realms_to_close)
realm->close();
}

void RealmCoordinator::clear_all_caches()
Expand Down