Skip to content

Data race in Worker.Platforms() causes panic: runtime error: index out of range #6866

Closed

Description

@okhowang

Contributing guidelines and issue reporting guide

Well-formed report checklist

  • I have found a bug that the documentation does not mention anything about my problem
  • I have found a bug that there are no open or closed issues that are related to my problem
  • I have provided version/information about my environment and done my best to provide a reproducer

Description of bug

Description

Concurrent gRPC calls to ListWorkers trigger a data race in Worker.Platforms(), resulting in a panic.

Stack Trace

panic: runtime error: index out of range [81] with length 81

goroutine 1018 [running]:
github.com/moby/buildkit/worker/base.(*Worker).Platforms(...)
        worker/base/worker.go:285
github.com/moby/buildkit/control.(*Controller).ListWorkers(...)
        control/control.go:624

Root Cause

Worker.Platforms(noCache bool) at worker/base/worker.go:290 has a data race when noCache=true:

func (w *Worker) Platforms(noCache bool) []ocispecs.Platform {
    if noCache {
        matchers := make([]platforms.MatchComparer, len(w.WorkerOpt.Platforms))  // (1) read length
        for i, p := range w.WorkerOpt.Platforms {                                 // (2) iterate
            matchers[i] = platforms.Only(p)
        }
        for _, p := range archutil.SupportedPlatforms(noCache) {
            // ...
            if !exists {
                w.WorkerOpt.Platforms = append(w.WorkerOpt.Platforms, p)          // (3) mutate
            }
        }
    }
    return w.WorkerOpt.Platforms
}

When two goroutines call Platforms(true) concurrently on the same Worker instance:

  1. Goroutine A reads len(w.WorkerOpt.Platforms) = 81, creates matchers with length 81
  2. Goroutine B enters the method, iterates through the first loop, and appends new platforms via w.WorkerOpt.Platforms = append(...) at step (3), extending the slice
  3. Due to the data race (undefined behavior), Goroutine A's range at step (2) may observe the extended slice, causing i to exceed matchers bounds → panic

How to Reproduce

Trigger multiple concurrent ListWorkers gRPC calls (e.g., running docker buildx ls from multiple terminals simultaneously, or CI systems polling worker status). The race is more likely when noCache=true causes SupportedPlatforms to return newly detected platforms that trigger append.

Fix

Add a sync.Mutex to protect the Platforms method:

 type Worker struct {
 	WorkerOpt
 	CacheMgr        cache.Manager
 	SourceManager   *source.Manager
 	imageWriter     *imageexporter.ImageWriter
 	ImageSource     *containerimage.Source
 	OCILayoutSource *containerimage.Source
 	GitSource       *git.Source
 	HTTPSource      *http.Source
+	platformsMu     sync.Mutex
 }

 func (w *Worker) Platforms(noCache bool) []ocispecs.Platform {
+	w.platformsMu.Lock()
+	defer w.platformsMu.Unlock()
 	if noCache {

Environment

  • BuildKit version: (affected versions including master)
  • Docker version: 29.5.x
  • OS: Linux

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions