From c690d060f93fae32bb0a646e374be98de9196af7 Mon Sep 17 00:00:00 2001 From: Seva <seva@binarytrails.net> Date: Thu, 5 Sep 2019 14:36:29 -0400 Subject: [PATCH] http: fix req cancel racing condition --- include/opendht/http.h | 1 + src/http.cpp | 32 ++++++++++++++++++-------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/include/opendht/http.h b/include/opendht/http.h index ca5f86aa..199dc6e5 100644 --- a/include/opendht/http.h +++ b/include/opendht/http.h @@ -99,6 +99,7 @@ public: void async_read(const size_t bytes, BytesHandlerCb cb); void timeout(const std::chrono::seconds timeout, HandlerCb cb = {}); + void close(); private: unsigned int id_; diff --git a/src/http.cpp b/src/http.cpp index a27ae183..276e919a 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -117,19 +117,21 @@ Connection::Connection(asio::io_context& ctx, std::shared_ptr<dht::crypto::Certi Connection::~Connection() { + close(); +} + +void +Connection::close() +{ + if (!is_open()) + return; asio::error_code ec; - if (is_open()){ - if (ssl_ctx_){ - ssl_socket_->cancel(ec); - ssl_socket_->close(ec); - } - else { - socket_->cancel(ec); - socket_->close(ec); - } - if (ec and logger_) - logger_->e("[http:client] [connection:%i] error closing: %s", id_, ec.message().c_str()); - } + if (ssl_ctx_) + ssl_socket_->close(ec); + else + socket_->close(ec); + if (ec and logger_) + logger_->e("[http:client] [connection:%i] error closing: %s", id_, ec.message().c_str()); } unsigned int @@ -442,7 +444,7 @@ Request::~Request() void Request::cancel() { - terminate(asio::error::eof); + conn_->close(); } unsigned int @@ -899,8 +901,10 @@ Request::handle_response_header(const asio::error_code& ec) void Request::handle_response_body(const asio::error_code& ec, const size_t bytes) { - if (ec == asio::error::operation_aborted) + if (ec == asio::error::operation_aborted){ + terminate(ec); return; + } else if ((ec == asio::error::eof) or (ec == asio::error::connection_reset)){ terminate(ec); return; -- GitLab