Skip to content

Commit

Permalink
[AllClusters] If opened multiple times and something fails in Init, c…
Browse files Browse the repository at this point in the history
…losing the app with Ctrl^C makes it crashes (#35844)

* [InteractionModelEngine] Add some checks to Shutdown to prevent crashing if someone calls Shutdown before Init

* [CommissioningWindowManager] Ensure Shutdown does nothing if Init has not been called
  • Loading branch information
vivien-apple authored Oct 8, 2024
1 parent 0a2e58d commit 4a21a8d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/app/InteractionModelEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ CHIP_ERROR InteractionModelEngine::Init(Messaging::ExchangeManager * apExchangeM
VerifyOrReturnError(apExchangeMgr != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(reportScheduler != nullptr, CHIP_ERROR_INVALID_ARGUMENT);

mState = State::kInitializing;
mpExchangeMgr = apExchangeMgr;
mpFabricTable = apFabricTable;
mpCASESessionMgr = apCASESessionMgr;
Expand All @@ -111,11 +112,14 @@ CHIP_ERROR InteractionModelEngine::Init(Messaging::ExchangeManager * apExchangeM
ChipLogError(InteractionModel, "WARNING └────────────────────────────────────────────────────");
#endif

mState = State::kInitialized;
return CHIP_NO_ERROR;
}

void InteractionModelEngine::Shutdown()
{
VerifyOrReturn(State::kUninitialized != mState);

mpExchangeMgr->GetSessionManager()->SystemLayer()->CancelTimer(ResumeSubscriptionsTimerCallback, this);

// TODO: individual object clears the entire command handler interface registry.
Expand Down Expand Up @@ -187,6 +191,8 @@ void InteractionModelEngine::Shutdown()
//
// mpFabricTable = nullptr;
// mpExchangeMgr = nullptr;

mState = State::kUninitialized;
}

uint32_t InteractionModelEngine::GetNumActiveReadHandlers() const
Expand Down
8 changes: 8 additions & 0 deletions src/app/InteractionModelEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,14 @@ class InteractionModelEngine : public Messaging::UnsolicitedMessageHandler,
DataModel::Provider * mDataModelProvider = nullptr;
Messaging::ExchangeContext * mCurrentExchange = nullptr;

enum class State : uint8_t
{
kUninitialized, // The object has not been initialized.
kInitializing, // Initial setup is in progress (e.g. setting up mpExchangeMgr).
kInitialized // The object has been fully initialized and is ready for use.
};
State mState = State::kUninitialized;

// Changes the current exchange context of a InteractionModelEngine to a given context
class CurrentExchangeValueScope
{
Expand Down
2 changes: 2 additions & 0 deletions src/app/server/CommissioningWindowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ void CommissioningWindowManager::OnPlatformEvent(const DeviceLayer::ChipDeviceEv

void CommissioningWindowManager::Shutdown()
{
VerifyOrReturn(nullptr != mServer);

StopAdvertisement(/* aShuttingDown = */ true);

ResetState();
Expand Down

0 comments on commit 4a21a8d

Please sign in to comment.