From a0262e78ebe493585729820b423d6a79043e413f Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Thu, 12 Nov 2026 20:03:52 -0600 Subject: [PATCH] Fix data race in LiGetEstimatedRttInfo --- src/ControlStream.c | 45 ++++++++++++++++++++++++++++--------------- src/Platform.c | 12 ++++++++++++ src/PlatformThreads.h | 1 + 3 files changed, 43 insertions(+), 15 deletions(-) diff --git a/src/ControlStream.c b/src/ControlStream.c index 9c4b66d..3babb7f 100644 --- a/src/ControlStream.c +++ b/src/ControlStream.c @@ -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 diff --git a/src/Platform.c b/src/Platform.c index bc77a15..78011bc 100644 --- a/src/Platform.c +++ b/src/Platform.c @@ -166,6 +166,18 @@ void PltLockMutex(PLT_MUTEX* mutex) { #endif } +bool PltTryLockMutex(PLT_MUTEX* mutex) { +#if defined(LC_WINDOWS) + return TryAcquireSRWLockExclusive(mutex); +#elif defined(__WIIU__) + return OSFastMutex_TryLock(mutex); +#elif defined(__3DS__) + return LightLock_TryLock(mutex) == 0; +#else + return pthread_mutex_trylock(mutex) == 0; +#endif +} + void PltUnlockMutex(PLT_MUTEX* mutex) { #if defined(LC_WINDOWS) ReleaseSRWLockExclusive(mutex); diff --git a/src/PlatformThreads.h b/src/PlatformThreads.h index d95b3de..786c266 100644 --- a/src/PlatformThreads.h +++ b/src/PlatformThreads.h @@ -52,6 +52,7 @@ typedef struct _PLT_EVENT { int PltCreateMutex(PLT_MUTEX* mutex); void PltDeleteMutex(PLT_MUTEX* mutex); void PltLockMutex(PLT_MUTEX* mutex); +bool PltTryLockMutex(PLT_MUTEX* mutex); void PltUnlockMutex(PLT_MUTEX* mutex); int PltCreateThread(const char* name, ThreadEntry entry, void* context, PLT_THREAD* thread);