Skip to content

Commit

Permalink
Force close ExchangeContexts when shutting down the ExchangeManager
Browse files Browse the repository at this point in the history
  • Loading branch information
kghost committed Mar 31, 2021
1 parent ad8104f commit 3d30424
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
33 changes: 33 additions & 0 deletions src/messaging/ExchangeContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,39 @@ void ExchangeContext::Free()
SYSTEM_STATS_DECREMENT(chip::System::Stats::kExchangeMgr_NumContexts);
}

void ExchangeContext::ForceFree()
{
VerifyOrDie(mExchangeMgr != nullptr);

ExchangeManager * em = mExchangeMgr;

// Clear protocol callbacks
if (mDelegate != nullptr)
{
mDelegate->OnExchangeClosing(this);
}
mDelegate = nullptr;

// Cancel the response timer.
CancelResponseTimer();

mExchangeMgr = nullptr;

em->DecrementContextsInUse();

if (mExchangeACL != nullptr)
{
chip::Platform::Delete(mExchangeACL);
mExchangeACL = nullptr;
}

#if defined(CHIP_EXCHANGE_CONTEXT_DETAIL_LOGGING)
ChipLogProgress(ExchangeManager, "ec-- id: %d [%04" PRIX16 "], inUse: %d, addr: 0x%x", (this - em->ContextPool + 1),
mExchangeId, em->GetContextsInUse(), this);
#endif
SYSTEM_STATS_DECREMENT(chip::System::Stats::kExchangeMgr_NumContexts);
}

bool ExchangeContext::MatchExchange(SecureSessionHandle session, const PacketHeader & packetHeader,
const PayloadHeader & payloadHeader)
{
Expand Down
1 change: 1 addition & 0 deletions src/messaging/ExchangeContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ class DLL_EXPORT ExchangeContext : public ReferenceCounted<ExchangeContext, Exch
ExchangeDelegate * delegate);
void Free();
void Reset();
void ForceFree();

void SetResponseTimeout(Timeout timeout);

Expand Down
3 changes: 2 additions & 1 deletion src/messaging/ExchangeMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ CHIP_ERROR ExchangeManager::Shutdown()
{
if (ec.GetReferenceCount() > 0)
{
ec.Abort();
ChipLogError(ExchangeManager, "Exchange (id: %d) leaked", ec.GetExchangeId());
ec.ForceFree();
}
}

Expand Down

0 comments on commit 3d30424

Please sign in to comment.