Skip to content

Commit

Permalink
fix potential data race in PeriodicalExecutor (zeromicro#344)
Browse files Browse the repository at this point in the history
* fix potential data race in PeriodicalExecutor

* add comment
  • Loading branch information
kevwan authored Jan 3, 2021
1 parent 771371e commit efa4348
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions core/executors/periodicalexecutor.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,6 @@ func (pe *PeriodicalExecutor) backgroundFlush() {
} else if pe.Flush() {
last = timex.Now()
} else if pe.shallQuit(last) {
pe.lock.Lock()
pe.guarded = false
pe.lock.Unlock()
return
}
}
Expand Down Expand Up @@ -180,8 +177,18 @@ func (pe *PeriodicalExecutor) hasTasks(tasks interface{}) bool {
}
}

func (pe *PeriodicalExecutor) shallQuit(last time.Duration) bool {
idleEnough := timex.Since(last) > pe.interval*idleRound
noPending := atomic.LoadInt32(&pe.inflight) == 0
return idleEnough && noPending
func (pe *PeriodicalExecutor) shallQuit(last time.Duration) (stop bool) {
if timex.Since(last) <= pe.interval*idleRound {
return
}

// checking pe.inflight and setting pe.guarded should be locked together
pe.lock.Lock()
if atomic.LoadInt32(&pe.inflight) == 0 {
pe.guarded = false
stop = true
}
pe.lock.Unlock()

return
}

0 comments on commit efa4348

Please sign in to comment.