Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix detached thread resource leak if Matter stack is stopped and then… #15167

Merged
merged 3 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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