Skip to content

Commit dcfcc90

Browse files
Merge pull request #126125 from mprahl/stop-idempotent
Allow calling Stop multiple times on RetryWatcher Kubernetes-commit: fc03f3e74c3d891e62b347c518b3197b62e9532c
2 parents bad8f77 + 001900e commit dcfcc90

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ require (
2525
golang.org/x/time v0.3.0
2626
google.golang.org/protobuf v1.34.2
2727
gopkg.in/evanphx/json-patch.v4 v4.12.0
28-
k8s.io/api v0.0.0-20240722223049-b689d905290f
28+
k8s.io/api v0.0.0-20240723194852-3421a80713ae
2929
k8s.io/apimachinery v0.0.0-20240720202316-95b78024e3fe
3030
k8s.io/klog/v2 v2.130.1
3131
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
156156
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
157157
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
158158
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
159-
k8s.io/api v0.0.0-20240722223049-b689d905290f h1:wtqzslJEcheiQ7hXjw1yGfqUyMCb7G4j72aL64Bzpbo=
160-
k8s.io/api v0.0.0-20240722223049-b689d905290f/go.mod h1:ytlEzqC2wOTwYET71W7+J+k7O2V7vrDuzmNLBSpgT+k=
159+
k8s.io/api v0.0.0-20240723194852-3421a80713ae h1:mV43yijQh5/Wf7fwSuyATasDFY+YJxjuXs1ecY5M1Bc=
160+
k8s.io/api v0.0.0-20240723194852-3421a80713ae/go.mod h1:ytlEzqC2wOTwYET71W7+J+k7O2V7vrDuzmNLBSpgT+k=
161161
k8s.io/apimachinery v0.0.0-20240720202316-95b78024e3fe h1:V9MwpYUwbKlfLKVrhpVuKWiat/LBIhm1pGB9/xdHm5Q=
162162
k8s.io/apimachinery v0.0.0-20240720202316-95b78024e3fe/go.mod h1:rsPdaZJfTfLsNJSQzNHQvYoTmxhoOEofxtOsF3rtsMo=
163163
k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk=

tools/watch/retrywatcher.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"io"
2424
"net/http"
25+
"sync"
2526
"time"
2627

2728
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -53,6 +54,7 @@ type RetryWatcher struct {
5354
stopChan chan struct{}
5455
doneChan chan struct{}
5556
minRestartDelay time.Duration
57+
stopChanLock sync.Mutex
5658
}
5759

5860
// NewRetryWatcher creates a new RetryWatcher.
@@ -286,7 +288,15 @@ func (rw *RetryWatcher) ResultChan() <-chan watch.Event {
286288

287289
// Stop implements Interface.
288290
func (rw *RetryWatcher) Stop() {
289-
close(rw.stopChan)
291+
rw.stopChanLock.Lock()
292+
defer rw.stopChanLock.Unlock()
293+
294+
// Prevent closing an already closed channel to prevent a panic
295+
select {
296+
case <-rw.stopChan:
297+
default:
298+
close(rw.stopChan)
299+
}
290300
}
291301

292302
// Done allows the caller to be notified when Retry watcher stops.

tools/watch/retrywatcher_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,8 @@ func TestRetryWatcherToFinishWithUnreadEvents(t *testing.T) {
585585
// Give the watcher a chance to get to sending events (blocking)
586586
time.Sleep(10 * time.Millisecond)
587587

588+
watcher.Stop()
589+
// Verify a second stop does not cause a panic
588590
watcher.Stop()
589591

590592
maxTime := time.Second

0 commit comments

Comments
 (0)