mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-24 08:32:02 +00:00
Merge pull request #48849 from ClickHouse/fix_zookeeper_join_race
Fix for race in ZooKeeper when joining send_thread/receive_thread
This commit is contained in:
commit
2728ce2979
@ -299,10 +299,7 @@ ZooKeeper::~ZooKeeper()
|
||||
{
|
||||
finalize(false, false, "Destructor called");
|
||||
|
||||
if (send_thread.joinable())
|
||||
send_thread.join();
|
||||
|
||||
if (receive_thread.joinable())
|
||||
receive_thread.join();
|
||||
}
|
||||
catch (...)
|
||||
@ -365,10 +362,7 @@ ZooKeeper::ZooKeeper(
|
||||
{
|
||||
tryLogCurrentException(log, "Failed to connect to ZooKeeper");
|
||||
|
||||
if (send_thread.joinable())
|
||||
send_thread.join();
|
||||
|
||||
if (receive_thread.joinable())
|
||||
receive_thread.join();
|
||||
|
||||
throw;
|
||||
@ -914,7 +908,6 @@ void ZooKeeper::finalize(bool error_send, bool error_receive, const String & rea
|
||||
}
|
||||
|
||||
/// Send thread will exit after sending close request or on expired flag
|
||||
if (send_thread.joinable())
|
||||
send_thread.join();
|
||||
}
|
||||
|
||||
@ -932,7 +925,7 @@ void ZooKeeper::finalize(bool error_send, bool error_receive, const String & rea
|
||||
tryLogCurrentException(log);
|
||||
}
|
||||
|
||||
if (!error_receive && receive_thread.joinable())
|
||||
if (!error_receive)
|
||||
receive_thread.join();
|
||||
|
||||
{
|
||||
|
@ -255,8 +255,30 @@ private:
|
||||
Watches watches TSA_GUARDED_BY(watches_mutex);
|
||||
std::mutex watches_mutex;
|
||||
|
||||
ThreadFromGlobalPool send_thread;
|
||||
ThreadFromGlobalPool receive_thread;
|
||||
/// A wrapper around ThreadFromGlobalPool that allows to call join() on it from multiple threads.
|
||||
class ThreadReference
|
||||
{
|
||||
public:
|
||||
const ThreadReference & operator = (ThreadFromGlobalPool && thread_)
|
||||
{
|
||||
std::lock_guard<std::mutex> l(lock);
|
||||
thread = std::move(thread_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void join()
|
||||
{
|
||||
std::lock_guard<std::mutex> l(lock);
|
||||
if (thread.joinable())
|
||||
thread.join();
|
||||
}
|
||||
private:
|
||||
std::mutex lock;
|
||||
ThreadFromGlobalPool thread;
|
||||
};
|
||||
|
||||
ThreadReference send_thread;
|
||||
ThreadReference receive_thread;
|
||||
|
||||
Poco::Logger * log;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user