Skip to content

Commit 9f6058d

Browse files
committed
pushWriter: correctly propagate errors
In the refactor from 926b9c7, the error handling was substantially reworked, and changed the types of errors returned. Notably, in the case of a network error, instead of propogating the error through to return from pushWriter.Write (as previously), it would be propagated through to pushWriter.Commit - however, this is too late, since we've already closed the io.Pipe by the time we would have reached this function. Therefore, we get the generic error message "io: read/write on closed pipe" for *every network error*. This patch corrects this behavior to ensure that the correct error object is always returned as early as possible, by checking the error result after writing and detecting a closed pipe. Additionally, we do some additional hardening - specifically we prevent falling through when resetting the content or detecting errors, and update the tests to explicitly check for the ErrReset message. Signed-off-by: Justin Chadwell <[email protected]>
1 parent 3f565da commit 9f6058d

File tree

2 files changed

+35
-38
lines changed

2 files changed

+35
-38
lines changed

remotes/docker/pusher.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -380,17 +380,24 @@ func (pw *pushWriter) Write(p []byte) (n int, err error) {
380380

381381
// If content has already been written, the bytes
382382
// cannot be written and the caller must reset
383-
if status.Offset > 0 {
384-
status.Offset = 0
385-
status.UpdatedAt = time.Now()
386-
pw.tracker.SetStatus(pw.ref, status)
387-
return 0, content.ErrReset
388-
}
383+
status.Offset = 0
384+
status.UpdatedAt = time.Now()
385+
pw.tracker.SetStatus(pw.ref, status)
386+
return 0, content.ErrReset
389387
default:
390388
}
391389
}
392390

393391
n, err = pw.pipe.Write(p)
392+
if errors.Is(err, io.ErrClosedPipe) {
393+
// if the pipe is closed, we might have the original error on the error
394+
// channel - so we should try and get it
395+
select {
396+
case err2 := <-pw.errC:
397+
err = err2
398+
default:
399+
}
400+
}
394401
status.Offset += int64(n)
395402
status.UpdatedAt = time.Now()
396403
pw.tracker.SetStatus(pw.ref, status)
@@ -431,7 +438,7 @@ func (pw *pushWriter) Digest() digest.Digest {
431438

432439
func (pw *pushWriter) Commit(ctx context.Context, size int64, expected digest.Digest, opts ...content.Opt) error {
433440
// Check whether read has already thrown an error
434-
if _, err := pw.pipe.Write([]byte{}); err != nil && err != io.ErrClosedPipe {
441+
if _, err := pw.pipe.Write([]byte{}); err != nil && !errors.Is(err, io.ErrClosedPipe) {
435442
return fmt.Errorf("pipe error before commit: %w", err)
436443
}
437444

@@ -442,9 +449,7 @@ func (pw *pushWriter) Commit(ctx context.Context, size int64, expected digest.Di
442449
var resp *http.Response
443450
select {
444451
case err := <-pw.errC:
445-
if err != nil {
446-
return err
447-
}
452+
return err
448453
case resp = <-pw.respC:
449454
defer resp.Body.Close()
450455
case p, ok := <-pw.pipeC:
@@ -456,18 +461,17 @@ func (pw *pushWriter) Commit(ctx context.Context, size int64, expected digest.Di
456461
}
457462
pw.pipe.CloseWithError(content.ErrReset)
458463
pw.pipe = p
464+
465+
// If content has already been written, the bytes
466+
// cannot be written again and the caller must reset
459467
status, err := pw.tracker.GetStatus(pw.ref)
460468
if err != nil {
461469
return err
462470
}
463-
// If content has already been written, the bytes
464-
// cannot be written again and the caller must reset
465-
if status.Offset > 0 {
466-
status.Offset = 0
467-
status.UpdatedAt = time.Now()
468-
pw.tracker.SetStatus(pw.ref, status)
469-
return content.ErrReset
470-
}
471+
status.Offset = 0
472+
status.UpdatedAt = time.Now()
473+
pw.tracker.SetStatus(pw.ref, status)
474+
return content.ErrReset
471475
}
472476

473477
// 201 is specified return status, some registries return

remotes/docker/pusher_test.go

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -117,26 +117,19 @@ func TestPusherErrReset(t *testing.T) {
117117
}
118118

119119
w, err := p.push(context.Background(), desc, remotes.MakeRefKey(context.Background(), desc), false)
120-
assert.Equal(t, err, nil, "no error should be there")
121-
122-
w.Write(ct)
123-
124-
pw, _ := w.(*pushWriter)
125-
126-
select {
127-
case p := <-pw.pipeC:
128-
p.Write(ct)
129-
case e := <-pw.errC:
130-
assert.Failf(t, "error: %v while retrying request", e.Error())
131-
}
132-
133-
select {
134-
case resp := <-pw.respC:
135-
assert.Equalf(t, resp.StatusCode, http.StatusCreated,
136-
"201 should be the response code when uploading new content")
137-
case <-pw.errC:
138-
assert.Fail(t, "should not give error")
139-
}
120+
assert.NoError(t, err)
121+
122+
// first push should fail with ErrReset
123+
_, err = w.Write(ct)
124+
assert.NoError(t, err)
125+
err = w.Commit(context.Background(), desc.Size, desc.Digest)
126+
assert.Equal(t, content.ErrReset, err)
127+
128+
// second push should succeed
129+
_, err = w.Write(ct)
130+
assert.NoError(t, err)
131+
err = w.Commit(context.Background(), desc.Size, desc.Digest)
132+
assert.NoError(t, err)
140133
}
141134

142135
func tryUpload(ctx context.Context, t *testing.T, p dockerPusher, layerContent []byte) error {

0 commit comments

Comments
 (0)