diff --git a/builder/builder-next/adapters/snapshot/layer.go b/builder/builder-next/adapters/snapshot/layer.go index ed13def16fdee..e7724d20102f5 100644 --- a/builder/builder-next/adapters/snapshot/layer.go +++ b/builder/builder-next/adapters/snapshot/layer.go @@ -22,6 +22,9 @@ func (s *snapshotter) GetDiffIDs(ctx context.Context, key string) ([]layer.DiffI } func (s *snapshotter) EnsureLayer(ctx context.Context, key string) ([]layer.DiffID, error) { + s.layerCreateLocker.Lock(key) + defer s.layerCreateLocker.Unlock(key) + diffIDs, err := s.GetDiffIDs(ctx, key) if err != nil { return nil, err diff --git a/builder/builder-next/adapters/snapshot/snapshot.go b/builder/builder-next/adapters/snapshot/snapshot.go index 656c79ec20bfd..8f8df8f06be16 100644 --- a/builder/builder-next/adapters/snapshot/snapshot.go +++ b/builder/builder-next/adapters/snapshot/snapshot.go @@ -16,6 +16,7 @@ import ( "github.com/docker/docker/pkg/idtools" "github.com/moby/buildkit/identity" "github.com/moby/buildkit/snapshot" + "github.com/moby/locker" "github.com/opencontainers/go-digest" "github.com/pkg/errors" bolt "go.etcd.io/bbolt" @@ -48,10 +49,11 @@ type checksumCalculator interface { type snapshotter struct { opt Opt - refs map[string]layer.Layer - db *bolt.DB - mu sync.Mutex - reg graphIDRegistrar + refs map[string]layer.Layer + db *bolt.DB + mu sync.Mutex + reg graphIDRegistrar + layerCreateLocker *locker.Locker } // NewSnapshotter creates a new snapshotter @@ -68,10 +70,11 @@ func NewSnapshotter(opt Opt, prevLM leases.Manager) (snapshot.Snapshotter, lease } s := &snapshotter{ - opt: opt, - db: db, - refs: map[string]layer.Layer{}, - reg: reg, + opt: opt, + db: db, + refs: map[string]layer.Layer{}, + reg: reg, + layerCreateLocker: locker.New(), } lm := newLeaseManager(s, prevLM)