Skip to content

Commit

Permalink
frontend: make sure interactive containers are released on disconnect
Browse files Browse the repository at this point in the history
Even when client does not release a container it should
be released automatically on disconnect.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
(cherry picked from commit 80aa8b4)
  • Loading branch information
tonistiigi committed Aug 22, 2022
1 parent a88f415 commit b8cdffd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions frontend/gateway/container.go
Expand Up @@ -361,6 +361,8 @@ func (gwCtr *gatewayContainer) Start(ctx context.Context, req client.StartReques
}

func (gwCtr *gatewayContainer) Release(ctx context.Context) error {
gwCtr.mu.Lock()
defer gwCtr.mu.Unlock()
gwCtr.cancel()
err1 := gwCtr.errGroup.Wait()

Expand All @@ -371,6 +373,7 @@ func (gwCtr *gatewayContainer) Release(ctx context.Context) error {
err2 = err
}
}
gwCtr.cleanup = nil

if err1 != nil {
return stack.Enable(err1)
Expand Down
6 changes: 6 additions & 0 deletions frontend/gateway/forwarder/forward.go
Expand Up @@ -52,6 +52,7 @@ type bridgeClient struct {
workers worker.Infos
workerRefByID map[string]*worker.WorkerRef
buildOpts client.BuildOpts
ctrs []client.Container
}

func (c *bridgeClient) Solve(ctx context.Context, req client.SolveRequest) (*client.Result, error) {
Expand Down Expand Up @@ -212,6 +213,10 @@ func (c *bridgeClient) toFrontendResult(r *client.Result) (*frontend.Result, err
}

func (c *bridgeClient) discard(err error) {
for _, ctr := range c.ctrs {
ctr.Release(context.TODO())
}

for id, workerRef := range c.workerRefByID {
workerRef.ImmutableRef.Release(context.TODO())
delete(c.workerRefByID, id)
Expand Down Expand Up @@ -300,6 +305,7 @@ func (c *bridgeClient) NewContainer(ctx context.Context, req client.NewContainer
if err != nil {
return nil, err
}
c.ctrs = append(c.ctrs, ctr)
return ctr, nil
}

Expand Down
7 changes: 7 additions & 0 deletions frontend/gateway/gateway.go
Expand Up @@ -345,6 +345,13 @@ func (b *bindMount) IdentityMapping() *idtools.IdentityMapping {
func (lbf *llbBridgeForwarder) Discard() {
lbf.mu.Lock()
defer lbf.mu.Unlock()

for ctr := range lbf.ctrs {
lbf.ReleaseContainer(context.TODO(), &pb.ReleaseContainerRequest{
ContainerID: ctr,
})
}

for id, workerRef := range lbf.workerRefByID {
workerRef.ImmutableRef.Release(context.TODO())
delete(lbf.workerRefByID, id)
Expand Down

0 comments on commit b8cdffd

Please sign in to comment.