From a0deec960f3a8190831c673e5ba998fe6fb7ea90 Mon Sep 17 00:00:00 2001 From: Changli Gao Date: Sun, 20 Nov 2016 18:14:33 -0800 Subject: [PATCH] Fix deadlock when calling getMergedHistogram Summary: When calling StatisticsImpl::HistogramInfo::getMergedHistogram(), if there is a dying thread, which is calling ThreadLocalPtr::StaticMeta::OnThreadExit() to merge its thread values to HistogramInfo, deadlock will occur. Because the former try to hold merge_lock then ThreadMeta::mutex_, but the later try to hold ThreadMeta::mutex_ then merge_lock. In short, the locking order isn't the same. This patch addressed this issue by releasing merge_lock before folding thread values. Closes https://github.com/facebook/rocksdb/pull/1552 Differential Revision: D4211942 Pulled By: ajkr fbshipit-source-id: ef89bcb --- util/statistics.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/util/statistics.cc b/util/statistics.cc index a27001eea..051956839 100644 --- a/util/statistics.cc +++ b/util/statistics.cc @@ -51,9 +51,11 @@ uint64_t StatisticsImpl::getTickerCount(uint32_t tickerType) const { std::unique_ptr StatisticsImpl::HistogramInfo::getMergedHistogram() const { - MutexLock lock(&merge_lock); std::unique_ptr res_hist(new HistogramImpl()); - res_hist->Merge(merged_hist); + { + MutexLock lock(&merge_lock); + res_hist->Merge(merged_hist); + } thread_value->Fold( [](void* curr_ptr, void* res) { auto tmp_res_hist = static_cast(res);