@@ -417,8 +417,10 @@ void ClientConnection::handleTcpConnected(const ASIO_ERROR& err, const tcp::endp
417417 Lock lock (mutex_);
418418 if (!err) {
419419 if (isClosed ()) {
420- ASIO_ERROR err;
421- socket_->close (err);
420+ if (socket_) {
421+ ASIO_ERROR err;
422+ socket_->close (err);
423+ }
422424 connectPromise_.setFailed (ResultAlreadyClosed);
423425 LOG_INFO (cnxString_ << " Connection already closed" );
424426 return ;
@@ -674,9 +676,11 @@ void ClientConnection::readNextCommand() {
674676}
675677
676678void ClientConnection::handleRead (const ASIO_ERROR & err, size_t bytesTransferred, uint32_t minReadSize) {
679+ Lock lock{mutex_};
677680 if (isClosed ()) {
678681 return ;
679682 }
683+ lock.unlock ();
680684 // Update buffer write idx with new data
681685 incomingBuffer_.bytesWritten (bytesTransferred);
682686
@@ -1145,9 +1149,11 @@ void ClientConnection::sendMessage(const std::shared_ptr<SendArguments>& args) {
11451149}
11461150
11471151void ClientConnection::handleSend (const ASIO_ERROR & err, const SharedBuffer&) {
1152+ Lock lock{mutex_};
11481153 if (isClosed ()) {
11491154 return ;
11501155 }
1156+ lock.unlock ();
11511157 if (err) {
11521158 LOG_WARN (cnxString_ << " Could not send message on connection: " << err << " " << err.message ());
11531159 close (ResultDisconnected);
@@ -1269,9 +1275,11 @@ void ClientConnection::handleGetLastMessageIdTimeout(const ASIO_ERROR& ec,
12691275}
12701276
12711277void ClientConnection::handleKeepAliveTimeout () {
1278+ Lock lock{mutex_};
12721279 if (isClosed ()) {
12731280 return ;
12741281 }
1282+ lock.unlock ();
12751283
12761284 if (havePendingPingRequest_) {
12771285 LOG_WARN (cnxString_ << " Forcing connection to close after keep-alive timeout" );
@@ -1325,8 +1333,9 @@ void ClientConnection::close(Result result, bool detach) {
13251333 } else {
13261334 // There is an ongoing connect operation, the socket will be closed after the operation succeeds
13271335 // in `handleTcpConnected`
1328- LOG_WARN (cnxString_
1329- << " Socket is still connecting, it will be closed once the connection attempt finishes" );
1336+ LOG_DEBUG (
1337+ cnxString_
1338+ << " Socket is still connecting, it will be closed once the connection attempt finishes" );
13301339 }
13311340 }
13321341 state_ = Disconnected;
0 commit comments