[wpiutil] Synchronization: fix shutdown use-after-free (#8213)

Also in ntcore, join the notifier thread on shutdown.
This prevents tsan from reporting it as a leaked thread.
This commit is contained in:
Peter Johnson
2025-09-08 21:15:00 -07:00
committed by GitHub
parent 5cd97c6353
commit f8ed2a4d92
4 changed files with 123 additions and 32 deletions

View File

@@ -13,6 +13,7 @@ using namespace nt;
std::atomic<int> InstanceImpl::s_default{-1};
std::atomic<InstanceImpl*> InstanceImpl::s_instances[kNumInstances];
wpi::mutex InstanceImpl::s_mutex;
InstanceImpl::Cleanup InstanceImpl::s_cleanup;
using namespace std::placeholders;

View File

@@ -75,6 +75,19 @@ class InstanceImpl {
static std::atomic<InstanceImpl*> s_instances[kNumInstances];
static wpi::mutex s_mutex;
struct Cleanup {
~Cleanup() {
for (auto&& inst : s_instances) {
// don't actually destroy (due to undefined global destruction order),
// but stop all listener threads
if (auto i = inst.load()) {
i->listenerStorage.Reset();
}
}
}
};
static Cleanup s_cleanup;
wpi::mutex m_mutex;
std::shared_ptr<NetworkServer> m_networkServer;
std::shared_ptr<INetworkClient> m_networkClient;

View File

@@ -355,9 +355,7 @@ void ListenerStorage::Reset() {
m_valueListeners.clear();
m_logListeners.clear();
m_timeSyncListeners.clear();
if (m_thread) {
m_thread.Stop();
}
m_thread.Join();
}
std::vector<std::pair<NT_Listener, unsigned int>>