Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

Commit

Permalink
Don't close socket in disconnect timer handler
Browse files Browse the repository at this point in the history
Closing the socket in the handler is unnecessary since it will be closed
when the dtor executes.
  • Loading branch information
halfalicious committed Aug 13, 2019
1 parent bf7b9ae commit de74c8c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 20 deletions.
4 changes: 2 additions & 2 deletions libp2p/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1200,11 +1200,11 @@ void Host::forEachPeer(
return;
}

std::unique_ptr<ba::steady_timer> Host::createTimer(std::chrono::seconds const& _expiryTime,
std::unique_ptr<ba::steady_timer> Host::createTimer(std::chrono::seconds const& _expiryDelay,
std::function<void(const boost::system::error_code& error)>&& _f)
{
std::unique_ptr<ba::steady_timer> timer{new ba::steady_timer{m_ioContext}};
timer->expires_after(_expiryTime);
timer->expires_after(_expiryDelay);
timer->async_wait(_f);
return timer;
}
4 changes: 2 additions & 2 deletions libp2p/Host.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,9 @@ class Host: public Worker

std::shared_ptr<CapabilityHostFace> 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<ba::steady_timer> createTimer(std::chrono::seconds const& _expiryTime,
std::unique_ptr<ba::steady_timer> createTimer(std::chrono::seconds const& _expiryDelay,
std::function<void(const boost::system::error_code& error)>&& _f);

protected:
Expand Down
21 changes: 5 additions & 16 deletions libp2p/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit de74c8c

Please sign in to comment.