From d5e6ecec8a5d881cf482095ec91712d3edbe81aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Aleksandrovi=C4=8D=20Klimov?= Date: Thu, 29 Jun 2023 10:42:12 +0200 Subject: [PATCH 1/2] ElasticsearchWriter#Pause(): call Flush() only once The first Flush() is redundant and may access m_DataBuffer at the same time as some Flush() in m_WorkQueue (race condition) which isn't joined, yet. --- lib/perfdata/elasticsearchwriter.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/perfdata/elasticsearchwriter.cpp b/lib/perfdata/elasticsearchwriter.cpp index 09ef31615..79b522768 100644 --- a/lib/perfdata/elasticsearchwriter.cpp +++ b/lib/perfdata/elasticsearchwriter.cpp @@ -118,8 +118,6 @@ void ElasticsearchWriter::Pause() m_HandleNotifications.disconnect(); m_FlushTimer->Stop(true); - - Flush(); m_WorkQueue.Join(); Flush(); From 076eb594437c2e5bc03b8d98913f5b175e2b7640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Aleksandrovi=C4=8D=20Klimov?= Date: Fri, 30 Jun 2023 14:57:18 +0200 Subject: [PATCH 2/2] ElasticsearchWriter#Pause(): lock m_DataBufferMutex during Flush() just to be sure regarding race conditions. --- lib/perfdata/elasticsearchwriter.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/perfdata/elasticsearchwriter.cpp b/lib/perfdata/elasticsearchwriter.cpp index 79b522768..9c8f52e60 100644 --- a/lib/perfdata/elasticsearchwriter.cpp +++ b/lib/perfdata/elasticsearchwriter.cpp @@ -119,7 +119,11 @@ void ElasticsearchWriter::Pause() m_FlushTimer->Stop(true); m_WorkQueue.Join(); - Flush(); + + { + std::unique_lock lock (m_DataBufferMutex); + Flush(); + } Log(LogInformation, "ElasticsearchWriter") << "'" << GetName() << "' paused.";