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

Commit

Permalink
Aleth waits for 2 seconds after sending disconnect to peer before clo…
Browse files Browse the repository at this point in the history
…sing socket
  • Loading branch information
twinstar26 authored and gumb0 committed Aug 8, 2019
1 parent 78355a2 commit ad75763
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- Added: [#5691](https://github.com/ethereum/aleth/pull/5691) Istanbul support: EIP-2028 Transaction data gas cost reduction.
- Added: [#5696](https://github.com/ethereum/aleth/pull/5696) Istanbul support: EIP-1344 ChainID opcode.
- Added: [#5701](https://github.com/ethereum/aleth/issues/5701) Outputs ENR text representation in admin.nodeInfo RPC.
- Added: [#5707](https://github.com/ethereum/aleth/pull/5707) Aleth waits for 2 seconds after sending disconnect to peer before closing socket.
- Changed: [#5532](https://github.com/ethereum/aleth/pull/5532) The leveldb is upgraded to 1.22. This is breaking change on Windows and the old databases are not compatible.
- Changed: [#5559](https://github.com/ethereum/aleth/pull/5559) Update peer validation error messages.
- Changed: [#5568](https://github.com/ethereum/aleth/pull/5568) Improve rlpx handshake log messages and create new rlpx log channel.
Expand Down
5 changes: 5 additions & 0 deletions libp2p/Host.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ class Host: public Worker

std::shared_ptr<CapabilityHostFace> capabilityHost() const { return m_capabilityHost; }

std::shared_ptr<ba::steady_timer> createTimer()
{
return std::make_shared<ba::steady_timer>(m_ioContext);
}

protected:
/*
* Used by the host to run a capability's background work loop
Expand Down
36 changes: 24 additions & 12 deletions libp2p/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ bool Session::checkPacket(bytesConstRef _msg)

void Session::send(bytes&& _msg)
{
if (m_dropped)
return;

bytesConstRef msg(&_msg);
LOG(m_netLoggerDetail) << capabilityPacketTypeToString(_msg[0]) << " to";
if (!checkPacket(msg))
Expand Down Expand Up @@ -276,16 +279,6 @@ void Session::drop(DisconnectReason _reason)
{
if (m_dropped)
return;
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 (...) {}

m_peer->m_lastDisconnect = _reason;
if (_reason == BadProtocol)
Expand All @@ -300,13 +293,32 @@ void Session::disconnect(DisconnectReason _reason)
{
clog(VerbosityTrace, "p2pcap") << "Disconnecting (our reason: " << reasonOf(_reason) << ") from " << m_logSuffix;

if (m_socket->ref().is_open())
if (!m_dropped)
{
RLPStream s;
prep(s, DisconnectPacket, 1) << (int)_reason;
sealAndSend(s);
auto disconnectTimer = m_server->createTimer();
auto self(shared_from_this());
disconnectTimer->expires_after(std::chrono::seconds(2));
disconnectTimer->async_wait([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 (...)
{
}
});
}
drop(_reason);
else
drop(_reason);
}

void Session::start()
Expand Down
2 changes: 1 addition & 1 deletion libp2p/Session.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Session: public SessionFace, public std::enable_shared_from_this<SessionFa

void ping() override;

bool isConnected() const override { return m_socket->ref().is_open(); }
bool isConnected() const override { return !m_dropped; }

NodeID id() const override;

Expand Down

0 comments on commit ad75763

Please sign in to comment.