From 44467243868e7ba615942e6b44742e3cec2ab45d Mon Sep 17 00:00:00 2001 From: Yufeng Wang Date: Mon, 21 Feb 2022 08:52:41 -0800 Subject: [PATCH] =?UTF-8?q?Fix=20detached=20thread=20resource=20leak=20if?= =?UTF-8?q?=20Matter=20stack=20is=20stopped=20and=20then=E2=80=A6=20(#1516?= =?UTF-8?q?7)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix detached thread resource leak if Matter stack is stopped and then restarted * Use platform API to cancel thread during shutdown * Fix CI failure by adjusting the shutdown order --- src/platform/Linux/PlatformManagerImpl.cpp | 23 +++++++++++++++++++--- src/platform/Linux/PlatformManagerImpl.h | 8 ++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/platform/Linux/PlatformManagerImpl.cpp b/src/platform/Linux/PlatformManagerImpl.cpp index 473653d4562c11..ed1bf643465ed9 100644 --- a/src/platform/Linux/PlatformManagerImpl.cpp +++ b/src/platform/Linux/PlatformManagerImpl.cpp @@ -33,8 +33,6 @@ #include #include -#include - #include #include #include @@ -190,11 +188,13 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack() this->mpGDBusConnection = UniqueGDBusConnection(g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &error)); std::thread gdbusThread(GDBus_Thread); + mGdbusThreadHandle = gdbusThread.native_handle(); gdbusThread.detach(); #endif #if CHIP_DEVICE_CONFIG_ENABLE_WIFI std::thread wifiIPThread(WiFIIPChangeListener); + mWifiIPThreadHandle = wifiIPThread.native_handle(); wifiIPThread.detach(); #endif @@ -217,6 +217,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack() CHIP_ERROR PlatformManagerImpl::_Shutdown() { + CHIP_ERROR err; uint64_t upTime = 0; if (GetDiagnosticDataProvider().GetUpTime(upTime) == CHIP_NO_ERROR) @@ -237,7 +238,23 @@ CHIP_ERROR PlatformManagerImpl::_Shutdown() ChipLogError(DeviceLayer, "Failed to get current uptime since the Node’s last reboot"); } - return Internal::GenericPlatformManagerImpl_POSIX::_Shutdown(); + err = Internal::GenericPlatformManagerImpl_POSIX::_Shutdown(); + +#if CHIP_WITH_GIO + if (pthread_cancel(mGdbusThreadHandle) != 0) + { + ChipLogError(DeviceLayer, "Failed to cancel gDBus thread"); + } +#endif + +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI + if (pthread_cancel(mWifiIPThreadHandle) != 0) + { + ChipLogError(DeviceLayer, "Failed to cancel WiFI IP Change Listener thread"); + } +#endif + + return err; } CHIP_ERROR PlatformManagerImpl::_GetFixedLabelList( diff --git a/src/platform/Linux/PlatformManagerImpl.h b/src/platform/Linux/PlatformManagerImpl.h index b6320ff734cb5b..15b001ac46a518 100644 --- a/src/platform/Linux/PlatformManagerImpl.h +++ b/src/platform/Linux/PlatformManagerImpl.h @@ -30,6 +30,8 @@ #include #endif +#include + namespace chip { namespace DeviceLayer { @@ -97,6 +99,12 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener }; using UniqueGDBusConnection = std::unique_ptr; UniqueGDBusConnection mpGDBusConnection; + + std::thread::native_handle_type mGdbusThreadHandle; +#endif + +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI + std::thread::native_handle_type mWifiIPThreadHandle; #endif };