From e533ac495f7754c41fa52a2726c969489a31c412 Mon Sep 17 00:00:00 2001 From: Bryan Biedenkapp Date: Wed, 19 Mar 2025 15:31:44 -0400 Subject: [PATCH] fix potential nullptr problem with Log when used from multiple threads during shutdown; reduce the intercycle delay to 2ms from 5ms for all main FNE threads; --- src/common/Log.cpp | 10 +++++++--- src/fne/HostFNE.cpp | 29 +++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/common/Log.cpp b/src/common/Log.cpp index 01553284..b1668f06 100644 --- a/src/common/Log.cpp +++ b/src/common/Log.cpp @@ -191,8 +191,10 @@ void LogFinalise() #if defined(CATCH2_TEST_COMPILATION) return; #endif - if (m_fpLog != nullptr) + if (m_fpLog != nullptr) { ::fclose(m_fpLog); + m_fpLog = nullptr; + } #if !defined(_WIN32) if (g_useSyslog) closelog(); @@ -330,8 +332,10 @@ void Log(uint32_t level, const char *module, const char* file, const int lineNo, if (!ret) return; - ::fprintf(m_fpLog, "%s\n", buffer); - ::fflush(m_fpLog); + if (m_fpLog != nullptr) { + ::fprintf(m_fpLog, "%s\n", buffer); + ::fflush(m_fpLog); + } } else { #if !defined(_WIN32) // convert our log level into syslog level diff --git a/src/fne/HostFNE.cpp b/src/fne/HostFNE.cpp index 7709916e..82ca25c2 100644 --- a/src/fne/HostFNE.cpp +++ b/src/fne/HostFNE.cpp @@ -39,6 +39,8 @@ using namespace lookups; // Constants // --------------------------------------------------------------------------- +#define THREAD_CYCLE_THRESHOLD 2U + #define IDLE_WARMUP_MS 5U #define DEFAULT_MTU_SIZE 496 @@ -646,10 +648,18 @@ void* HostFNE::threadMasterNetwork(void* arg) ::pthread_setname_np(th->thread, threadName.c_str()); #endif // _GNU_SOURCE + StopWatch stopWatch; + stopWatch.start(); + if (fne->m_network != nullptr) { while (!g_killed) { + uint32_t ms = stopWatch.elapsed(); + stopWatch.start(); + fne->m_network->processNetwork(); - Thread::sleep(5U); + + if (ms < THREAD_CYCLE_THRESHOLD) + Thread::sleep(THREAD_CYCLE_THRESHOLD); } } @@ -694,10 +704,18 @@ void* HostFNE::threadDiagNetwork(void* arg) ::pthread_setname_np(th->thread, threadName.c_str()); #endif // _GNU_SOURCE + StopWatch stopWatch; + stopWatch.start(); + if (fne->m_diagNetwork != nullptr) { while (!g_killed) { + uint32_t ms = stopWatch.elapsed(); + stopWatch.start(); + fne->m_diagNetwork->processNetwork(); - Thread::sleep(5U); + + if (ms < THREAD_CYCLE_THRESHOLD) + Thread::sleep(THREAD_CYCLE_THRESHOLD); } } @@ -890,6 +908,7 @@ void* HostFNE::threadVirtualNetworking(void* arg) stopWatch.start(); while (!g_killed) { + uint32_t ms = stopWatch.elapsed(); stopWatch.start(); uint8_t packet[DEFAULT_MTU_SIZE]; @@ -908,7 +927,8 @@ void* HostFNE::threadVirtualNetworking(void* arg) } } - Thread::sleep(2U); + if (ms < THREAD_CYCLE_THRESHOLD) + Thread::sleep(THREAD_CYCLE_THRESHOLD); } } @@ -968,7 +988,8 @@ void* HostFNE::threadVirtualNetworkingClock(void* arg) break; } - Thread::sleep(2U); + if (ms < THREAD_CYCLE_THRESHOLD) + Thread::sleep(THREAD_CYCLE_THRESHOLD); } }