@@ -219,10 +219,14 @@ func (c *Coordinator) ExecuteAndBuildSpecs(ctx context.Context, batchSpec *batch
219219 c .exec .Start (ctx , tasks , ui )
220220 results , errs := c .exec .Wait ()
221221
222+ // Create a copy of results to safely iterate over during cache operations
223+ resultsCopy := make ([]taskResult , len (results ))
224+ copy (resultsCopy , results )
225+
222226 // Write all step cache results to the cache.
223227 // Lock to protect cache operations from race conditions
224228 c .cacheMutex .Lock ()
225- for _ , res := range results {
229+ for _ , res := range resultsCopy {
226230 for _ , stepRes := range res .stepResults {
227231 cacheKey := res .task .CacheKey (c .opts .GlobalEnv , c .opts .ExecOpts .WorkingDirectory , stepRes .StepIndex )
228232 if err := c .opts .Cache .Set (ctx , cacheKey , stepRes ); err != nil {
@@ -236,7 +240,8 @@ func (c *Coordinator) ExecuteAndBuildSpecs(ctx context.Context, batchSpec *batch
236240 var specs []* batcheslib.ChangesetSpec
237241
238242 // Build ChangesetSpecs if possible and add to list.
239- for _ , taskResult := range results {
243+ // Using the copy of results to avoid race conditions
244+ for _ , taskResult := range resultsCopy {
240245 // Don't build changeset specs for failed workspaces.
241246 if taskResult .err != nil {
242247 continue
0 commit comments