diff --git a/src/dht.cpp b/src/dht.cpp index 8c9cfecd..45ae2310 100644 --- a/src/dht.cpp +++ b/src/dht.cpp @@ -889,10 +889,16 @@ Dht::search(const InfoHash& id, sr = std::make_shared(); srs.emplace(id, sr); } else { - for (auto it = srs.begin(); it != srs.end();) { + for (auto it = srs.begin(); it != srs.end(); ++it) { auto& s = *it->second; if ((s.done or s.expired) and s.announce.empty() and s.listeners.empty()) { sr = it->second; + if (sr->nextSearchStep) + scheduler.cancel(sr->nextSearchStep); + if (sr->opExpirationJob) + scheduler.cancel(sr->opExpirationJob); + sr->clear(); + srs.erase(it); break; } } @@ -901,8 +907,11 @@ Dht::search(const InfoHash& id, logger_->error("[search {} IPv{}] Maximum number of searches reached!", id.toString(), (af == AF_INET) ? '4' : '6'); + if (dcb) + dcb(false, {}); return {}; } + srs.emplace(id, sr); } sr->af = af; sr->tid = search_id++; diff --git a/src/op_cache.cpp b/src/op_cache.cpp index 885130fe..74905073 100644 --- a/src/op_cache.cpp +++ b/src/op_cache.cpp @@ -210,6 +210,13 @@ SearchCache::cancelAll(const std::function& onCancel) ops.clear(); } +void +SearchCache::clear() +{ + ops.clear(); + nextExpiration_ = time_point::max(); +} + time_point SearchCache::expire(const time_point& now, const std::function& onCancel) { diff --git a/src/op_cache.h b/src/op_cache.h index ce843334..00c68b05 100644 --- a/src/op_cache.h +++ b/src/op_cache.h @@ -164,6 +164,7 @@ public: bool cancelListen(size_t gtoken, const time_point& now); void cancelAll(const std::function& onCancel); + void clear(); time_point expire(const time_point& now, const std::function& onCancel); time_point getExpiration() const { return nextExpiration_; } diff --git a/src/search.h b/src/search.h index cb231ceb..9a1bcdc5 100644 --- a/src/search.h +++ b/src/search.h @@ -836,6 +836,7 @@ struct Dht::Search listeners.clear(); nodes.clear(); nextSearchStep.reset(); + cache.clear(); } };