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

Commit ad75763

Browse files
twinstar26gumb0
authored andcommitted
Aleth waits for 2 seconds after sending disconnect to peer before closing socket
1 parent 78355a2 commit ad75763

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
@@ -17,6 +17,7 @@
1717
- Added: [#5691](https://github.com/ethereum/aleth/pull/5691) Istanbul support: EIP-2028 Transaction data gas cost reduction.
1818
- Added: [#5696](https://github.com/ethereum/aleth/pull/5696) Istanbul support: EIP-1344 ChainID opcode.
1919
- Added: [#5701](https://github.com/ethereum/aleth/issues/5701) Outputs ENR text representation in admin.nodeInfo RPC.
20+
- Added: [#5707](https://github.com/ethereum/aleth/pull/5707) Aleth waits for 2 seconds after sending disconnect to peer before closing socket.
2021
- 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.
2122
- Changed: [#5559](https://github.com/ethereum/aleth/pull/5559) Update peer validation error messages.
2223
- 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
@@ -266,6 +266,11 @@ class Host: public Worker
266266

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

269+
std::shared_ptr<ba::steady_timer> createTimer()
270+
{
271+
return std::make_shared<ba::steady_timer>(m_ioContext);
272+
}
273+
269274
protected:
270275
/*
271276
* 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)