livoxProto1:Device: Spinlock guard heartbeat stop() for races

This commit is contained in:
2025-11-07 21:36:00 -04:00
parent 7b092956c0
commit 1afa085fd4
2 changed files with 19 additions and 8 deletions
+15 -6
View File
@@ -1042,6 +1042,8 @@ void Device::startHeartbeat()
}
// Register heartbeat ACK handler (cmd_set=0x00, cmd_id=0x03)
smo::SpinLock::Guard lock(heartbeatActiveLock);
registerUdpCommandHandler(
0x00, 0x03, discardHeartbeatAck, discoveredDevice.ipAddr);
@@ -1057,14 +1059,16 @@ void Device::startHeartbeat()
void Device::stopHeartbeat()
{
if (heartbeatActive.load())
{
smo::SpinLock::Guard lock(heartbeatActiveLock);
heartbeatActive.store(false);
if (heartbeatTimer) {
heartbeatTimer->cancel();
unregisterUdpCommandHandler(0x00, 0x03, discoveredDevice.ipAddr);
}
unregisterUdpCommandHandler(0x00, 0x03, discoveredDevice.ipAddr);
if (heartbeatTimer) {
heartbeatTimer->cancel();
heartbeatTimer.reset();
}
}
@@ -1140,7 +1144,6 @@ void Device::sendHeartbeat()
}
catch (const std::exception& e)
{
heartbeatActive.store(false);
std::cerr << __func__ << ": Heartbeat send failed for device "
<< discoveredDevice.deviceIdentifier
<< ": " << e.what() << std::endl;
@@ -1156,15 +1159,21 @@ void Device::onHeartbeatTimer(const boost::system::error_code& error)
if (error)
{
heartbeatActive.store(false);
std::cerr << "[" << __func__ << "] Heartbeat timer error for device "
<< discoveredDevice.deviceIdentifier
<< ": " << error.message() << std::endl;
return;
}
// Send next heartbeat
{
smo::SpinLock::Guard lock(heartbeatActiveLock);
if (!heartbeatActive.load())
{ return; }
sendHeartbeat();
}
}
uint32_t Device::getSubnetMaskFor(uint8_t nbits)
+2
View File
@@ -17,6 +17,7 @@
#include <boost/asio/posix/stream_descriptor.hpp>
#include "protocol.h"
#include <callback.h>
#include <spinLock.h>
// Custom hash function for std::pair<uint8_t, uint8_t>
namespace std {
@@ -162,6 +163,7 @@ public:
// Heartbeat state
std::unique_ptr<boost::asio::deadline_timer> heartbeatTimer;
std::atomic<bool> heartbeatActive;
smo::SpinLock heartbeatActiveLock;
// Point cloud data state
std::atomic<bool> pcloudDataActive;