From 6cf535a86f4042f5d9646ee90c24e70b32121c30 Mon Sep 17 00:00:00 2001 From: James Rasell Date: Wed, 4 Jun 2025 14:11:17 +0100 Subject: [PATCH] drainer: Fix data race within test by correctly copying alloc. (#25975) Some test cases were writing the same allocation object (memory pointer) to Nomad state in subsequent upsert calls. This causes a race condition with the drainer job watcher which reads the same object from Nomad state to perform conditional checks. The data race is fixed by ensuring the allocation is copied between writes. --- nomad/drainer/watch_jobs_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nomad/drainer/watch_jobs_test.go b/nomad/drainer/watch_jobs_test.go index 05a2a509e..e0cc7ff94 100644 --- a/nomad/drainer/watch_jobs_test.go +++ b/nomad/drainer/watch_jobs_test.go @@ -193,7 +193,7 @@ func TestDrainingJobWatcher_DrainJobs(t *testing.T) { replacement.NodeID = runningNode.ID // start in pending state with no health status - updates = append(updates, a, replacement) + updates = append(updates, a.Copy(), replacement) replacements[i] = replacement.Copy() } must.NoError(t, store.UpsertAllocs(structs.MsgTypeTestSetup, index, updates)) @@ -254,7 +254,7 @@ func TestDrainingJobWatcher_DrainJobs(t *testing.T) { a.ClientStatus = structs.AllocClientStatusComplete replacement := newAlloc(runningNode, a.Job) - updates = append(updates, a, replacement) + updates = append(updates, a.Copy(), replacement) replacements[i] = replacement.Copy() } must.NoError(t, store.UpsertAllocs(structs.MsgTypeTestSetup, index, updates)) @@ -298,7 +298,7 @@ func TestDrainingJobWatcher_DrainJobs(t *testing.T) { a.ClientStatus = structs.AllocClientStatusComplete replacement := newAlloc(runningNode, a.Job) - updates = append(updates, a, replacement) + updates = append(updates, a.Copy(), replacement) replacements[i] = replacement.Copy() } must.NoError(t, store.UpsertAllocs(structs.MsgTypeTestSetup, index, updates))