From e2d7dbc2937acb914195de3d16d675a220c864e7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Tue, 5 Nov 2019 16:39:03 -0500
Subject: [PATCH] proxy client: declare mutex before protected objects

---
 include/opendht/dht_proxy_client.h | 28 ++++++++++++++--------------
 src/dht_proxy_client.cpp           |  2 ++
 2 files changed, 16 insertions(+), 14 deletions(-)

diff --git a/include/opendht/dht_proxy_client.h b/include/opendht/dht_proxy_client.h
index 2c0c3fb4..80198a7f 100644
--- a/include/opendht/dht_proxy_client.h
+++ b/include/opendht/dht_proxy_client.h
@@ -330,18 +330,6 @@ private:
     //std::pair<std::string, std::string> serverHostService_;
     std::string pushClientId_;
 
-    /*
-     * ASIO I/O Context for sockets in httpClient_
-     * Note: Each context is used in one thread only
-     */
-    asio::io_context httpContext_;
-    std::shared_ptr<http::Resolver> resolver_;
-    std::map<unsigned int /*id*/, std::shared_ptr<http::Request>> requests_;
-    /*
-     * Thread for executing the http io_context.run() blocking call
-     */
-    std::thread httpClientThread_;
-
     mutable std::mutex lockCurrentProxyInfos_;
     NodeStatus statusIpv4_ {NodeStatus::Disconnected};
     NodeStatus statusIpv6_ {NodeStatus::Disconnected};
@@ -355,20 +343,32 @@ private:
     // registred types
     TypeStore types;
 
+    /*
+     * ASIO I/O Context for sockets in httpClient_
+     * Note: Each context is used in one thread only
+     */
+    asio::io_context httpContext_;
+    std::shared_ptr<http::Resolver> resolver_;
+    std::map<unsigned int /*id*/, std::shared_ptr<http::Request>> requests_;
+    /*
+     * Thread for executing the http io_context.run() blocking call
+     */
+    std::thread httpClientThread_;
+
     /**
      * Store listen requests.
      */
     struct ProxySearch;
 
+    mutable std::mutex searchLock_;
     size_t listenerToken_ {0};
     std::map<InfoHash, ProxySearch> searches_;
-    mutable std::mutex searchLock_;
 
     /**
      * Callbacks should be executed in the main thread.
      */
-    std::vector<std::function<void()>> callbacks_;
     std::mutex lockCallbacks_;
+    std::vector<std::function<void()>> callbacks_;
 
     Sp<InfoState> infoState_;
 
diff --git a/src/dht_proxy_client.cpp b/src/dht_proxy_client.cpp
index 73b97486..3495802a 100644
--- a/src/dht_proxy_client.cpp
+++ b/src/dht_proxy_client.cpp
@@ -981,6 +981,8 @@ DhtProxyClient::sendListen(const restinio::http_request_header_t header,
 void
 DhtProxyClient::opFailed()
 {
+    if (isDestroying_)
+        return;
     if (logger_)
         logger_->e("[proxy:client] proxy request failed");
     {
-- 
GitLab