progress: make sure all channels have written before returning

Possible write on closed channel on cancellation before.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
pull/398/head
Tonis Tiigi 4 years ago
parent 232af9aa0d
commit 40fad4bbb5

@ -48,6 +48,8 @@ func ReadRemoteFiles(ctx context.Context, dis []build.DriverInfo, url string, na
return nil, nil, err return nil, nil, err
} }
ch, done := progress.NewChannel(pw)
defer func() { <-done }()
_, err = c.Build(ctx, client.SolveOpt{}, "buildx", func(ctx context.Context, c gwclient.Client) (*gwclient.Result, error) { _, err = c.Build(ctx, client.SolveOpt{}, "buildx", func(ctx context.Context, c gwclient.Client) (*gwclient.Result, error) {
def, err := st.Marshal(ctx) def, err := st.Marshal(ctx)
if err != nil { if err != nil {
@ -71,7 +73,7 @@ func ReadRemoteFiles(ctx context.Context, dis []build.DriverInfo, url string, na
files, err = filesFromRef(ctx, ref, names) files, err = filesFromRef(ctx, ref, names)
} }
return nil, err return nil, err
}, progress.NewChannel(pw)) }, ch)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err

@ -658,7 +658,9 @@ func Build(ctx context.Context, drivers []DriverInfo, opt map[string]Options, do
eg.Go(func() error { eg.Go(func() error {
defer wg.Done() defer wg.Done()
rr, err := c.Solve(ctx, nil, so, progress.NewChannel(pw)) ch, done := progress.NewChannel(pw)
defer func() { <-done }()
rr, err := c.Solve(ctx, nil, so, ch)
if err != nil { if err != nil {
return err return err
} }

@ -19,6 +19,8 @@ func createTempDockerfileFromURL(ctx context.Context, d driver.Driver, url strin
return "", err return "", err
} }
var out string var out string
ch, done := progress.NewChannel(pw)
defer func() { <-done }()
_, err = c.Build(ctx, client.SolveOpt{}, "buildx", func(ctx context.Context, c gwclient.Client) (*gwclient.Result, error) { _, err = c.Build(ctx, client.SolveOpt{}, "buildx", func(ctx context.Context, c gwclient.Client) (*gwclient.Result, error) {
def, err := llb.HTTP(url, llb.Filename("Dockerfile"), llb.WithCustomNamef("[internal] load %s", url)).Marshal(ctx) def, err := llb.HTTP(url, llb.Filename("Dockerfile"), llb.WithCustomNamef("[internal] load %s", url)).Marshal(ctx)
if err != nil { if err != nil {
@ -60,7 +62,7 @@ func createTempDockerfileFromURL(ctx context.Context, d driver.Driver, url strin
} }
out = dir out = dir
return nil, nil return nil, nil
}, progress.NewChannel(pw)) }, ch)
if err != nil { if err != nil {
return "", err return "", err

@ -39,16 +39,18 @@ func Write(w Writer, name string, f func() error) {
}) })
} }
func NewChannel(w Writer) chan *client.SolveStatus { func NewChannel(w Writer) (chan *client.SolveStatus, chan struct{}) {
ch := make(chan *client.SolveStatus) ch := make(chan *client.SolveStatus)
done := make(chan struct{})
go func() { go func() {
for { for {
v, ok := <-ch v, ok := <-ch
if !ok { if !ok {
close(done)
return return
} }
w.Write(v) w.Write(v)
} }
}() }()
return ch return ch, done
} }

Loading…
Cancel
Save