From 9e287ab4f0d54f709c41a3db4cc023911463df46 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Thu, 7 Apr 2022 16:49:01 +0200 Subject: [PATCH] Logger fixes - Fixed a race condition when stopping the logger - Flush log file only once per batch of log lines --- src/log.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/log.cpp b/src/log.cpp index d40767e..3f6249f 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -32,7 +32,7 @@ bool CONSOLE_COLORS = true; #ifndef P2POOL_LOG_DISABLE -static volatile bool stopped = false; +static std::atomic stopped{ false }; static volatile bool worker_started = false; #ifdef _WIN32 @@ -102,11 +102,10 @@ public: FORCEINLINE void stop() { - if (stopped) { + if (stopped.exchange(true)) { return; } - stopped = true; LOGINFO(0, "stopped"); uv_thread_join(&m_worker); uv_cond_destroy(&m_cond); @@ -208,7 +207,6 @@ private: } m_logFile.write(p, size); - m_logFile.flush(); } } @@ -218,6 +216,11 @@ private: m_readPos += SLOT_SIZE; } while (m_readPos < writePos); } + + // Flush the log file only after all pending log lines have been written + if (m_logFile.is_open()) { + m_logFile.flush(); + } } while (!stopped); }