Skip to content

Commit

Permalink
fix: deadlock in Heartbeat() (#3530)
Browse files Browse the repository at this point in the history
* acquire and immediately release db_slice.GetSerializationMutex() in Heartbeat()

---------

Signed-off-by: kostas <[email protected]>
  • Loading branch information
kostasrim authored Aug 20, 2024
1 parent b979994 commit 4835b5d
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/server/engine_shard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -633,8 +633,15 @@ void EngineShard::Heartbeat() {
void EngineShard::RetireExpiredAndEvict() {
// TODO: iterate over all namespaces
DbSlice& db_slice = namespaces.GetDefaultNamespace().GetDbSlice(shard_id());
// Some of the functions below might acquire the lock again so we need to unlock it
std::unique_lock lk(db_slice.GetSerializationMutex());
// Some of the functions below might acquire the same lock again so we need to unlock it
// asap. We won't yield before we relock the mutex again, so the code below is atomic
// in respect to preemptions of big values. An example of that is the call to
// DeleteExpiredStep() below, which eventually calls ExpireIfNeeded()
// and within that the call to RecordExpiry() will trigger the registered
// callback OnJournalEntry which locks the exact same mutex.
// We need to lock below and immediately release because there should be no other fiber
// that is serializing a big value.
{ std::unique_lock lk(db_slice.GetSerializationMutex()); }
constexpr double kTtlDeleteLimit = 200;
constexpr double kRedLimitFactor = 0.1;

Expand Down Expand Up @@ -676,8 +683,6 @@ void EngineShard::RetireExpiredAndEvict() {
}
}

// Because TriggerOnJournalWriteToSink will lock the same lock leading to a deadlock.
lk.unlock();
// Journal entries for expired entries are not writen to socket in the loop above.
// Trigger write to socket when loop finishes.
if (auto journal = EngineShard::tlocal()->journal(); journal) {
Expand Down

0 comments on commit 4835b5d

Please sign in to comment.