From ce12f0b37cc5ab3100fa41476521cd25e53be8cc Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Thu, 31 May 2018 22:26:21 +0300 Subject: [PATCH] Fixed error in ZK library [#CLICKHOUSE-3753] --- dbms/src/Common/ZooKeeper/ZooKeeperImpl.cpp | 16 +++++++++------- dbms/src/Common/ZooKeeper/ZooKeeperImpl.h | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/dbms/src/Common/ZooKeeper/ZooKeeperImpl.cpp b/dbms/src/Common/ZooKeeper/ZooKeeperImpl.cpp index bca15656eda..0222dd1aed5 100644 --- a/dbms/src/Common/ZooKeeper/ZooKeeperImpl.cpp +++ b/dbms/src/Common/ZooKeeper/ZooKeeperImpl.cpp @@ -996,13 +996,14 @@ void ZooKeeper::receiveEvent() void ZooKeeper::finalize(bool error_send, bool error_receive) { - std::unique_lock lock(finalize_mutex, std::defer_lock); - if (!lock.try_lock()) - return; + { + /// Will wait for pushRequest method. + std::unique_lock lock(expired_mutex); - if (expired) - return; - expired = true; + if (expired) + return; + expired = true; + } active_session_metric_increment.destroy(); @@ -1020,6 +1021,7 @@ void ZooKeeper::finalize(bool error_send, bool error_receive) /// This happens for example, when "Cannot push request to queue within operation timeout". tryLogCurrentException(__PRETTY_FUNCTION__); } + send_thread.join(); } @@ -1348,7 +1350,7 @@ void ZooKeeper::pushRequest(RequestInfo && info) /// to avoid forgotten operations in the queue when session is expired. /// Invariant: when expired, no new operations will be pushed to the queue in 'pushRequest' /// and the queue will be drained in 'finalize'. - std::lock_guard lock(finalize_mutex); + std::lock_guard lock(expired_mutex); if (expired) throw Exception("Session expired", ZSESSIONEXPIRED); diff --git a/dbms/src/Common/ZooKeeper/ZooKeeperImpl.h b/dbms/src/Common/ZooKeeper/ZooKeeperImpl.h index ad5facf7f6d..7b17b581403 100644 --- a/dbms/src/Common/ZooKeeper/ZooKeeperImpl.h +++ b/dbms/src/Common/ZooKeeper/ZooKeeperImpl.h @@ -573,7 +573,7 @@ private: std::atomic xid {1}; std::atomic expired {false}; - std::mutex finalize_mutex; + std::mutex expired_mutex; using clock = std::chrono::steady_clock;