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

Commit da6d587

Browse files
committed
Aleth waits for 2 seconds after sending disconnect to peer before closing socket
1 parent 5bf79e5 commit da6d587

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- Added: [#5634](https://github.com/ethereum/aleth/pull/5634) Bootnodes for Rinkeby and Goerli.
1616
- Added: [#5640](https://github.com/ethereum/aleth/pull/5640) Istanbul support: EIP-1702 Generalized Account Versioning Scheme.
1717
- Added: [#5690](https://github.com/ethereum/aleth/issues/5690) Istanbul support: EIP-2028 transaction data gas cost reduction.
18+
- Added: [#5650](https://github.com/ethereum/aleth/issues/5650) Aleth waits for 2 seconds after sending disconnect to peer before closing socket.
1819
- 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.
1920
- Changed: [#5559](https://github.com/ethereum/aleth/pull/5559) Update peer validation error messages.
2021
- Changed: [#5568](https://github.com/ethereum/aleth/pull/5568) Improve rlpx handshake log messages and create new rlpx log channel.

libp2p/Host.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ class Host: public Worker
258258

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

261+
std::shared_ptr<ba::steady_timer> createTimer()
262+
{
263+
return std::make_shared<ba::steady_timer>(m_ioContext);
264+
}
265+
261266
protected:
262267
/*
263268
* Used by the host to run a capability's background work loop

libp2p/Session.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ bool Session::checkPacket(bytesConstRef _msg)
206206

207207
void Session::send(bytes&& _msg)
208208
{
209+
if (m_dropped)
210+
return;
211+
209212
bytesConstRef msg(&_msg);
210213
LOG(m_netLoggerDetail) << capabilityPacketTypeToString(_msg[0]) << " to";
211214
if (!checkPacket(msg))
@@ -276,16 +279,6 @@ void Session::drop(DisconnectReason _reason)
276279
{
277280
if (m_dropped)
278281
return;
279-
bi::tcp::socket& socket = m_socket->ref();
280-
if (socket.is_open())
281-
try
282-
{
283-
boost::system::error_code ec;
284-
LOG(m_netLoggerDetail) << "Closing (" << reasonOf(_reason) << ") connection with";
285-
socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
286-
socket.close();
287-
}
288-
catch (...) {}
289282

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

303-
if (m_socket->ref().is_open())
296+
if (!m_dropped)
304297
{
305298
RLPStream s;
306299
prep(s, DisconnectPacket, 1) << (int)_reason;
307300
sealAndSend(s);
301+
auto disconnectTimer = m_server->createTimer();
302+
auto self(shared_from_this());
303+
disconnectTimer->expires_after(std::chrono::seconds(2));
304+
disconnectTimer->async_wait([self, this, _reason](boost::system::error_code) {
305+
bi::tcp::socket& socket = m_socket->ref();
306+
if (socket.is_open())
307+
try
308+
{
309+
boost::system::error_code ec;
310+
LOG(m_netLoggerDetail)
311+
<< "Closing (" << reasonOf(_reason) << ") connection with";
312+
socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
313+
socket.close();
314+
}
315+
catch (...)
316+
{
317+
}
318+
});
308319
}
309-
drop(_reason);
320+
else
321+
drop(_reason);
310322
}
311323

312324
void Session::start()

libp2p/Session.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class Session: public SessionFace, public std::enable_shared_from_this<SessionFa
8484

8585
void ping() override;
8686

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

8989
NodeID id() const override;
9090

0 commit comments

Comments
 (0)