Avoid deadlock between mutex_ and log_write_mutex_ (#5437)

Summary:
To avoid deadlock mutex_ should never be acquired before log_write_mutex_. The patch documents that and also fixes one case in ::FlushWAL that acquires mutex_ through ::WriteStatusCheck when it already holds lock on log_write_mutex_.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5437

Differential Revision: D15749722

Pulled By: maysamyabandeh

fbshipit-source-id: f57b69c44b4b80cc6d7ddf3d3fdf4a9eb5a5a45a
main
Maysam Yabandeh 6 years ago committed by Facebook Github Bot
parent b2584577fa
commit c8c1a549f0
  1. 11
      db/db_impl/db_impl.cc
  2. 2
      db/db_impl/db_impl.h

@ -1046,10 +1046,13 @@ int DBImpl::FindMinimumEmptyLevelFitting(
Status DBImpl::FlushWAL(bool sync) { Status DBImpl::FlushWAL(bool sync) {
if (manual_wal_flush_) { if (manual_wal_flush_) {
// We need to lock log_write_mutex_ since logs_ might change concurrently Status s;
InstrumentedMutexLock wl(&log_write_mutex_); {
log::Writer* cur_log_writer = logs_.back().writer; // We need to lock log_write_mutex_ since logs_ might change concurrently
auto s = cur_log_writer->WriteBuffer(); InstrumentedMutexLock wl(&log_write_mutex_);
log::Writer* cur_log_writer = logs_.back().writer;
s = cur_log_writer->WriteBuffer();
}
if (!s.ok()) { if (!s.ok()) {
ROCKS_LOG_ERROR(immutable_db_options_.info_log, "WAL flush error %s", ROCKS_LOG_ERROR(immutable_db_options_.info_log, "WAL flush error %s",
s.ToString().c_str()); s.ToString().c_str());

@ -1521,6 +1521,8 @@ class DBImpl : public DB {
// logfile_number_. With two_write_queues it also protects alive_log_files_, // logfile_number_. With two_write_queues it also protects alive_log_files_,
// and log_empty_. Refer to the definition of each variable below for more // and log_empty_. Refer to the definition of each variable below for more
// details. // details.
// Note: to avoid dealock, if needed to acquire both log_write_mutex_ and
// mutex_, the order should be first mutex_ and then log_write_mutex_.
InstrumentedMutex log_write_mutex_; InstrumentedMutex log_write_mutex_;
std::atomic<bool> shutting_down_; std::atomic<bool> shutting_down_;

Loading…
Cancel
Save