From 72d5a79798506915465a83229a159efe8d7a361f Mon Sep 17 00:00:00 2001 From: John Donaldson Date: Tue, 17 May 2022 13:30:33 -0700 Subject: [PATCH] Fixing some race condition issues --- cmd/mothd/state_filesystem.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/cmd/mothd/state_filesystem.go b/cmd/mothd/state_filesystem.go index 989d0d7..2ab6862 100644 --- a/cmd/mothd/state_filesystem.go +++ b/cmd/mothd/state_filesystem.go @@ -420,6 +420,8 @@ func (s *State) awardPointsAtTime(when int64, teamID string, category string, po } func (s *State) PointExists(teamID string, cat string, points int) bool { + s.pointsLock.RLock() + defer s.pointsLock.RUnlock() for _, pointEntry := range s.pointsLog { if (pointEntry.TeamID == teamID) && (pointEntry.Category == cat) && (pointEntry.Points == points) { return true @@ -430,6 +432,9 @@ func (s *State) PointExists(teamID string, cat string, points int) bool { } func (s *State) PointExistsAtTime(teamID string, cat string, points int, when int64) bool { + s.pointsLock.RLock() + defer s.pointsLock.RUnlock() + for _, pointEntry := range s.pointsLog { if (pointEntry.TeamID == teamID) && (pointEntry.Category == cat) && (pointEntry.Points == points) && (pointEntry.When == when) { return true @@ -745,8 +750,7 @@ func (s *State) reopenEventLog() error { } func (s *State) updateCaches() { - s.pointsLock.RLock() - defer s.pointsLock.RUnlock() + // Re-read the points log { @@ -769,8 +773,9 @@ func (s *State) updateCaches() { } pointsLog = append(pointsLog, cur) } - + s.pointsLock.Lock() s.pointsLog = pointsLog + s.pointsLock.Unlock() } }