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

Commit bb4721f

Browse files
authored
Merge pull request #5707 from twinstar26/master
Aleth waits for 2 seconds after sending disconnect packet
2 parents 78355a2 + d7bd87c commit bb4721f

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
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.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,3 +1200,11 @@ void Host::forEachPeer(
12001200
return;
12011201
}
12021202

1203+
std::unique_ptr<ba::steady_timer> Host::createTimer(std::chrono::seconds const& _expiryDelay,
1204+
std::function<void(const boost::system::error_code& error)>&& _f)
1205+
{
1206+
std::unique_ptr<ba::steady_timer> timer{new ba::steady_timer{m_ioContext}};
1207+
timer->expires_after(_expiryDelay);
1208+
timer->async_wait(_f);
1209+
return timer;
1210+
}

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+
/// Execute work on the network thread after an @a _expiryDelay delay.
270+
/// Returned timer should be kept alive until delay is over.
271+
std::unique_ptr<ba::steady_timer> createTimer(std::chrono::seconds const& _expiryDelay,
272+
std::function<void(const boost::system::error_code& error)>&& _f);
273+
269274
protected:
270275
/*
271276
* Used by the host to run a capability's background work loop

libp2p/Session.cpp

Lines changed: 17 additions & 16 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)
@@ -298,14 +291,22 @@ void Session::drop(DisconnectReason _reason)
298291

299292
void Session::disconnect(DisconnectReason _reason)
300293
{
294+
if (m_dropped)
295+
return;
296+
301297
clog(VerbosityTrace, "p2pcap") << "Disconnecting (our reason: " << reasonOf(_reason) << ") from " << m_logSuffix;
302298

303-
if (m_socket->ref().is_open())
304-
{
305-
RLPStream s;
306-
prep(s, DisconnectPacket, 1) << (int)_reason;
307-
sealAndSend(s);
308-
}
299+
RLPStream s;
300+
prep(s, DisconnectPacket, 1) << (int)_reason;
301+
sealAndSend(s);
302+
303+
auto self(shared_from_this());
304+
// The empty handler will keep the Session alive for the supplied amount of time, after
305+
// which Host will garbage-collect the Session which will invoke the Session dtor and close the
306+
// socket
307+
m_disconnectTimer =
308+
m_server->createTimer(std::chrono::seconds(2), [self](boost::system::error_code) {});
309+
309310
drop(_reason);
310311
}
311312

libp2p/Session.h

Lines changed: 3 additions & 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

@@ -177,6 +177,8 @@ class Session: public SessionFace, public std::enable_shared_from_this<SessionFa
177177

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

180+
std::unique_ptr<ba::steady_timer> m_disconnectTimer;
181+
180182
std::string m_logSuffix;
181183

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

0 commit comments

Comments
 (0)