Skip to content

Commit 4a21a8d

Browse files
authored
[AllClusters] If opened multiple times and something fails in Init, closing the app with Ctrl^C makes it crashes (project-chip#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
1 parent 0a2e58d commit 4a21a8d

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

src/app/InteractionModelEngine.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ CHIP_ERROR InteractionModelEngine::Init(Messaging::ExchangeManager * apExchangeM
8989
VerifyOrReturnError(apExchangeMgr != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
9090
VerifyOrReturnError(reportScheduler != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
9191

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

115+
mState = State::kInitialized;
114116
return CHIP_NO_ERROR;
115117
}
116118

117119
void InteractionModelEngine::Shutdown()
118120
{
121+
VerifyOrReturn(State::kUninitialized != mState);
122+
119123
mpExchangeMgr->GetSessionManager()->SystemLayer()->CancelTimer(ResumeSubscriptionsTimerCallback, this);
120124

121125
// TODO: individual object clears the entire command handler interface registry.
@@ -187,6 +191,8 @@ void InteractionModelEngine::Shutdown()
187191
//
188192
// mpFabricTable = nullptr;
189193
// mpExchangeMgr = nullptr;
194+
195+
mState = State::kUninitialized;
190196
}
191197

192198
uint32_t InteractionModelEngine::GetNumActiveReadHandlers() const

src/app/InteractionModelEngine.h

+8
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,14 @@ class InteractionModelEngine : public Messaging::UnsolicitedMessageHandler,
713713
DataModel::Provider * mDataModelProvider = nullptr;
714714
Messaging::ExchangeContext * mCurrentExchange = nullptr;
715715

716+
enum class State : uint8_t
717+
{
718+
kUninitialized, // The object has not been initialized.
719+
kInitializing, // Initial setup is in progress (e.g. setting up mpExchangeMgr).
720+
kInitialized // The object has been fully initialized and is ready for use.
721+
};
722+
State mState = State::kUninitialized;
723+
716724
// Changes the current exchange context of a InteractionModelEngine to a given context
717725
class CurrentExchangeValueScope
718726
{

src/app/server/CommissioningWindowManager.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ void CommissioningWindowManager::OnPlatformEvent(const DeviceLayer::ChipDeviceEv
111111

112112
void CommissioningWindowManager::Shutdown()
113113
{
114+
VerifyOrReturn(nullptr != mServer);
115+
114116
StopAdvertisement(/* aShuttingDown = */ true);
115117

116118
ResetState();

0 commit comments

Comments
 (0)