From 887fa1ab6f6809ce4b30a958d8c561e3df06b03c Mon Sep 17 00:00:00 2001 From: Hayodea Hekol Date: Fri, 7 Nov 2025 20:45:16 -0400 Subject: [PATCH] Bug:UdpCmdDemux: Add SpinLock for races around stop() --- commonLibs/livoxProto1/udpCommandDemuxer.cpp | 18 +++++++++++++----- commonLibs/livoxProto1/udpCommandDemuxer.h | 2 ++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/commonLibs/livoxProto1/udpCommandDemuxer.cpp b/commonLibs/livoxProto1/udpCommandDemuxer.cpp index 9d720ad..b8e6330 100644 --- a/commonLibs/livoxProto1/udpCommandDemuxer.cpp +++ b/commonLibs/livoxProto1/udpCommandDemuxer.cpp @@ -44,8 +44,11 @@ void UdpCommandDemuxer::start() try { setupSockets(); - isActive.store(true); - shouldStop.store(false); + { + smo::SpinLock::Guard lock(isActiveAndShouldStopLock); + isActive.store(true); + shouldStop.store(false); + } // Start the async receive loop startAsyncReceive(); @@ -66,10 +69,13 @@ void UdpCommandDemuxer::start() void UdpCommandDemuxer::stop() { - if (!isActive.load()) - { return; } + { + smo::SpinLock::Guard lock(isActiveAndShouldStopLock); + if (!isActive.load()) + { return; } - shouldStop.store(true); + shouldStop.store(true); + } // Close socket and cleanup if (cmdEndpointFdDesc) @@ -232,6 +238,8 @@ void UdpCommandDemuxer::onDataReady(const boost::system::error_code &error) return; } + smo::SpinLock::Guard lock(isActiveAndShouldStopLock); + if (!isActive.load() || shouldStop.load()) { return; } diff --git a/commonLibs/livoxProto1/udpCommandDemuxer.h b/commonLibs/livoxProto1/udpCommandDemuxer.h index be8b988..d67fe03 100644 --- a/commonLibs/livoxProto1/udpCommandDemuxer.h +++ b/commonLibs/livoxProto1/udpCommandDemuxer.h @@ -6,6 +6,7 @@ #include #include #include +#include namespace livoxProto1 { @@ -81,6 +82,7 @@ private: uint16_t dataPort; // State management + smo::SpinLock isActiveAndShouldStopLock; std::atomic isActive{false}; std::atomic shouldStop{false};