From cce6451b6346152ff5f73a1fdda85ef4669c3099 Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Sat, 26 Feb 2022 05:15:21 +0100 Subject: [PATCH] watch: Clean up watch when restarting mirror after an error (#3993) Add cleanup for watch code when it exits and mirroring is restarted. --- cmd/client-fs.go | 3 +++ cmd/client-s3.go | 42 +++++++++++++++++++++++++----------------- cmd/mirror-main.go | 2 ++ cmd/watch.go | 14 +++++++------- 4 files changed, 37 insertions(+), 24 deletions(-) diff --git a/cmd/client-fs.go b/cmd/client-fs.go index b677e7eb..0b6a7836 100644 --- a/cmd/client-fs.go +++ b/cmd/client-fs.go @@ -181,6 +181,9 @@ func (f *fsClient) Watch(ctx context.Context, options WatchOptions) (*WatchObjec close(eventChan) close(errorChan) notify.Stop(in) + // At this point, notify is guaranteed to not write + // in 'in' channel so we can close it. + close(in) }() timeFormatFS := "2006-01-02T15:04:05.000Z" diff --git a/cmd/client-s3.go b/cmd/client-s3.go index b7f51372..27b639ed 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -783,26 +783,34 @@ func (c *S3Client) Watch(ctx context.Context, options WatchOptions) (*WatchObjec } go func() { - // Start listening on all bucket events. - for notificationInfo := range eventsCh { - if notificationInfo.Err != nil { - var perr *probe.Error - if minio.ToErrorResponse(notificationInfo.Err).Code == "NotImplemented" { - perr = probe.NewError(APINotImplemented{ - API: "Watch", - APIType: c.GetURL().String(), - }) - } else { - perr = probe.NewError(notificationInfo.Err) + defer close(wo.EventInfoChan) + defer close(wo.ErrorChan) + + for { + // Start listening on all bucket events. + select { + case notificationInfo, ok := <-eventsCh: + if !ok { + return } - wo.Errors() <- perr - } else { - wo.Events() <- c.notificationToEventsInfo(notificationInfo) + if notificationInfo.Err != nil { + var perr *probe.Error + if minio.ToErrorResponse(notificationInfo.Err).Code == "NotImplemented" { + perr = probe.NewError(APINotImplemented{ + API: "Watch", + APIType: c.GetURL().String(), + }) + } else { + perr = probe.NewError(notificationInfo.Err) + } + wo.Errors() <- perr + } else { + wo.Events() <- c.notificationToEventsInfo(notificationInfo) + } + case <-wo.DoneChan: + return } } - - close(wo.EventInfoChan) - close(wo.ErrorChan) }() return wo, nil diff --git a/cmd/mirror-main.go b/cmd/mirror-main.go index 28a08817..0d67cff6 100644 --- a/cmd/mirror-main.go +++ b/cmd/mirror-main.go @@ -645,6 +645,8 @@ func (mj *mirrorJob) watchMirrorEvents(ctx context.Context, events []EventInfo) // this goroutine will watch for notifications, and add modified objects to the queue func (mj *mirrorJob) watchMirror(ctx context.Context) { + defer mj.watcher.Stop() + for { select { case events, ok := <-mj.watcher.Events(): diff --git a/cmd/watch.go b/cmd/watch.go index b8958efc..9684c97f 100644 --- a/cmd/watch.go +++ b/cmd/watch.go @@ -103,13 +103,11 @@ func (w *Watcher) Events() chan []EventInfo { return w.EventInfoChan } -// Watching returns if the watcher is watching for notifications -func (w *Watcher) Watching() bool { - return (len(w.o) > 0) -} - -// Wait for watcher to wait -func (w *Watcher) Wait() { +// Stop all watchers +func (w *Watcher) Stop() { + for _, w := range w.o { + close(w.DoneChan) + } w.wg.Wait() } @@ -135,6 +133,8 @@ func (w *Watcher) Join(ctx context.Context, client Client, recursive bool) *prob for { select { + case <-wo.DoneChan: + return case events, ok := <-wo.Events(): if !ok { return