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

Aleth waits for 2 seconds after sending disconnect packet #5707

Merged
merged 4 commits into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 8 additions & 0 deletions libp2p/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1200,3 +1200,11 @@ void Host::forEachPeer(
return;
}

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(_expiryDelay);
timer->async_wait(_f);
return timer;
}
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; }

/// Execute work on the network thread after an @a _expiryDelay delay.
/// Returned timer should be kept alive until delay is over.
std::unique_ptr<ba::steady_timer> createTimer(std::chrono::seconds const& _expiryDelay,
std::function<void(const boost::system::error_code& error)>&& _f);

protected:
/*
* Used by the host to run a capability's background work loop
Expand Down
33 changes: 17 additions & 16 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 @@ -298,14 +291,22 @@ void Session::drop(DisconnectReason _reason)

void Session::disconnect(DisconnectReason _reason)
{
if (m_dropped)
return;

clog(VerbosityTrace, "p2pcap") << "Disconnecting (our reason: " << reasonOf(_reason) << ") from " << m_logSuffix;

if (m_socket->ref().is_open())
{
RLPStream s;
prep(s, DisconnectPacket, 1) << (int)_reason;
sealAndSend(s);
}
RLPStream s;
prep(s, DisconnectPacket, 1) << (int)_reason;
sealAndSend(s);

auto self(shared_from_this());
// 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
4 changes: 3 additions & 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 Expand Up @@ -177,6 +177,8 @@ class Session: public SessionFace, public std::enable_shared_from_this<SessionFa

std::set<std::string> m_disabledCapabilities;

std::unique_ptr<ba::steady_timer> m_disconnectTimer;

std::string m_logSuffix;

Logger m_netLogger{createLogger(VerbosityDebug, "net")};
Expand Down