diff --git a/include/opendht/dhtrunner.h b/include/opendht/dhtrunner.h index 09229eb9e24af2e6eea4bf33e564aa908f555f57..b8e0572f380f48716afcc5649e9aae01ced7a6c3 100644 --- a/include/opendht/dhtrunner.h +++ b/include/opendht/dhtrunner.h @@ -388,6 +388,7 @@ private: std::thread bootstrap_thread {}; /** protects bootstrap_nodes, bootstrap_thread */ std::mutex bootstrap_mtx {}; + std::condition_variable bootstrap_cv {}; std::queue<std::function<void(SecureDht&)>> pending_ops_prio {}; std::queue<std::function<void(SecureDht&)>> pending_ops {}; diff --git a/src/dhtrunner.cpp b/src/dhtrunner.cpp index 0d5e2b2b329a27a4793a0371a87cc1bd6178be32..a8a3cf0287fdd33dd27337e1ea26758481e004d9 100644 --- a/src/dhtrunner.cpp +++ b/src/dhtrunner.cpp @@ -129,6 +129,7 @@ DhtRunner::join() { running = false; cv.notify_all(); + bootstrap_cv.notify_all(); if (dht_thread.joinable()) dht_thread.join(); if (rcv_thread.joinable()) @@ -606,16 +607,21 @@ DhtRunner::tryBootstrapCoutinuously() next += BOOTSTRAP_PERIOD; { - std::mutex mtx; // dummy mutex + std::mutex mtx; std::unique_lock<std::mutex> blck(mtx); - std::atomic<unsigned> ping_count(0); + unsigned ping_count(0); // Reverse: try last inserted bootstrap nodes first for (auto it = nodes.rbegin(); it != nodes.rend(); it++) { ++ping_count; try { bootstrap(getAddrInfo(it->first, it->second), [&](bool) { - --ping_count; - cv.notify_all(); + if (not running) + return; + { + std::unique_lock<std::mutex> blck(mtx); + --ping_count; + } + bootstrap_cv.notify_all(); }); } catch (std::invalid_argument& e) { --ping_count; @@ -623,10 +629,10 @@ DhtRunner::tryBootstrapCoutinuously() } } // wait at least until the next BOOTSTRAP_PERIOD - cv.wait_until(blck, next, [&]() { return not running; }); + bootstrap_cv.wait_until(blck, next, [&]() { return not running; }); // wait for bootstrap requests to end. if (running) - cv.wait(blck, [&]() { return not running or ping_count == 0; }); + bootstrap_cv.wait(blck, [&]() { return not running or ping_count == 0; }); } // update state { diff --git a/src/network_engine.cpp b/src/network_engine.cpp index 9141bc4fcca3d591e33fe27e53dfb9b488a1ab1f..18ce38c0f6060c153cf03213942dbe0c2d4829b5 100644 --- a/src/network_engine.cpp +++ b/src/network_engine.cpp @@ -250,8 +250,10 @@ NetworkEngine::sendRequest(std::shared_ptr<Request>& request) { request->start = scheduler.time(); auto e = requests.emplace(request->tid, request); - if (!e.second) + if (!e.second) { DHT_LOG_ERR("Request already existed (tid: %d)!", request->tid); + return; + } request->node->requested(request); requestStep(request); }