Skip to content

Commit

Permalink
Fix detached thread resource leak if Matter stack is stopped and then… (
Browse files Browse the repository at this point in the history
#15167)

* 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
  • Loading branch information
yufengwangca authored and pull[bot] committed Nov 8, 2023
1 parent 59d82bd commit d28538c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/platform/Linux/PlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
#include <platform/PlatformManager.h>
#include <platform/internal/GenericPlatformManagerImpl_POSIX.cpp>

#include <thread>

#include <arpa/inet.h>
#include <dirent.h>
#include <errno.h>
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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<PlatformManagerImpl>::_Shutdown();
err = Internal::GenericPlatformManagerImpl_POSIX<PlatformManagerImpl>::_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(
Expand Down
8 changes: 8 additions & 0 deletions src/platform/Linux/PlatformManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <gio/gio.h>
#endif

#include <thread>

namespace chip {
namespace DeviceLayer {

Expand Down Expand Up @@ -97,6 +99,12 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener
};
using UniqueGDBusConnection = std::unique_ptr<GDBusConnection, GDBusConnectionDeleter>;
UniqueGDBusConnection mpGDBusConnection;

std::thread::native_handle_type mGdbusThreadHandle;
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
std::thread::native_handle_type mWifiIPThreadHandle;
#endif
};

Expand Down

0 comments on commit d28538c

Please sign in to comment.