mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 13:37:30 +00:00
Merge pull request #29743 from timothysc/wait_race_fix
Automatic merge from submit-queue Fix race condition found in JitterUntil. This was caused by the recent addition of "sliding" manifested in: https://github.com/kubernetes/kubernetes/issues/26782
This commit is contained in:
commit
9fab05fe59
@ -20,8 +20,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"k8s.io/kubernetes/pkg/util/runtime"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// For any test of the style:
|
// For any test of the style:
|
||||||
@ -64,13 +62,14 @@ func NonSlidingUntil(f func(), period time.Duration, stopCh <-chan struct{}) {
|
|||||||
// stop channel is already closed. Pass NeverStop to Until if you
|
// stop channel is already closed. Pass NeverStop to Until if you
|
||||||
// don't want it stop.
|
// don't want it stop.
|
||||||
func JitterUntil(f func(), period time.Duration, jitterFactor float64, sliding bool, stopCh <-chan struct{}) {
|
func JitterUntil(f func(), period time.Duration, jitterFactor float64, sliding bool, stopCh <-chan struct{}) {
|
||||||
|
for {
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-stopCh:
|
case <-stopCh:
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
|
||||||
jitteredPeriod := period
|
jitteredPeriod := period
|
||||||
if jitterFactor > 0.0 {
|
if jitterFactor > 0.0 {
|
||||||
jitteredPeriod = Jitter(period, jitterFactor)
|
jitteredPeriod = Jitter(period, jitterFactor)
|
||||||
@ -82,22 +81,18 @@ func JitterUntil(f func(), period time.Duration, jitterFactor float64, sliding b
|
|||||||
}
|
}
|
||||||
|
|
||||||
func() {
|
func() {
|
||||||
defer runtime.HandleCrash()
|
|
||||||
f()
|
f()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if sliding {
|
if sliding {
|
||||||
t = time.NewTimer(jitteredPeriod)
|
t = time.NewTimer(jitteredPeriod)
|
||||||
} else {
|
|
||||||
// The timer we created could already have fired, so be
|
|
||||||
// careful and check stopCh first.
|
|
||||||
select {
|
|
||||||
case <-stopCh:
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NOTE: b/c there is no priority selection in golang
|
||||||
|
// it is possible for this to race, meaning we could
|
||||||
|
// trigger t.C and stopCh, and t.C select falls through.
|
||||||
|
// In order to mitigate we re-check stopCh at the beginning
|
||||||
|
// of every loop to prevent extra executions of f().
|
||||||
select {
|
select {
|
||||||
case <-stopCh:
|
case <-stopCh:
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user