From 5caab82485132370bbb7c8ca1a1f15d018d399f3 Mon Sep 17 00:00:00 2001 From: Ibrahim Jarif Date: Mon, 8 Jul 2019 12:06:44 +0530 Subject: [PATCH] Fix race condition in flushDiscardStats function (#921) A couple of builds failed on teamcity with data race error. See https://teamcity.dgraph.io/viewLog.html?buildId=16131 This PR fixes the data race in `flushDiscardStats` function. --- value.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/value.go b/value.go index 815e808b2..c01fa70e0 100644 --- a/value.go +++ b/value.go @@ -1396,9 +1396,13 @@ func (vlog *valueLog) updateDiscardStats(stats map[uint32]int64) error { // flushDiscardStats inserts discard stats into badger. Returns error on failure. func (vlog *valueLog) flushDiscardStats() error { + vlog.lfDiscardStats.Lock() if len(vlog.lfDiscardStats.m) == 0 { + vlog.lfDiscardStats.Unlock() return nil } + vlog.lfDiscardStats.Unlock() + entries := []*Entry{{ Key: y.KeyWithTs(lfDiscardStatsKey, 1), Value: vlog.encodedDiscardStats(),