Skip to content

Commit 34a1cb1

Browse files
lujindak8s-infra-cherrypick-robot
authored andcommitted
fix(dockerFetcher): resolve deadlock issue in dockerFetcher open
Signed-off-by: jinda.ljd <[email protected]>
1 parent 1a989e4 commit 34a1cb1

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

core/remotes/docker/fetcher.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,11 @@ func (r dockerFetcher) open(ctx context.Context, req *request, mediatype string,
565565
ReadCloser: io.NopCloser(io.MultiReader(readers...)),
566566
}
567567
} else {
568+
defer func() {
569+
if retErr != nil {
570+
r.Release(1)
571+
}
572+
}()
568573
body = &fnOnClose{
569574
BeforeClose: func() {
570575
r.Release(1)

core/remotes/docker/fetcher_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,40 @@ func TestDockerFetcherOpen(t *testing.T) {
559559
}
560560
}
561561

562+
func TestDockerFetcherOpenLimiterDeadlock(t *testing.T) {
563+
s := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
564+
rw.Header().Set("Content-Encoding", "gzip")
565+
rw.Write([]byte("bad gzip data"))
566+
rw.WriteHeader(http.StatusOK)
567+
}))
568+
defer s.Close()
569+
570+
u, err := url.Parse(s.URL)
571+
if err != nil {
572+
t.Fatal(err)
573+
}
574+
575+
f := dockerFetcher{&dockerBase{
576+
repository: "ns",
577+
limiter: semaphore.NewWeighted(int64(1)),
578+
}}
579+
580+
host := RegistryHost{
581+
Client: s.Client(),
582+
Host: u.Host,
583+
Scheme: u.Scheme,
584+
Path: u.Path,
585+
}
586+
587+
req := f.request(host, http.MethodGet)
588+
_, err = f.open(context.Background(), req, "", 0, true)
589+
assert.Error(t, err)
590+
591+
// verify that the limiter Release has been successfully called when the last open error occurred
592+
_, err = f.open(context.Background(), req, "", 0, true)
593+
assert.Error(t, err)
594+
}
595+
562596
// httpRange specifies the byte range to be sent to the client.
563597
type httpRange struct {
564598
start, length int64

0 commit comments

Comments
 (0)