Fix data race in LiGetEstimatedRttInfo

This commit is contained in:
Cameron Gutman committed 2026-11-12 20:03:52 -06:00
1 parent d5b38a3ac6
commit a0262e78eb
3 files changed
+43 -15

No files matched your search

+30 -15
View File
@@ -126,6 +126,9 @@ static int lastConnectionStatusUpdate;
static uint32_t currentEnetSequenceNumber;
static uint64_t firstFrameTimeMs;
static atomic_uint lastEnetRtt;
static atomic_uint lastEnetRttVariance;
static LINKED_BLOCKING_QUEUE referenceFrameControlQueue;
static LINKED_BLOCKING_QUEUE frameFecStatusQueue;
static LINKED_BLOCKING_QUEUE asyncCallbackQueue;
@@ -358,6 +361,9 @@ int initializeControlStream(void) {
}
}
atomic_store_explicit(&lastEnetRtt, 0, memory_order_relaxed);
atomic_store_explicit(&lastEnetRttVariance, 0, memory_order_relaxed);
lastGoodFrame = 0;
lastSeenFrame = 0;
disconnectPending = false;
@@ -1937,25 +1943,34 @@ bool isControlDataInTransit(void) {
}
bool LiGetEstimatedRttInfo(uint32_t* estimatedRtt, uint32_t* estimatedRttVariance) {
bool ret = false;
// We do not acquire enetMutex here because we're just reading metrics
// and observing a torn write every once in a while is totally fine.
// The peer pointer points to memory reserved inside the client object,
// so it's guaranteed that it will never go away underneath us.
if (peer != NULL && peer->state == ENET_PEER_STATE_CONNECTED) {
if (estimatedRtt != NULL) {
*estimatedRtt = peer->roundTripTime;
// Opportunistically update the cached RTT stats if nobody is using ENet right now
if (PltTryLockMutex(&enetMutex)) {
if (peer != NULL && peer->state == ENET_PEER_STATE_CONNECTED) {
atomic_store_explicit(&lastEnetRttVariance, peer->roundTripTimeVariance, memory_order_relaxed);
atomic_store_explicit(&lastEnetRtt, peer->roundTripTime, memory_order_release);
}
else {
atomic_store_explicit(&lastEnetRttVariance, 0, memory_order_relaxed);
atomic_store_explicit(&lastEnetRtt, 0, memory_order_release);
}
if (estimatedRttVariance != NULL) {
*estimatedRttVariance = peer->roundTripTimeVariance;
}
ret = true;
PltUnlockMutex(&enetMutex);
}
return ret;
// Return the cached values to the caller (if present)
uint32_t lastRtt = atomic_load_explicit(&lastEnetRtt, memory_order_acquire);
if (lastRtt == 0) {
return false;
}
if (estimatedRtt != NULL) {
*estimatedRtt = lastRtt;
}
if (estimatedRttVariance != NULL) {
*estimatedRttVariance = atomic_load_explicit(&lastEnetRttVariance, memory_order_relaxed);
}
return true;
}
// Starts the control stream