From 9f2143e3df2c8b777c77e3bebdfb29f85d0c5517 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Wed, 3 May 2023 17:11:08 -0700 Subject: [PATCH] container-driver: avoid hanging on container panic Signed-off-by: Tonis Tiigi --- driver/docker-container/driver.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/driver/docker-container/driver.go b/driver/docker-container/driver.go index 14e589d8..56f99d5a 100644 --- a/driver/docker-container/driver.go +++ b/driver/docker-container/driver.go @@ -9,6 +9,7 @@ import ( "path" "path/filepath" "strings" + "sync/atomic" "time" "github.com/docker/buildx/driver" @@ -369,7 +370,11 @@ func (d *Driver) Client(ctx context.Context) (*client.Client, error) { } var opts []client.ClientOpt + var counter int64 opts = append(opts, client.WithContextDialer(func(context.Context, string) (net.Conn, error) { + if atomic.AddInt64(&counter, 1) > 1 { + return nil, net.ErrClosed + } return conn, nil })) if td, ok := exp.(client.TracerDelegate); ok { @@ -395,7 +400,10 @@ func (d *Driver) Features() map[driver.Feature]bool { func demuxConn(c net.Conn) net.Conn { pr, pw := io.Pipe() // TODO: rewrite parser with Reader() to avoid goroutine switch - go stdcopy.StdCopy(pw, os.Stderr, c) + go func() { + _, err := stdcopy.StdCopy(pw, os.Stderr, c) + pw.CloseWithError(err) + }() return &demux{ Conn: c, Reader: pr,