diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index d4036587477..6bfcffa3de1 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -1200,11 +1200,11 @@ void Host::forEachPeer( return; } -std::unique_ptr Host::createTimer(std::chrono::seconds const& _expiryTime, +std::unique_ptr Host::createTimer(std::chrono::seconds const& _expiryDelay, std::function&& _f) { std::unique_ptr timer{new ba::steady_timer{m_ioContext}}; - timer->expires_after(_expiryTime); + timer->expires_after(_expiryDelay); timer->async_wait(_f); return timer; } diff --git a/libp2p/Host.h b/libp2p/Host.h index 42eb6ae929c..9327cc1e318 100644 --- a/libp2p/Host.h +++ b/libp2p/Host.h @@ -266,9 +266,9 @@ class Host: public Worker std::shared_ptr capabilityHost() const { return m_capabilityHost; } - /// Execute work on the network thread after @a _expiryTime delay. + /// Execute work on the network thread after an @_expiryDelay delay. /// Returned timer should be kept alive until delay is over. - std::unique_ptr createTimer(std::chrono::seconds const& _expiryTime, + std::unique_ptr createTimer(std::chrono::seconds const& _expiryDelay, std::function&& _f); protected: diff --git a/libp2p/Session.cpp b/libp2p/Session.cpp index d17ac199991..ebbfff18ab0 100644 --- a/libp2p/Session.cpp +++ b/libp2p/Session.cpp @@ -301,22 +301,11 @@ void Session::disconnect(DisconnectReason _reason) sealAndSend(s); auto self(shared_from_this()); - m_disconnectTimer = m_server->createTimer( - std::chrono::seconds(2), [self, this, _reason](boost::system::error_code) { - bi::tcp::socket& socket = m_socket->ref(); - if (socket.is_open()) - try - { - boost::system::error_code ec; - LOG(m_netLoggerDetail) - << "Closing (" << reasonOf(_reason) << ") connection with"; - socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec); - socket.close(); - } - catch (...) - { - } - }); + // The empty handler will keep the Session alive for the supplied amount of time, after + // which Host will garbage-collect the Session which will invoke the Session dtor and close the + // socket + m_disconnectTimer = + m_server->createTimer(std::chrono::seconds(2), [self](boost::system::error_code) {}); drop(_reason); }