From b41f5b04f513b5de19b6d57f980335d81e966410 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Mon, 29 Jun 2020 14:44:31 -0400
Subject: [PATCH] proxyclient: only restart listeners when connected

restartListeners() must be called only when the proxy is connected. Else, the
client will receive a lot of http errors
---
 include/opendht/dht_proxy_client.h |  5 ++---
 src/dht_proxy_client.cpp           | 12 ++++++++++--
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/include/opendht/dht_proxy_client.h b/include/opendht/dht_proxy_client.h
index 577cdae5..848ea0af 100644
--- a/include/opendht/dht_proxy_client.h
+++ b/include/opendht/dht_proxy_client.h
@@ -264,11 +264,10 @@ public:
     std::vector<unsigned> getNodeMessageStats(bool) override { return {}; }
     void setStorageLimit(size_t) override {}
     void connectivityChanged(sa_family_t) override {
-        restartListeners();
+        getProxyInfos();
     }
     void connectivityChanged() override {
         getProxyInfos();
-        restartListeners();
         loopSignal_();
     }
 
@@ -385,7 +384,7 @@ private:
     /**
      * Relaunch LISTEN requests if the client disconnect/reconnect.
      */
-    void restartListeners();
+    void restartListeners(const asio::error_code &ec);
 
     /**
      * Refresh a listen via a token
diff --git a/src/dht_proxy_client.cpp b/src/dht_proxy_client.cpp
index 18f9c525..247c9d6d 100644
--- a/src/dht_proxy_client.cpp
+++ b/src/dht_proxy_client.cpp
@@ -720,7 +720,7 @@ DhtProxyClient::onProxyInfos(const Json::Value& proxyInfos, const sa_family_t fa
     if (newStatus == NodeStatus::Connected) {
         if (oldStatus == NodeStatus::Disconnected || oldStatus == NodeStatus::Connecting) {
             listenerRestartTimer_->expires_at(std::chrono::steady_clock::now());
-            listenerRestartTimer_->async_wait(std::bind(&DhtProxyClient::restartListeners, this));
+            listenerRestartTimer_->async_wait(std::bind(&DhtProxyClient::restartListeners, this, std::placeholders::_1));
         }
         nextProxyConfirmationTimer_->expires_at(std::chrono::steady_clock::now() + std::chrono::minutes(15));
         nextProxyConfirmationTimer_->async_wait(std::bind(&DhtProxyClient::handleProxyConfirm, this, std::placeholders::_1));
@@ -1078,8 +1078,16 @@ DhtProxyClient::getConnectivityStatus()
 }
 
 void
-DhtProxyClient::restartListeners()
+DhtProxyClient::restartListeners(const asio::error_code &ec)
 {
+    if (ec == asio::error::operation_aborted)
+        return;
+    else if (ec){
+        if (logger_)
+            logger_->e("[proxy:client] restart error: %s", ec.message().c_str());
+        return;
+    }
+
     if (isDestroying_)
         return;
     if (logger_)
-- 
GitLab