Skip to content

Commit 2bcd66d

Browse files
authored
Merge pull request #6482 from tonistiigi/v0.27-picks-2
[v0.27] forwarder: fix concurrent map write panic in the gateway forwarder
2 parents 64bc21a + 6c53e9e commit 2bcd66d

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

frontend/gateway/forwarder/forward.go

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ type BridgeClient struct {
5757
buildOpts client.BuildOpts
5858
ctrs []client.Container
5959
executor executor.Executor
60-
mounts map[string]snapshot.Mounter
60+
61+
mounts map[string]snapshot.Mounter
62+
mountsMu sync.Mutex
6163
}
6264

6365
func (c *BridgeClient) Solve(ctx context.Context, req client.SolveRequest) (*client.Result, error) {
@@ -215,9 +217,7 @@ func (c *BridgeClient) discard(err error) {
215217
ctr.Release(context.TODO())
216218
}
217219

218-
for _, mount := range c.mounts {
219-
mount.Unmount()
220-
}
220+
c.discardMounts()
221221

222222
for id, workerRef := range c.workerRefByID {
223223
workerRef.ImmutableRef.Release(context.TODO())
@@ -235,6 +235,16 @@ func (c *BridgeClient) discard(err error) {
235235
}
236236
}
237237

238+
func (c *BridgeClient) discardMounts() {
239+
c.mountsMu.Lock()
240+
defer c.mountsMu.Unlock()
241+
242+
for _, mount := range c.mounts {
243+
mount.Unmount()
244+
}
245+
c.mounts = nil
246+
}
247+
238248
func (c *BridgeClient) Warn(ctx context.Context, dgst digest.Digest, msg string, opts client.WarnOpts) error {
239249
return c.FrontendLLBBridge.Warn(ctx, dgst, msg, opts)
240250
}
@@ -388,26 +398,28 @@ func (r *ref) StatFile(ctx context.Context, req client.StatRequest) (*fstypes.St
388398

389399
func (r *ref) getMounter(ctx context.Context) (snapshot.Mounter, error) {
390400
id := r.resultProxy.ID()
391-
if mounter, ok := r.c.mounts[id]; ok {
392-
return mounter, nil
393-
}
394401

395-
rr, err := r.resultProxy.Result(ctx)
396-
if err != nil {
397-
return nil, r.c.wrapSolveError(err)
398-
}
399-
ref, ok := rr.Sys().(*worker.WorkerRef)
402+
r.c.mountsMu.Lock()
403+
defer r.c.mountsMu.Unlock()
404+
405+
mounter, ok := r.c.mounts[id]
400406
if !ok {
401-
return nil, errors.Errorf("invalid ref: %T", rr.Sys())
402-
}
407+
rr, err := r.resultProxy.Result(ctx)
408+
if err != nil {
409+
return nil, r.c.wrapSolveError(err)
410+
}
411+
ref, ok := rr.Sys().(*worker.WorkerRef)
412+
if !ok {
413+
return nil, errors.Errorf("invalid ref: %T", rr.Sys())
414+
}
403415

404-
mountable, err := ref.ImmutableRef.Mount(ctx, true, r.session)
405-
if err != nil {
406-
return nil, err
416+
mountable, err := ref.ImmutableRef.Mount(ctx, true, r.session)
417+
if err != nil {
418+
return nil, err
419+
}
420+
mounter = snapshot.LocalMounter(mountable)
421+
r.c.mounts[id] = mounter
407422
}
408-
mounter := snapshot.LocalMounter(mountable)
409-
410-
r.c.mounts[id] = mounter
411423
return mounter, nil
412424
}
413425

0 commit comments

Comments
 (0)