From 285979e96c7b5416aec40d6a8561e61e37ff3065 Mon Sep 17 00:00:00 2001 From: Michael Schurter Date: Tue, 16 Aug 2022 10:50:40 -0700 Subject: [PATCH] deployments: fix data races (#14121) * deployments: fix data races Both priority and state related fields may be mutated concurrently and need to be accessed with the lock acquired. --- nomad/deploymentwatcher/deployment_watcher.go | 2 ++ nomad/deploymentwatcher/deployments_watcher.go | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/nomad/deploymentwatcher/deployment_watcher.go b/nomad/deploymentwatcher/deployment_watcher.go index dec0c8f2c..32648c081 100644 --- a/nomad/deploymentwatcher/deployment_watcher.go +++ b/nomad/deploymentwatcher/deployment_watcher.go @@ -840,10 +840,12 @@ func (w *deploymentWatcher) getEval() *structs.Evaluation { // on the previous version that are then "watched" on a leader that's on // the new version. This would result in an eval with its priority set to // zero which would be bad. This therefore protects against that. + w.l.Lock() priority := w.d.EvalPriority if priority == 0 { priority = w.j.Priority } + w.l.Unlock() return &structs.Evaluation{ ID: uuid.Generate(), diff --git a/nomad/deploymentwatcher/deployments_watcher.go b/nomad/deploymentwatcher/deployments_watcher.go index 56617430f..46db7a73c 100644 --- a/nomad/deploymentwatcher/deployments_watcher.go +++ b/nomad/deploymentwatcher/deployments_watcher.go @@ -193,7 +193,12 @@ func (w *Watcher) watchDeployments(ctx context.Context) { // getDeploys retrieves all deployments blocking at the given index. func (w *Watcher) getDeploys(ctx context.Context, minIndex uint64) ([]*structs.Deployment, uint64, error) { - resp, index, err := w.state.BlockingQuery(w.getDeploysImpl, minIndex, ctx) + // state can be updated concurrently + w.l.Lock() + stateStore := w.state + w.l.Unlock() + + resp, index, err := stateStore.BlockingQuery(w.getDeploysImpl, minIndex, ctx) if err != nil { return nil, 0, err }