Skip to content
Permalink
Browse files

http: fix resolver destruction race condition

  • Loading branch information...
binarytrails authored and aberaud committed Sep 9, 2019
1 parent 6a760a6 commit 63afc8c29a01f4b062b596608e0a5886e4f05a18
Showing with 7 additions and 5 deletions.
  1. +1 −0 include/opendht/http.h
  2. +6 −5 src/http.cpp
@@ -171,6 +171,7 @@ class OPENDHT_PUBLIC Resolver
Url url_;
asio::error_code ec_;
asio::ip::tcp::resolver resolver_;
std::shared_ptr<bool> destroyed_;
std::vector<asio::ip::tcp::endpoint> endpoints_;

bool completed_ {false};
@@ -301,14 +301,14 @@ Connection::timeout(const std::chrono::seconds timeout, HandlerCb cb)
// Resolver

Resolver::Resolver(asio::io_context& ctx, const std::string& url, std::shared_ptr<dht::Logger> logger)
: url_(url), resolver_(ctx), logger_(logger)
: url_(url), resolver_(ctx), destroyed_(std::make_shared<bool>(false)), logger_(logger)
{
resolve(url_.host, url_.service);
}

Resolver::Resolver(asio::io_context& ctx, const std::string& host, const std::string& service,
const bool ssl, std::shared_ptr<dht::Logger> logger)
: resolver_(ctx), logger_(logger)
: resolver_(ctx), destroyed_(std::make_shared<bool>(false)), logger_(logger)
{
url_.host = host;
url_.service = service;
@@ -318,7 +318,7 @@ Resolver::Resolver(asio::io_context& ctx, const std::string& host, const std::st

Resolver::Resolver(asio::io_context& ctx, std::vector<asio::ip::tcp::endpoint> endpoints, const bool ssl,
std::shared_ptr<dht::Logger> logger)
: resolver_(ctx), logger_(logger)
: resolver_(ctx), destroyed_(std::make_shared<bool>(false)), logger_(logger)
{
url_.protocol = (ssl ? "https" : "http");
endpoints_ = std::move(endpoints);
@@ -338,6 +338,7 @@ Resolver::~Resolver()
cb(asio::error::operation_aborted, {});
cbs.pop();
}
*destroyed_ = true;
}

void
@@ -355,10 +356,10 @@ Resolver::resolve(const std::string& host, const std::string& service)
{
asio::ip::tcp::resolver::query query_(host, service);

resolver_.async_resolve(query_, [this, host, service]
resolver_.async_resolve(query_, [this, host, service, destroyed = destroyed_]
(const asio::error_code& ec, asio::ip::tcp::resolver::results_type endpoints)
{
if (ec == asio::error::operation_aborted)
if (ec == asio::error::operation_aborted or *destroyed)
return;
if (logger_){
if (ec)

0 comments on commit 63afc8c

Please sign in to comment.
You can’t perform that action at this time.