Skip to content

Commit

Permalink
Fix shutdown ordering in DeviceController. (#7430)
Browse files Browse the repository at this point in the history
Before this fix we would tear down some things (importantly the secure
session manager) before starting shutdown of the network layer.  This
would lead to a window of time during which we can still receive
messages while in a partially torn down state, which would leave to
crashes.

Moving network layer shutdown, and in particular platform manager
shutdown, to be first in the shutdown sequence ensures this can't
happen by shutting down the message processing thread before we tear
down any other state.

Fixes #7297
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Jun 11, 2021
1 parent b4d2afa commit 0c68184
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,20 @@ CHIP_ERROR DeviceController::Shutdown()

ChipLogDetail(Controller, "Shutting down the controller");

#if CONFIG_DEVICE_LAYER
// Start by shutting down the PlatformManager. This will ensure, with
// reasonable synchronization, that we stop processing of incoming messages
// before doing any other shutdown work. Otherwise we can end up trying to
// process incoming messages in a partially shut down state, which is not
// great at all.
ReturnErrorOnFailure(DeviceLayer::PlatformMgr().Shutdown());
#else
mInetLayer->Shutdown();
mSystemLayer->Shutdown();
chip::Platform::Delete(mInetLayer);
chip::Platform::Delete(mSystemLayer);
#endif // CONFIG_DEVICE_LAYER

mState = State::NotInitialized;

// TODO(#6668): Some exchange has leak, shutting down ExchangeManager will cause a assert fail.
Expand All @@ -304,15 +318,6 @@ CHIP_ERROR DeviceController::Shutdown()
mSessionMgr->Shutdown();
}

#if CONFIG_DEVICE_LAYER
ReturnErrorOnFailure(DeviceLayer::PlatformMgr().Shutdown());
#else
mInetLayer->Shutdown();
mSystemLayer->Shutdown();
chip::Platform::Delete(mInetLayer);
chip::Platform::Delete(mSystemLayer);
#endif // CONFIG_DEVICE_LAYER

mSystemLayer = nullptr;
mInetLayer = nullptr;
mStorageDelegate = nullptr;
Expand Down

0 comments on commit 0c68184

Please sign in to comment.