From 1178668477eaa1a773ea838eb5ccc17a4e4a7c8f Mon Sep 17 00:00:00 2001
From: Kevin Schoedel <67607049+kpschoedel@users.noreply.github.com>
Date: Tue, 8 Jun 2021 10:29:43 -0400
Subject: [PATCH] Fix System vs Inet and BLE shutdown order (#7429)

#### Problem

Inet and BLE contain pointers to the System::Layer, but in some
implementations System::Layer is shut down first, leaving those
pointers invalid. (There appear to be no current uses of those
invalid pointers in the tree.)

#### Change overview

Shut down system layer after the others in:
- `DeviceController::Shutdown()`
- `GenericPlatformManagerImpl::_Shutdown()`
- `JNI_OnUnload()`

#### Testing

- Some existing unit tests use the correct order (System last),
  so it's known to work.
- Manually tested with platforms using `GenericPlatformManagerImpl::_Shutdown()`
  (Linux paired with ESP32).
---
 src/controller/CHIPDeviceController.cpp                     | 4 ++--
 src/controller/java/CHIPDeviceController-JNI.cpp            | 2 +-
 .../platform/internal/GenericPlatformManagerImpl.cpp        | 6 ++++--
 3 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/src/controller/CHIPDeviceController.cpp b/src/controller/CHIPDeviceController.cpp
index 15d59e696d1a3c..8e41081f76695e 100644
--- a/src/controller/CHIPDeviceController.cpp
+++ b/src/controller/CHIPDeviceController.cpp
@@ -307,10 +307,10 @@ CHIP_ERROR DeviceController::Shutdown()
 #if CONFIG_DEVICE_LAYER
     ReturnErrorOnFailure(DeviceLayer::PlatformMgr().Shutdown());
 #else
-    mSystemLayer->Shutdown();
     mInetLayer->Shutdown();
-    chip::Platform::Delete(mSystemLayer);
+    mSystemLayer->Shutdown();
     chip::Platform::Delete(mInetLayer);
+    chip::Platform::Delete(mSystemLayer);
 #endif // CONFIG_DEVICE_LAYER
 
     mSystemLayer     = nullptr;
diff --git a/src/controller/java/CHIPDeviceController-JNI.cpp b/src/controller/java/CHIPDeviceController-JNI.cpp
index 238ef7218fa4bd..e0308cdac3f154 100644
--- a/src/controller/java/CHIPDeviceController-JNI.cpp
+++ b/src/controller/java/CHIPDeviceController-JNI.cpp
@@ -229,8 +229,8 @@ void JNI_OnUnload(JavaVM * jvm, void * reserved)
     sBleLayer.Shutdown();
 #endif
 
-    sSystemLayer.Shutdown();
     sInetLayer.Shutdown();
+    sSystemLayer.Shutdown();
     sJVM = NULL;
 
     chip::Platform::MemoryShutdown();
diff --git a/src/include/platform/internal/GenericPlatformManagerImpl.cpp b/src/include/platform/internal/GenericPlatformManagerImpl.cpp
index 469112018946e5..82990ccd771ee7 100644
--- a/src/include/platform/internal/GenericPlatformManagerImpl.cpp
+++ b/src/include/platform/internal/GenericPlatformManagerImpl.cpp
@@ -124,8 +124,6 @@ template <class ImplClass>
 CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_Shutdown()
 {
     CHIP_ERROR err;
-    ChipLogError(DeviceLayer, "System Layer shutdown");
-    err = SystemLayer.Shutdown();
     ChipLogError(DeviceLayer, "Inet Layer shutdown");
     err = InetLayer.Shutdown();
 
@@ -133,6 +131,10 @@ CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_Shutdown()
     ChipLogError(DeviceLayer, "BLE layer shutdown");
     err = BLEMgr().GetBleLayer()->Shutdown();
 #endif
+
+    ChipLogError(DeviceLayer, "System Layer shutdown");
+    err = SystemLayer.Shutdown();
+
     return err;
 }