Keepalive: join the thread outside the background job itself.

Fixes #67, when etcd server stops, the keepalive action will crash.


Signed-off-by: Tao He <sighingnow@gmail.com>
This commit is contained in:
Tao He 2021-06-15 12:03:35 +08:00
parent b3a193c75e
commit 68c5626d2c
1 changed files with 4 additions and 5 deletions

View File

@ -101,6 +101,10 @@ etcd::KeepAlive::KeepAlive(std::string const & address,
etcd::KeepAlive::~KeepAlive() etcd::KeepAlive::~KeepAlive()
{ {
this->Cancel(); this->Cancel();
// clean up
if (task_.joinable()) {
task_.join();
}
} }
void etcd::KeepAlive::Cancel() void etcd::KeepAlive::Cancel()
@ -112,12 +116,7 @@ void etcd::KeepAlive::Cancel()
if (keepalive_timer_) { if (keepalive_timer_) {
keepalive_timer_->cancel(); keepalive_timer_->cancel();
} }
// clean up
context.stop(); context.stop();
if (task_.joinable()) {
task_.join();
}
} }
void etcd::KeepAlive::Check() { void etcd::KeepAlive::Check() {