Skip to content

Commit

Permalink
copy: setError should imply Close
Browse files Browse the repository at this point in the history
If sending two messages from goroutine X:

	a <- 1
	b <- 2

And receiving them in goroutine Y:

	select {
	case <- a:
	case <- b:
	}

Either branch of the select can trigger first - so when we call
.setError and .Close next to each other, we don't know whether the done
channel will close first or the error channel will receive first - so
sometimes, we get an incorrect error message.

We resolve this by not sending both signals - instead, we can have
.setError *imply* .Close, by having the pushWriter call .Close on
itself, after receiving an error.

Signed-off-by: Justin Chadwell <me@jedevc.com>
  • Loading branch information
jedevc committed Mar 4, 2024
1 parent a800400 commit 8727463
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions remotes/docker/pusher.go
Expand Up @@ -292,7 +292,6 @@ func (p dockerPusher) push(ctx context.Context, desc ocispec.Descriptor, ref str
resp, err := req.doWithRetries(ctx, nil)
if err != nil {
pushw.setError(err)
pushw.Close()
return
}

Expand All @@ -302,7 +301,6 @@ func (p dockerPusher) push(ctx context.Context, desc ocispec.Descriptor, ref str
err := remoteserrors.NewUnexpectedStatusErr(resp)
log.G(ctx).WithField("resp", resp).WithField("body", string(err.(remoteserrors.ErrUnexpectedStatus).Body)).Debug("unexpected response")
pushw.setError(err)
pushw.Close()
}
pushw.setResponse(resp)
}()
Expand Down Expand Up @@ -435,6 +433,7 @@ func (pw *pushWriter) Write(p []byte) (n int, err error) {
select {
case <-pw.done:
case err = <-pw.errC:
pw.Close()
case p := <-pw.pipeC:
return 0, pw.replacePipe(p)
}
Expand Down Expand Up @@ -492,6 +491,7 @@ func (pw *pushWriter) Commit(ctx context.Context, size int64, expected digest.Di
case <-pw.done:
return io.ErrClosedPipe
case err := <-pw.errC:
pw.Close()
return err
case resp = <-pw.respC:
defer resp.Body.Close()
Expand Down

0 comments on commit 8727463

Please sign in to comment.