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

Commit de74c8c

Browse files
committed
Don't close socket in disconnect timer handler
Closing the socket in the handler is unnecessary since it will be closed when the dtor executes.
1 parent bf7b9ae commit de74c8c

File tree

3 files changed

+9
-20
lines changed

3 files changed

+9
-20
lines changed

libp2p/Host.cpp

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

1203-
std::unique_ptr<ba::steady_timer> Host::createTimer(std::chrono::seconds const& _expiryTime,
1203+
std::unique_ptr<ba::steady_timer> Host::createTimer(std::chrono::seconds const& _expiryDelay,
12041204
std::function<void(const boost::system::error_code& error)>&& _f)
12051205
{
12061206
std::unique_ptr<ba::steady_timer> timer{new ba::steady_timer{m_ioContext}};
1207-
timer->expires_after(_expiryTime);
1207+
timer->expires_after(_expiryDelay);
12081208
timer->async_wait(_f);
12091209
return timer;
12101210
}

libp2p/Host.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,9 @@ class Host: public Worker
266266

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

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

274274
protected:

libp2p/Session.cpp

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -301,22 +301,11 @@ void Session::disconnect(DisconnectReason _reason)
301301
sealAndSend(s);
302302

303303
auto self(shared_from_this());
304-
m_disconnectTimer = m_server->createTimer(
305-
std::chrono::seconds(2), [self, this, _reason](boost::system::error_code) {
306-
bi::tcp::socket& socket = m_socket->ref();
307-
if (socket.is_open())
308-
try
309-
{
310-
boost::system::error_code ec;
311-
LOG(m_netLoggerDetail)
312-
<< "Closing (" << reasonOf(_reason) << ") connection with";
313-
socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
314-
socket.close();
315-
}
316-
catch (...)
317-
{
318-
}
319-
});
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) {});
320309

321310
drop(_reason);
322311
}

0 commit comments

Comments
 (0)