From cee4610dd6200f4a2e24b4d9aa1adf3309705c89 Mon Sep 17 00:00:00 2001 From: James Munnelly Date: Fri, 1 Dec 2017 22:54:12 +0000 Subject: [PATCH] Enable go race detector and fix race --- Makefile | 1 + pkg/scheduler/scheduler.go | 11 +++++------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 82de54c4c..ba28abab2 100644 --- a/Makefile +++ b/Makefile @@ -76,6 +76,7 @@ $(CMDS): go_test: go test -v \ + -race \ $$(go list ./... | \ grep -v '/vendor/' | \ grep -v '/test/e2e' | \ diff --git a/pkg/scheduler/scheduler.go b/pkg/scheduler/scheduler.go index ac99ef5d0..3e742520c 100644 --- a/pkg/scheduler/scheduler.go +++ b/pkg/scheduler/scheduler.go @@ -35,19 +35,18 @@ func NewScheduledWorkQueue(processFunc ProcessFunc) ScheduledWorkQueue { // Duration has come (since the time Add was called). If an existing Timer for // obj already exists, the previous timer will be cancelled. func (s *scheduledWorkQueue) Add(obj interface{}, duration time.Duration) { - s.clearTimer(obj) + // we call Forget before acquiring the workLock in order to avoid deadlock + s.Forget(obj) + s.workLock.Lock() + defer s.workLock.Unlock() s.work[obj] = time.AfterFunc(duration, func() { - defer s.clearTimer(obj) + defer s.Forget(obj) s.processFunc(obj) }) } // Forget will cancel the timer for the given object, if the timer exists. func (s *scheduledWorkQueue) Forget(obj interface{}) { - s.clearTimer(obj) -} - -func (s *scheduledWorkQueue) clearTimer(obj interface{}) { s.workLock.Lock() defer s.workLock.Unlock() if timer, ok := s.work[obj]; ok {