Skip to content

Commit

Permalink
Do not lock destroyed mutex after wait_for_upload timeout (#5944)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbreams authored Oct 18, 2022
1 parent 145e136 commit 0e3d874
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions test/object-store/util/test_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,29 +214,37 @@ std::string SyncServer::url_for_realm(StringData realm_name) const
return util::format("%1/%2", m_url, realm_name);
}

struct WaitForSessionState {
std::condition_variable cv;
std::mutex mutex;
bool complete = false;
std::error_code ec;
};

static std::error_code wait_for_session(Realm& realm,
void (SyncSession::*fn)(util::UniqueFunction<void(std::error_code)>&&),
std::chrono::seconds timeout)
{
std::condition_variable cv;
std::mutex wait_mutex;
bool wait_flag(false);
std::error_code ec;
auto shared_state = std::make_shared<WaitForSessionState>();
auto& session = *realm.config().sync_config->user->session_for_on_disk_path(realm.config().path);
(session.*fn)([&](std::error_code error) {
std::unique_lock<std::mutex> lock(wait_mutex);
wait_flag = true;
ec = error;
cv.notify_one();
(session.*fn)([weak_state = std::weak_ptr<WaitForSessionState>(shared_state)](std::error_code error) {
auto shared_state = weak_state.lock();
if (!shared_state) {
return;
}
std::lock_guard<std::mutex> lock(shared_state->mutex);
shared_state->complete = true;
shared_state->ec = error;
shared_state->cv.notify_one();
});
std::unique_lock<std::mutex> lock(wait_mutex);
bool completed = cv.wait_for(lock, timeout, [&]() {
return wait_flag == true;
std::unique_lock<std::mutex> lock(shared_state->mutex);
bool completed = shared_state->cv.wait_for(lock, timeout, [&]() {
return shared_state->complete == true;
});
if (!completed) {
throw std::runtime_error("wait_For_session() timed out");
}
return ec;
return shared_state->ec;
}

std::error_code wait_for_upload(Realm& realm, std::chrono::seconds timeout)
Expand Down

0 comments on commit 0e3d874

Please sign in to comment.