Merge pull request #281 from tonistiigi/load-fix

build: improve error checking on load
pull/321/head
Tõnis Tiigi 5 years ago committed by GitHub
commit fd6de6b6ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -817,20 +817,27 @@ func newDockerLoader(ctx context.Context, d DockerAPI, name string, mw *progress
} }
pr, pw := io.Pipe() pr, pw := io.Pipe()
started := make(chan struct{}) done := make(chan struct{})
w := &waitingWriter{
ctx, cancel := context.WithCancel(ctx)
var w *waitingWriter
w = &waitingWriter{
PipeWriter: pw, PipeWriter: pw,
f: func() { f: func() {
resp, err := c.ImageLoad(ctx, pr, false) resp, err := c.ImageLoad(ctx, pr, false)
defer close(done)
if err != nil { if err != nil {
pr.CloseWithError(err) pr.CloseWithError(err)
w.mu.Lock()
w.err = err
w.mu.Unlock()
return return
} }
prog := mw.WithPrefix("", false) prog := mw.WithPrefix("", false)
close(started)
progress.FromReader(prog, "importing to docker", resp.Body) progress.FromReader(prog, "importing to docker", resp.Body)
}, },
started: started, done: done,
cancel: cancel,
} }
return w, func() { return w, func() {
pr.Close() pr.Close()
@ -839,11 +846,12 @@ func newDockerLoader(ctx context.Context, d DockerAPI, name string, mw *progress
type waitingWriter struct { type waitingWriter struct {
*io.PipeWriter *io.PipeWriter
f func() f func()
once sync.Once once sync.Once
mu sync.Mutex mu sync.Mutex
err error err error
started chan struct{} done chan struct{}
cancel func()
} }
func (w *waitingWriter) Write(dt []byte) (int, error) { func (w *waitingWriter) Write(dt []byte) (int, error) {
@ -855,6 +863,11 @@ func (w *waitingWriter) Write(dt []byte) (int, error) {
func (w *waitingWriter) Close() error { func (w *waitingWriter) Close() error {
err := w.PipeWriter.Close() err := w.PipeWriter.Close()
<-w.started <-w.done
if err == nil {
w.mu.Lock()
defer w.mu.Unlock()
return w.err
}
return err return err
} }

Loading…
Cancel
Save