diff --git a/src/Watcher.cpp b/src/Watcher.cpp index 99114e3..334be24 100644 --- a/src/Watcher.cpp +++ b/src/Watcher.cpp @@ -298,10 +298,12 @@ void etcd::Watcher::doWatch(std::string const & key, stubs->call->waitForResponse(callback); if (wait_callback != nullptr) { // issue the callback in another thread (detached) to avoid deadlock, - // it is ok to detach a pplx::task, but we cannot use the pplx::task + // it is ok to detach a pplx::task, but we don't want to use the pplx::task // in the core library - std::thread canceller([this]() { - wait_callback(stubs->call->Cancelled()); + bool cancelled = stubs->call->Cancelled(); + std::function wait_callback = this->wait_callback; + std::thread canceller([wait_callback, cancelled]() { + wait_callback(cancelled); }); canceller.detach(); } diff --git a/tst/RewatchTest.cpp b/tst/RewatchTest.cpp index e1e8ead..0ff7c92 100644 --- a/tst/RewatchTest.cpp +++ b/tst/RewatchTest.cpp @@ -8,7 +8,7 @@ #include "etcd/SyncClient.hpp" #include "etcd/Watcher.hpp" -static const std::string etcd_url("http://127.0.0.1:2379"); +static const std::string etcd_url("http://127.0.0.1:24799"); static int watcher_called = 0; @@ -33,9 +33,17 @@ void print_response(etcd::Response const & resp) } } -void wait_for_connection(etcd::Client &client) { - // wait until the client connects to etcd server - while (!client.head().get().is_ok()) { +void wait_for_connection(std::string endpoints) { + // wait until the client connects to etcd server + while (true) { + try { + etcd::Client client(endpoints); + if (client.head().get().is_ok()) { + break; + } + } catch (...) { + // pass + } sleep(1); } } @@ -44,8 +52,9 @@ void initialize_watcher(const std::string& endpoints, const std::string& prefix, std::function callback, std::shared_ptr& watcher) { + // wait until the endpoints turn to be available + wait_for_connection(endpoints); etcd::Client client(endpoints); - wait_for_connection(client); // Check if the failed one has been cancelled first if (watcher && watcher->Cancelled()) {