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
… restarted
  • Loading branch information
yufengwangca committed Feb 15, 2022
1 parent 6ad78e2 commit 0a91871
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
43 changes: 27 additions & 16 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 @@ -210,6 +208,9 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack()
{
CHIP_ERROR err;
struct sigaction action;
#if CHIP_WITH_GIO
GError * error = nullptr;
#endif

memset(&action, 0, sizeof(action));
action.sa_handler = SignalHandler;
Expand All @@ -220,20 +221,6 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack()
sigaction(SIGUSR2, &action, NULL);
sigaction(SIGTSTP, &action, NULL);

#if CHIP_WITH_GIO
GError * error = nullptr;

this->mpGDBusConnection = UniqueGDBusConnection(g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &error));

std::thread gdbusThread(GDBus_Thread);
gdbusThread.detach();
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
std::thread wifiIPThread(WiFIIPChangeListener);
wifiIPThread.detach();
#endif

// Initialize the configuration system.
err = Internal::PosixConfig::Init();
SuccessOrExit(err);
Expand All @@ -247,6 +234,16 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack()

mStartTime = System::SystemClock().GetMonotonicTimestamp();

#if CHIP_WITH_GIO
this->mpGDBusConnection = UniqueGDBusConnection(g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &error));

mGdbusThread = std::thread(GDBus_Thread);
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
mWifiIPThread = std::thread(WiFIIPChangeListener);
#endif

exit:
return err;
}
Expand All @@ -273,6 +270,20 @@ CHIP_ERROR PlatformManagerImpl::_Shutdown()
ChipLogError(DeviceLayer, "Failed to get current uptime since the Node’s last reboot");
}

#if CHIP_WITH_GIO
if (mGdbusThread.joinable())
{
mGdbusThread.join();
}
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
if (mWifiIPThread.joinable())
{
mWifiIPThread.join();
}
#endif

return Internal::GenericPlatformManagerImpl_POSIX<PlatformManagerImpl>::_Shutdown();
}

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 mGdbusThread;
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
std::thread mWifiIPThread;
#endif
};

Expand Down

0 comments on commit 0a91871

Please sign in to comment.