mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-27 02:01:42 +00:00
[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:
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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>>
|
||||
|
||||
Reference in New Issue
Block a user