Bug:UdpCmdDemux: Add SpinLock for races around stop()

This commit is contained in:
2025-11-07 20:45:16 -04:00
parent 7d2cb58200
commit 887fa1ab6f
2 changed files with 15 additions and 5 deletions
+13 -5
View File
@@ -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; }
@@ -6,6 +6,7 @@
#include <memory>
#include <boost/asio/posix/stream_descriptor.hpp>
#include <componentThread.h>
#include <spinLock.h>
namespace livoxProto1 {
@@ -81,6 +82,7 @@ private:
uint16_t dataPort;
// State management
smo::SpinLock isActiveAndShouldStopLock;
std::atomic<bool> isActive{false};
std::atomic<bool> shouldStop{false};