From e79fa3867f061c1103b3d8bf040663a2c6aecbbe Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Sun, 25 Sep 2022 19:07:55 +0200 Subject: [PATCH] fix(connection): clear the send-queue on socket-error --- src/Connection.cpp | 8 +++++++- src/Packet.cpp | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Connection.cpp b/src/Connection.cpp index 4abc4d2..875fb41 100644 --- a/src/Connection.cpp +++ b/src/Connection.cpp @@ -107,6 +107,12 @@ void TrueMQTT::Client::Impl::Connection::runRead() case State::SOCKET_ERROR: m_state = State::BACKOFF; m_impl.connectionStateChange(false); + + // Clear send-queue, as we can't send anything anymore. + { + std::scoped_lock lock(m_send_queue_mutex); + m_send_queue.clear(); + } break; case State::STOP: @@ -166,7 +172,7 @@ void TrueMQTT::Client::Impl::Connection::runWrite() default: // Sleep for a bit to avoid hogging the CPU. - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); break; } } diff --git a/src/Packet.cpp b/src/Packet.cpp index ed85892..683bb92 100644 --- a/src/Packet.cpp +++ b/src/Packet.cpp @@ -362,7 +362,7 @@ bool TrueMQTT::Client::Impl::Connection::sendConnect() packet.write_string(m_impl.m_last_will_message); } - return send(std::move(packet)); + return send(std::move(packet), true); } bool TrueMQTT::Client::Impl::Connection::sendPingRequest() -- libgit2 0.21.4