diff --git a/src/messaging/ExchangeContext.cpp b/src/messaging/ExchangeContext.cpp index daee580971b404..bcdd4ea643af72 100644 --- a/src/messaging/ExchangeContext.cpp +++ b/src/messaging/ExchangeContext.cpp @@ -156,6 +156,7 @@ CHIP_ERROR ExchangeContext::SendMessage(Protocols::Id protocolId, uint8_t msgTyp bool reliableTransmissionRequested = GetSessionHandle()->RequireMRP() && !sendFlags.Has(SendMessageFlags::kNoAutoRequestAck) && !IsGroupExchangeContext(); + bool startedResponseTimer = false; // If a response message is expected... if (sendFlags.Has(SendMessageFlags::kExpectResponse) && !IsGroupExchangeContext()) { @@ -177,6 +178,7 @@ CHIP_ERROR ExchangeContext::SendMessage(Protocols::Id protocolId, uint8_t msgTyp SetResponseExpected(false); return err; } + startedResponseTimer = true; } } @@ -217,8 +219,8 @@ CHIP_ERROR ExchangeContext::SendMessage(Protocols::Id protocolId, uint8_t msgTyp #if CONFIG_BUILD_FOR_HOST_UNIT_TEST } #endif - - if (err != CHIP_NO_ERROR && IsResponseExpected()) + // We should only cancel the response timer if the ExchangeContext fails to send the message that starts the response timer. + if (err != CHIP_NO_ERROR && startedResponseTimer) { CancelResponseTimer(); SetResponseExpected(false); @@ -267,8 +269,11 @@ void ExchangeContext::DoClose(bool clearRetransTable) mExchangeMgr->GetReliableMessageMgr()->ClearRetransTable(this); } - // Cancel the response timer. - CancelResponseTimer(); + if (IsResponseExpected()) + { + // Cancel the response timer. + CancelResponseTimer(); + } } /** @@ -590,12 +595,15 @@ CHIP_ERROR ExchangeContext::HandleMessage(uint32_t messageCounter, const Payload return CHIP_ERROR_INCORRECT_STATE; } - // Since we got the response, cancel the response timer. - CancelResponseTimer(); + if (IsResponseExpected()) + { + // Since we got the response, cancel the response timer. + CancelResponseTimer(); - // If the context was expecting a response to a previously sent message, this message - // is implicitly that response. - SetResponseExpected(false); + // If the context was expecting a response to a previously sent message, this message + // is implicitly that response. + SetResponseExpected(false); + } // Don't send messages on to our delegate if our dispatch does not allow // those messages.