From ee38ab858125d68c7357133a9644dbf4dd6479a3 Mon Sep 17 00:00:00 2001 From: Konstantin Lebedev <9497591+kmlebedev@users.noreply.github.com> Date: Sun, 16 Oct 2022 23:28:56 +0500 Subject: [PATCH] avoid data race of LogBuffer isStopping (#3859) --- weed/util/log_buffer/log_buffer.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/weed/util/log_buffer/log_buffer.go b/weed/util/log_buffer/log_buffer.go index 86857b563..2d12a5bf2 100644 --- a/weed/util/log_buffer/log_buffer.go +++ b/weed/util/log_buffer/log_buffer.go @@ -118,6 +118,13 @@ func (m *LogBuffer) AddToBuffer(partitionKey, data []byte, processingTsNs int64) } +func (m *LogBuffer) IsStopping() bool { + m.RLock() + defer m.RUnlock() + + return m.isStopping +} + func (m *LogBuffer) Shutdown() { m.Lock() defer m.Unlock() @@ -144,13 +151,12 @@ func (m *LogBuffer) loopFlush() { } func (m *LogBuffer) loopInterval() { - for !m.isStopping { + for !m.IsStopping() { time.Sleep(m.flushInterval) - m.Lock() - if m.isStopping { - m.Unlock() + if m.IsStopping() { return } + m.Lock() toFlush := m.copyToFlush() m.Unlock() if toFlush != nil {