From 500ae6ef54bcacf6e1b50b9f268bc7b18bb810b8 Mon Sep 17 00:00:00 2001
From: Seva <seva@binarytrails.net>
Date: Sat, 10 Aug 2019 15:34:02 -0400
Subject: [PATCH] http: handle asio cancel delays

---
 include/opendht/http.h | 1 +
 src/http.cpp           | 6 +++++-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/include/opendht/http.h b/include/opendht/http.h
index a31c8aee..469a8610 100644
--- a/include/opendht/http.h
+++ b/include/opendht/http.h
@@ -294,6 +294,7 @@ private:
     Response response_ {};
     std::string request_;
     std::atomic<bool> message_complete_ {false};
+    std::atomic<bool> finishing_ {false};
     std::unique_ptr<http_parser> parser_;
     std::unique_ptr<http_parser_settings> parser_s_;
 
diff --git a/src/http.cpp b/src/http.cpp
index 833d98ac..2b8290ff 100644
--- a/src/http.cpp
+++ b/src/http.cpp
@@ -53,7 +53,6 @@ Connection::~Connection()
     if (is_open()){
 #ifdef OPENDHT_PROXY_OPENSSL
         get_socket().cancel();
-        get_socket().shutdown(asio::ip::tcp::socket::shutdown_both);
 #endif
         get_socket().close();
     }
@@ -682,10 +681,15 @@ Request::post()
 void
 Request::terminate(const asio::error_code& ec)
 {
+    if (finishing_.load())
+        return;
+
     if (ec != asio::error::eof and ec != asio::error::operation_aborted)
         if (logger_)
             logger_->e("[http:request:%i] end with error: %s", id_, ec.message().c_str());
 
+    finishing_.store(true);
+
     // close the connection cancelling the scheduled socket operations on the io_context
     conn_.reset();
     // reset the http_parser holding the data pointer to the user callbacks
-- 
GitLab