Skip to content

Commit

Permalink
fix pusher concurrent close channel
Browse files Browse the repository at this point in the history
Signed-off-by: rongfu.leng <rongfu.leng@daocloud.io>
(cherry picked from commit 63a7d8a)
Signed-off-by: Akhil Mohan <makhil@vmware.com>
  • Loading branch information
lengrongfu authored and akhilerm committed Nov 3, 2022
1 parent 4a4232f commit 28595a3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
17 changes: 7 additions & 10 deletions remotes/docker/pusher.go
Expand Up @@ -23,6 +23,7 @@ import (
"net/http"
"net/url"
"strings"
"sync"
"time"

"github.com/containerd/containerd/content"
Expand Down Expand Up @@ -317,9 +318,10 @@ type pushWriter struct {

pipe *io.PipeWriter

pipeC chan *io.PipeWriter
respC chan *http.Response
errC chan error
pipeC chan *io.PipeWriter
respC chan *http.Response
closeOnce sync.Once
errC chan error

isManifest bool

Expand Down Expand Up @@ -395,14 +397,9 @@ func (pw *pushWriter) Write(p []byte) (n int, err error) {
func (pw *pushWriter) Close() error {
// Ensure pipeC is closed but handle `Close()` being
// called multiple times without panicking
select {
case _, ok := <-pw.pipeC:
if ok {
close(pw.pipeC)
}
default:
pw.closeOnce.Do(func() {
close(pw.pipeC)
}
})
if pw.pipe != nil {
status, err := pw.tracker.GetStatus(pw.ref)
if err == nil && !status.Committed {
Expand Down
8 changes: 4 additions & 4 deletions remotes/docker/pusher_test.go
Expand Up @@ -293,7 +293,7 @@ func Test_dockerPusher_push(t *testing.T) {
dp dockerPusher
dockerBaseObject string
args args
checkerFunc func(writer pushWriter) bool
checkerFunc func(writer *pushWriter) bool
wantErr error
}{
{
Expand All @@ -306,7 +306,7 @@ func Test_dockerPusher_push(t *testing.T) {
ref: fmt.Sprintf("manifest-%s", manifestContentDigest.String()),
unavailableOnFail: false,
},
checkerFunc: func(writer pushWriter) bool {
checkerFunc: func(writer *pushWriter) bool {
select {
case resp := <-writer.respC:
// 201 should be the response code when uploading a new manifest
Expand Down Expand Up @@ -340,7 +340,7 @@ func Test_dockerPusher_push(t *testing.T) {
ref: fmt.Sprintf("layer-%s", layerContentDigest.String()),
unavailableOnFail: false,
},
checkerFunc: func(writer pushWriter) bool {
checkerFunc: func(writer *pushWriter) bool {
select {
case resp := <-writer.respC:
// 201 should be the response code when uploading a new blob
Expand Down Expand Up @@ -381,7 +381,7 @@ func Test_dockerPusher_push(t *testing.T) {
}

// test whether a proper response has been received after the push operation
assert.True(t, test.checkerFunc(*pw))
assert.True(t, test.checkerFunc(pw))

})
}
Expand Down

0 comments on commit 28595a3

Please sign in to comment.