Repository navigation
fix(lib/backend): close the registry ping response body in updateChallenge - #751
Merged
thijmv merged 2 commits intoNov 19, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
🟢 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.Closehelper. - 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
thijmv
approved these changes
Nov 18, 2026
| desc string | ||
| status int | ||
| version string | ||
| wantErr bool |
Collaborator
There was a problem hiding this comment.
nit: Consider using wantErr string with require.ErrorContains.
Contributor
Author
There was a problem hiding this comment.
@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
reviewed
Nov 18, 2026
thijmv
left a comment
Collaborator
There was a problem hiding this comment.
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
force-pushed
the
fix/fix-lib-backend-close-the-registry-ping
branch
from
November 18, 2026 17:26
cfe231b to
2e32880
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
If the registry backend has credentials configured (
BasicAuthorRemoteCredentialsStore, seeshouldAuthatlib/backend/registrybackend/security/security.go:125-127), everyAuthenticatecall goes throughupdateChallenge(lib/backend/registrybackend/security/security.go:114).updateChallengesendsGET <address>/v2/withhttputil.Sendand accepts 200 and 401 (lib/backend/registrybackend/security/security.go:150-155). When the status code is accepted,Sendreturns the response with its body still open (utils/httputil/httputil.go:370-373). The body is closed only for rejected codes, insideNewStatusError(utils/httputil/httputil.go:55-60).updateChallengereads the headers and passes the response tochallengeManager.AddResponse. None of its return paths callresp.Body.Close()(lib/backend/registrybackend/security/security.go:159-168).Authenticateruns on every registry request, not once per client:BlobClient.StatandBlobClient.Download:lib/backend/registrybackend/blobclient.go:84andlib/backend/registrybackend/blobclient.go:100TagClient.StatandTagClient.Download:lib/backend/registrybackend/tagclient.go:89andlib/backend/registrybackend/tagclient.go:125That means every blob or tag stat or download against an authenticated registry leaves one ping response body open. The
http.Transportcan'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 addsdefer closers.Close(resp.Body). This is the same helper the registry clients already use after their ownSendcalls (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 withAddResponsesucceeding,AddResponsefailing, and "registry is not v2". The only other change is the new import.auth.APIVersionsandAddResponseread the response before the function returns, so closing the body on return doesn't change what they see.The patch also adds a test,
TestUpdateChallengeClosesResponseBodyinlib/backend/registrybackend/security/security_test.go. It builds anauthenticatoraround a stubhttp.RoundTripperthat returns a fixed status andDocker-Distribution-Api-Versionheader, and records each response body along with whetherClosewas 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 callsupdateChallengethree 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
updateChallengeis called (lib/backend/registrybackend/security/security.go:99-113), so they are not affected.Not changed here: the
HEADresponses inBlobClient.statHelper(lib/backend/registrybackend/blobclient.go:116-131) andTagClient.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
a58d1aaf3c57against basecaf1816eda31.go test -race -tags unit -count=1 ./lib/backend/registrybackend/security→ exit 1, failed as it mustgo test -race -tags unit -count=1 ./lib/backend/registrybackend/security→ exit 0, passedgo test -race -tags unit -count=1 ./lib/backend/registrybackend/security→ exit 0, passed (ok github.com/uber/kraken/lib/backend/registrybackend/security 1.060s)golangci-lint run ./lib/backend/registrybackend/security→ exit 0, passedgofmt -l lib/backend/registrybackend/security/security.go lib/backend/registrybackend/security/security_test.go→ exit 0, passedWithout the fix:
This was found and prepared with AI assistance and reviewed by a human before sending.