From 50c7f0013545b3882f71f64e864a648943483b1b Mon Sep 17 00:00:00 2001 From: Wang Qixiang <43193572+wqx6@users.noreply.github.com> Date: Sat, 6 May 2023 03:09:57 +0800 Subject: [PATCH] ExchangeCtx: Fix issue with response timer cancellation (#26215) * ExchangeCtx: Fix issue with response timer cancellation * Add a boolean to record whether the message starts the reponse timer --- src/messaging/ExchangeContext.cpp | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) 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.