Skip to content

fix(lib/backend): close the registry ping response body in updateChallenge - #751

Merged
thijmv merged 2 commits into
uber:masterfrom
Sami-Juhani:fix/fix-lib-backend-close-the-registry-ping
Nov 19, 2026
Merged

thijmv merged 2 commits into
uber:masterfrom
Sami-Juhani:fix/fix-lib-backend-close-the-registry-ping

Conversation

@Sami-Juhani

Copy link
Copy Markdown
Contributor

What was wrong

If the registry backend has credentials configured (BasicAuth or RemoteCredentialsStore, see shouldAuth at lib/backend/registrybackend/security/security.go:125-127), every Authenticate call goes through updateChallenge (lib/backend/registrybackend/security/security.go:114). updateChallenge sends GET <address>/v2/ with httputil.Send and accepts 200 and 401 (lib/backend/registrybackend/security/security.go:150-155). When the status code is accepted, Send returns the response with its body still open (utils/httputil/httputil.go:370-373). The body is closed only for rejected codes, inside NewStatusError (utils/httputil/httputil.go:55-60). updateChallenge reads the headers and passes the response to challengeManager.AddResponse. None of its return paths call resp.Body.Close() (lib/backend/registrybackend/security/security.go:159-168).

Authenticate runs on every registry request, not once per client:

  • BlobClient.Stat and BlobClient.Download: lib/backend/registrybackend/blobclient.go:84 and lib/backend/registrybackend/blobclient.go:100
  • TagClient.Stat and TagClient.Download: lib/backend/registrybackend/tagclient.go:89 and lib/backend/registrybackend/tagclient.go:125

That means every blob or tag stat or download against an authenticated registry leaves one ping response body open. The http.Transport can't put that connection back in its idle pool or close it while the body is open. So the connection, its file descriptor and the transport's read/write goroutines stay allocated, and their number grows with traffic.

What this changes

Right after the error check in updateChallenge, the patch adds defer closers.Close(resp.Body). This is the same helper the registry clients already use after their own Send calls (lib/backend/registrybackend/blobclient.go:150, lib/backend/registrybackend/tagclient.go:142). It logs a close error at debug level (utils/closers/closer.go:12-23). Because the close is deferred, it runs on all three return paths: v2 with AddResponse succeeding, AddResponse failing, and "registry is not v2". The only other change is the new import. auth.APIVersions and AddResponse read the response before the function returns, so closing the body on return doesn't change what they see.

The patch also adds a test, TestUpdateChallengeClosesResponseBody in lib/backend/registrybackend/security/security_test.go. It builds an authenticator around a stub http.RoundTripper that returns a fixed status and Docker-Distribution-Api-Version header, and records each response body along with whether Close was called on it. It runs three cases: 200 with v2, 401 with v2, and 200 with no version header (the "not v2" error path). Each case calls updateChallenge three times, then requires every recorded body to be closed.

Our assessment: this resource leak grows with registry pull traffic. Any deployment whose registry backend has credentials configured and TLS enabled is affected. A process that runs long enough can eventually run out of file descriptors. Deployments with TLS client disabled, or without credentials, return before updateChallenge is called (lib/backend/registrybackend/security/security.go:99-113), so they are not affected.

Not changed here: the HEAD responses in BlobClient.statHelper (lib/backend/registrybackend/blobclient.go:116-131) and TagClient.Stat (lib/backend/registrybackend/tagclient.go:95-114) are not closed either. I left them out to keep this change small; they could go in a follow-up.

Testing

Verified on commit a58d1aaf3c57 against base caf1816eda31.

  • Without the fix (must fail), go test ./lib/backend/registrybackend/security: go test -race -tags unit -count=1 ./lib/backend/registrybackend/security → exit 1, failed as it must
  • With the fix, go test ./lib/backend/registrybackend/security: go test -race -tags unit -count=1 ./lib/backend/registrybackend/security → exit 0, passed
  • Tests of the touched packages, go test (touched packages): go test -race -tags unit -count=1 ./lib/backend/registrybackend/security → exit 0, passed (ok github.com/uber/kraken/lib/backend/registrybackend/security 1.060s)
  • Lint, golangci-lint: golangci-lint run ./lib/backend/registrybackend/security → exit 0, passed
  • Format, gofmt: gofmt -l lib/backend/registrybackend/security/security.go lib/backend/registrybackend/security/security_test.go → exit 0, passed

Without the fix:

--- FAIL: TestUpdateChallengeClosesResponseBody (0.00s)
    --- FAIL: TestUpdateChallengeClosesResponseBody/ok_v2 (0.00s)
        security_test.go:90: 
            	Error Trace:	lib/backend/registrybackend/security/security_test.go:90
            	Error:      	Should be true
            	Test:       	TestUpdateChallengeClosesResponseBody/ok_v2
            	Messages:   	response body not closed
    --- FAIL: TestUpdateChallengeClosesResponseBody/unauthorized_v2 (0.00s)
        security_test.go:90: 
            	Error Trace:	lib/backend/registrybackend/security/security_test.go:90
            	Error:      	Should be true
            	Test:       	TestUpdateChallengeClosesResponseBody/unauthorized_v2
            	Messages:   	response body not closed
    --- FAIL: TestUpdateChallengeClosesResponseBody/not_v2 (0.00s)
        security_test.go:90: 
            	Error Trace:	lib/backend/registrybackend/security/security_test.go:90
            	Error:      	Should be true
            	Test:       	TestUpdateChallengeClosesResponseBody/not_v2
            	Messages:   	response body not closed
FAIL
FAIL	github.com/uber/kraken/lib/backend/registrybackend/security	0.057s
FAIL

This was found and prepared with AI assistance and reviewed by a human before sending.

@cla-assistant

cla-assistant Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@gkeesh7
gkeesh7 requested a review from thijmv November 16, 2026 07:39
@thijmv thijmv self-assigned this Nov 16, 2026
@thijmv
thijmv requested a balanced review from Copilot November 18, 2026 09:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block approval of the registry authentication response-body cleanup change.

No open issues 🎉

What changed in this PR

Fixes a response-body leak in the registry backend’s authentication flow by closing registry ping responses when updateChallenge returns.

Changes:

  • Defers response-body cleanup using the existing closers.Close helper.
  • Adds regression tests verifying cleanup across repeated calls for HTTP 200 and 401 v2 responses and the non-v2 error path.
File Description
lib/​backend/​registrybackend/​security/​security.go Closes successful ping responses on every subsequent return path.
lib/​backend/​registrybackend/​security/​security_test.go Adds a tracking response body and table-driven cleanup tests.

Review effort: Balanced

desc string
status int
version string
wantErr bool

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Consider using wantErr string with require.ErrorContains.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thijmv Done. wantErr is now a string. The “not v2” case expects “registry is not v2” and is checked with require.ErrorContains, so a different failure can no longer pass that case.

@thijmv thijmv left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please rebase your branch to fix the integration tests?

…lenge

`updateChallenge` never closes `resp.Body` from `httputil.Send` (Send
returns accepted 200/401 responses with the body still open), so each
call leaks one connection, its fd and its read/write goroutines. Callers
read: blobclient/tagclient Stat and Download call Authenticate on every
request, so the leak grows with each pull until fds run out. Add `defer
resp.Body.Close()` after the error check.

Trigger: Registry backend configured with BasicAuth or
RemoteCredentialsStore. Every blob/tag Stat or Download calls
Authenticate, then updateChallenge, which sends GET <address>/v2/ and
gets a 200 or 401 back. updateChallenge returns without closing
resp.Body. After N pulls, N TCP connections, file descriptors and
transport goroutines stay held.

- Authenticate runs on every blob/tag Stat and Download, so the unclosed
  /v2/ ping body held one connection, fd and transport goroutines per call.
@Sami-Juhani
Sami-Juhani force-pushed the fix/fix-lib-backend-close-the-registry-ping branch from cfe231b to 2e32880 Compare November 18, 2026 17:26
@thijmv
thijmv merged commit 2873ae3 into uber:master Nov 19, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants