JsonRpcConnection#Disconnect(): spawn coroutine only if necessary

by checking the now atomic #m_ShuttingDown outside of it.
This commit is contained in:
Alexander A. Klimov 2024-02-07 13:46:13 +01:00 committed by Yonas Habteab
parent a51910a19b
commit 33f8ea6dcc
2 changed files with 7 additions and 8 deletions

View File

@ -188,12 +188,10 @@ void JsonRpcConnection::Disconnect()
{ {
namespace asio = boost::asio; namespace asio = boost::asio;
JsonRpcConnection::Ptr keepAlive (this); if (!m_ShuttingDown.exchange(true)) {
JsonRpcConnection::Ptr keepAlive (this);
IoEngine::SpawnCoroutine(m_IoStrand, [this, keepAlive](asio::yield_context yc) {
if (!m_ShuttingDown) {
m_ShuttingDown = true;
IoEngine::SpawnCoroutine(m_IoStrand, [this, keepAlive](asio::yield_context yc) {
Log(LogWarning, "JsonRpcConnection") Log(LogWarning, "JsonRpcConnection")
<< "API client disconnected for identity '" << m_Identity << "'"; << "API client disconnected for identity '" << m_Identity << "'";
@ -241,8 +239,8 @@ void JsonRpcConnection::Disconnect()
shutdownTimeout->Cancel(); shutdownTimeout->Cancel();
m_Stream->lowest_layer().shutdown(m_Stream->lowest_layer().shutdown_both, ec); m_Stream->lowest_layer().shutdown(m_Stream->lowest_layer().shutdown_both, ec);
} });
}); }
} }
void JsonRpcConnection::MessageHandler(const String& jsonString) void JsonRpcConnection::MessageHandler(const String& jsonString)

View File

@ -5,6 +5,7 @@
#include "remote/i2-remote.hpp" #include "remote/i2-remote.hpp"
#include "remote/endpoint.hpp" #include "remote/endpoint.hpp"
#include "base/atomic.hpp"
#include "base/io-engine.hpp" #include "base/io-engine.hpp"
#include "base/tlsstream.hpp" #include "base/tlsstream.hpp"
#include "base/timer.hpp" #include "base/timer.hpp"
@ -77,7 +78,7 @@ private:
std::vector<String> m_OutgoingMessagesQueue; std::vector<String> m_OutgoingMessagesQueue;
AsioConditionVariable m_OutgoingMessagesQueued; AsioConditionVariable m_OutgoingMessagesQueued;
AsioConditionVariable m_WriterDone; AsioConditionVariable m_WriterDone;
bool m_ShuttingDown; Atomic<bool> m_ShuttingDown;
boost::asio::deadline_timer m_CheckLivenessTimer, m_HeartbeatTimer; boost::asio::deadline_timer m_CheckLivenessTimer, m_HeartbeatTimer;
JsonRpcConnection(const String& identity, bool authenticated, const Shared<AsioTlsStream>::Ptr& stream, ConnectionRole role, boost::asio::io_context& io); JsonRpcConnection(const String& identity, bool authenticated, const Shared<AsioTlsStream>::Ptr& stream, ConnectionRole role, boost::asio::io_context& io);