diff --git a/src/messaging/ExchangeContext.cpp b/src/messaging/ExchangeContext.cpp index 08d916a15246e2..422a2f88e34ce4 100644 --- a/src/messaging/ExchangeContext.cpp +++ b/src/messaging/ExchangeContext.cpp @@ -325,7 +325,7 @@ ExchangeContext::~ExchangeContext() VerifyOrDie(mExchangeMgr != nullptr && GetReferenceCount() == 0); VerifyOrDie(!IsAckPending()); - if (IsAutoReleaseSession() && mSession) + if (ReleaseSessionOnDestruction() && mSession) mSession->AsSecureSession()->MarkForRemoval(); #if CONFIG_DEVICE_LAYER && CHIP_DEVICE_CONFIG_ENABLE_SED diff --git a/src/messaging/ExchangeMgr.cpp b/src/messaging/ExchangeMgr.cpp index 05b70adec8adf9..b01d5b4fe4cb5c 100644 --- a/src/messaging/ExchangeMgr.cpp +++ b/src/messaging/ExchangeMgr.cpp @@ -378,6 +378,8 @@ void ExchangeManager::CloseAllContextsForDelegate(const ExchangeDelegate * deleg void ExchangeManager::AbortExchangesForFabricExceptOne(FabricIndex fabricIndex, ExchangeContext * deferred) { + VerifyOrDie(deferred->HasSessionHandle() && deferred->GetSessionHandle()->IsSecureSession()); + mContextPool.ForEachActiveObject([&](auto * ec) { if (ec->HasSessionHandle() && ec->GetSessionHandle()->GetFabricIndex() == fabricIndex) { @@ -388,6 +390,8 @@ void ExchangeManager::AbortExchangesForFabricExceptOne(FabricIndex fabricIndex, } return Loop::Continue; }); + + mSessionManager->ReleaseSessionsForFabricExceptOne(fabricIndex, deferred->GetSessionHandle()); } } // namespace Messaging diff --git a/src/messaging/ExchangeMgr.h b/src/messaging/ExchangeMgr.h index 910dcb8ee81db3..112cddd82c7ecb 100644 --- a/src/messaging/ExchangeMgr.h +++ b/src/messaging/ExchangeMgr.h @@ -183,9 +183,8 @@ class DLL_EXPORT ExchangeManager : public SessionMessageDelegate */ void CloseAllContextsForDelegate(const ExchangeDelegate * delegate); - // This API is used by commands that need to shut down all existing exchanges on - // a fabric but need to make sure the response to the command still goes out on the exchange - // the command came in on. This API flags that one exchange to shut down its session + // This API is used by commands that need to shut down all existing exchanges on a fabric but need to make sure the response to + // the command still goes out on the exchange the command came in on. This API flags that one exchange to shut down its session // when it's done. void AbortExchangesForFabricExceptOne(FabricIndex fabricIndex, ExchangeContext * deferred); diff --git a/src/messaging/ReliableMessageContext.h b/src/messaging/ReliableMessageContext.h index 6e94da51f4a739..9b2f8d67ab0539 100644 --- a/src/messaging/ReliableMessageContext.h +++ b/src/messaging/ReliableMessageContext.h @@ -258,7 +258,7 @@ inline void ReliableMessageContext::SetAutoReleaseSession() mFlags.Set(Flags::kFlagAutoReleaseSession, true); } -inline bool ReliableMessageContext::IsAutoReleaseSession() +inline bool ReliableMessageContext::ReleaseSessionOnDestruction() { return mFlags.Has(Flags::kFlagAutoReleaseSession); } diff --git a/src/transport/SecureSession.cpp b/src/transport/SecureSession.cpp index e63d2d861e4ce1..9363d92b323f21 100644 --- a/src/transport/SecureSession.cpp +++ b/src/transport/SecureSession.cpp @@ -50,9 +50,9 @@ void SecureSession::MarkForRemoval() } } -void SecureSession::MarkForInactive() +void SecureSession::MarkInactive() { - ChipLogDetail(Inet, "SecureSession[%p]: MarkForInactive Type:%d LSID:%d", this, to_underlying(mSecureSessionType), + ChipLogDetail(Inet, "SecureSession[%p]: MarkInactive Type:%d LSID:%d", this, to_underlying(mSecureSessionType), mLocalSessionId); ReferenceCountedHandle ref(*this); switch (mState) diff --git a/src/transport/SecureSession.h b/src/transport/SecureSession.h index 6ce8202c65b2d3..829b179965a8b2 100644 --- a/src/transport/SecureSession.h +++ b/src/transport/SecureSession.h @@ -145,7 +145,7 @@ class SecureSession : public Session, public ReferenceCountedIsSecureSession()); + SecureSession * deferredSecureSession = deferred->AsSecureSession(); + mSecureSessions.ForEachSession([&](auto session) { if (session->GetPeer().GetFabricIndex() == fabricIndex) { - if (session == deferred->AsSecureSession()) - session->MarkForInactive(); + if (session == deferredSecureSession) + session->MarkInactive(); else session->MarkForRemoval(); } diff --git a/src/transport/SessionManager.h b/src/transport/SessionManager.h index c57f7089f04725..d88031f670f4ac 100644 --- a/src/transport/SessionManager.h +++ b/src/transport/SessionManager.h @@ -176,8 +176,9 @@ class DLL_EXPORT SessionManager : public TransportMgrDelegate void ExpireAllPairingsForFabric(FabricIndex fabric); void ExpireAllPASEPairings(); - // This API is used by UpdateNOC command, to invalidate all sessions except the given one, whose release is deferred until - // UpdateNOC command finishing its work. + // This API is used by commands that need to release all existing sessions on a fabric but need to make sure the response to the + // command still goes out on the exchange the command came in on. This API flags that the release of the session used by the + // exchange is deferred until the exchange is done. void ReleaseSessionsForFabricExceptOne(FabricIndex fabricIndex, const SessionHandle & deferred); /**