From f6d9f2c184095cbca1d6eec611d4b1a681492e0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Andr=C3=A9s=20V=C3=A9lez=20Vidal?= Date: Tue, 22 Mar 2022 14:12:37 -0500 Subject: [PATCH] MM-42412 - guard watcher stop/stopped channels with mutex to prevent race condition (#19759) * MM-42412 - Guard watcher stop/stopped channels with mutex to prevent race condition * reinitialize channels on Stop fn call Co-authored-by: Pablo Velez Vidal Co-authored-by: Mattermod --- jobs/jobs_watcher.go | 7 +++++-- jobs/server_test.go | 2 -- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/jobs/jobs_watcher.go b/jobs/jobs_watcher.go index e0f57a5cf6..464cce994a 100644 --- a/jobs/jobs_watcher.go +++ b/jobs/jobs_watcher.go @@ -26,6 +26,8 @@ type Watcher struct { func (srv *JobServer) MakeWatcher(workers *Workers, pollingInterval int) *Watcher { return &Watcher{ + stop: make(chan struct{}), + stopped: make(chan struct{}), pollingInterval: pollingInterval, workers: workers, srv: srv, @@ -34,8 +36,6 @@ func (srv *JobServer) MakeWatcher(workers *Workers, pollingInterval int) *Watche func (watcher *Watcher) Start() { mlog.Debug("Watcher Started") - watcher.stop = make(chan struct{}) - watcher.stopped = make(chan struct{}) // Delay for some random number of milliseconds before starting to ensure that multiple // instances of the jobserver don't poll at a time too close to each other. rand.Seed(time.Now().UTC().UnixNano()) @@ -61,6 +61,9 @@ func (watcher *Watcher) Stop() { mlog.Debug("Watcher Stopping") close(watcher.stop) <-watcher.stopped + + watcher.stop = make(chan struct{}) + watcher.stopped = make(chan struct{}) } func (watcher *Watcher) PollAndNotify() { diff --git a/jobs/server_test.go b/jobs/server_test.go index 4e02726d7c..9c550c54d9 100644 --- a/jobs/server_test.go +++ b/jobs/server_test.go @@ -11,7 +11,6 @@ import ( ) func TestStartWorkers(t *testing.T) { - t.Skip("MM-42412") t.Run("uninitialized", func(t *testing.T) { jobServer, _, _ := makeJobServer(t) err := jobServer.StartWorkers() @@ -44,7 +43,6 @@ func TestStartWorkers(t *testing.T) { } func TestStopWorkers(t *testing.T) { - t.Skip("MM-42412") t.Run("uninitialized", func(t *testing.T) { jobServer, _, _ := makeJobServer(t) err := jobServer.StopWorkers()