Skip to content

Commit 3590508

Browse files
bzbarsky-applepull[bot]
authored andcommitted
Fix shutdown ordering in DeviceController. (#7430)
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
1 parent 933c75b commit 3590508

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/controller/CHIPDeviceController.cpp

+14-9
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,20 @@ CHIP_ERROR DeviceController::Shutdown()
292292

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

295+
#if CONFIG_DEVICE_LAYER
296+
// Start by shutting down the PlatformManager. This will ensure, with
297+
// reasonable synchronization, that we stop processing of incoming messages
298+
// before doing any other shutdown work. Otherwise we can end up trying to
299+
// process incoming messages in a partially shut down state, which is not
300+
// great at all.
301+
ReturnErrorOnFailure(DeviceLayer::PlatformMgr().Shutdown());
302+
#else
303+
mInetLayer->Shutdown();
304+
mSystemLayer->Shutdown();
305+
chip::Platform::Delete(mInetLayer);
306+
chip::Platform::Delete(mSystemLayer);
307+
#endif // CONFIG_DEVICE_LAYER
308+
295309
mState = State::NotInitialized;
296310

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

307-
#if CONFIG_DEVICE_LAYER
308-
ReturnErrorOnFailure(DeviceLayer::PlatformMgr().Shutdown());
309-
#else
310-
mInetLayer->Shutdown();
311-
mSystemLayer->Shutdown();
312-
chip::Platform::Delete(mInetLayer);
313-
chip::Platform::Delete(mSystemLayer);
314-
#endif // CONFIG_DEVICE_LAYER
315-
316321
mSystemLayer = nullptr;
317322
mInetLayer = nullptr;
318323
mStorageDelegate = nullptr;

0 commit comments

Comments
 (0)