From bf731558cda02f1f2a4003e0bbbe0d1d604b62bc Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 19 May 2020 14:31:24 -0700 Subject: [PATCH] remove panic on double close watch channel (#3223) --- cmd/client-s3.go | 6 ++++-- cmd/watch-main.go | 2 +- cmd/watch.go | 21 --------------------- 3 files changed, 5 insertions(+), 24 deletions(-) diff --git a/cmd/client-s3.go b/cmd/client-s3.go index 76d8d0fd..56e5a8fa 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -801,8 +801,10 @@ func (c *S3Client) Watch(options WatchOptions) (*WatchObject, *probe.Error) { } go func() { - wg.Wait() - wo.Close() + wg.Wait() // this occurs when all watchers have returned + // we can safely close data and error channels + close(wo.EventInfoChan) + close(wo.ErrorChan) }() return wo, nil diff --git a/cmd/watch-main.go b/cmd/watch-main.go index 6fdfbdaf..f9f54666 100644 --- a/cmd/watch-main.go +++ b/cmd/watch-main.go @@ -162,7 +162,7 @@ func mainWatch(ctx *cli.Context) error { fatalIf(err, "Cannot watch on the specified bucket.") // Initialize.. waitgroup to track the go-routine. - wg := sync.WaitGroup{} + var wg sync.WaitGroup // Increment wait group to wait subsequent routine. wg.Add(1) diff --git a/cmd/watch.go b/cmd/watch.go index 0edd6dde..921cab3f 100644 --- a/cmd/watch.go +++ b/cmd/watch.go @@ -93,14 +93,6 @@ func (w *WatchObject) Errors() chan *probe.Error { return w.ErrorChan } -// Close the watcher, will stop all goroutines -func (w *WatchObject) Close() { - // Cleanup - close(w.EventInfoChan) - close(w.ErrorChan) - close(w.DoneChan) -} - // Watcher can be used to have one or multiple clients watch for notifications type Watcher struct { sessionStartTime time.Time @@ -137,19 +129,6 @@ func (w *Watcher) Events() chan []EventInfo { return w.EventInfoChan } -// Stop watcher -func (w *Watcher) Stop() { - // close all running goroutines - for _, wo := range w.o { - wo.Close() - } - - w.wg.Wait() - - close(w.ErrorChan) - close(w.EventInfoChan) -} - // Watching returns if the watcher is watching for notifications func (w *Watcher) Watching() bool { return (len(w.o) > 0)