Skip to content

fix: Close response body when watch content-type negotiation fails#140467

Open
karlkfi wants to merge 2 commits into
kubernetes:masterfrom
karlkfi:watch-b-close-body
Open

fix: Close response body when watch content-type negotiation fails#140467
karlkfi wants to merge 2 commits into
kubernetes:masterfrom
karlkfi:watch-b-close-body

Conversation

@karlkfi

@karlkfi karlkfi commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind bug
/sig api-machinery

What this PR does / why we need it:

When Request.Watch gets a 200 response but the negotiator has no stream decoder for the response content type, newStreamWatcher returns the error without closing resp.Body, leaking the connection.

The fix closes the body on that error path without draining it: a watch response can be a stream held open by the server, so draining could block indefinitely, and the connection cannot be reused anyway. The test holds the stream open to pin that Watch returns promptly.

Structured as two commits: the first extends TestWatchUnknownContentType with the in-package readTracker close counter and fails without the fix, the second is the fix. Test output without the fix:

=== RUN   TestWatchUnknownContentType
    request_test.go:2271: Expected response body Close to be invoked 1 time, but got: 0
--- FAIL: TestWatchUnknownContentType (0.00s)

With the fix, -race and stress are clean:

$ go test -race ./rest/...             # k8s.io/client-go
ok  	k8s.io/client-go/rest	18.041s
ok  	k8s.io/client-go/rest/watch	1.701s
$ stress -p 8 -count 200 rest.test -test.run 'TestWatch'
1m45s: 200 runs total, 0 failures

Which issue(s) this PR fixes:

Fixes #140464

Special notes for your reviewer:

Follow-up to #131706, which cleaned up response body handling in the watch tests. Originally identified as a TODO in #131505.

/cc @liggitt @jpbetz

Does this PR introduce a user-facing change?

Fixed a response body leak in the watch client when the server returns an unsupported content type.

@kubernetes-prow kubernetes-prow Bot requested a review from jpbetz July 11, 2026 23:41
@kubernetes-prow kubernetes-prow Bot added the release-note Denotes a PR that will be considered when it comes time to generate release notes. label Jul 11, 2026
@kubernetes-prow kubernetes-prow Bot requested a review from liggitt July 11, 2026 23:41
@kubernetes-prow kubernetes-prow Bot added kind/bug Categorizes issue or PR as related to a bug. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jul 11, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@kubernetes-prow kubernetes-prow Bot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Jul 11, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: karlkfi
Once this PR has been reviewed and has the lgtm label, please assign aojea for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

karlkfi added 2 commits July 11, 2026 17:16
Extend TestWatchUnknownContentType to track response body Close calls
using the existing readTracker helper, and hold the watch stream open
in the handler so the client must return the negotiation error without
waiting for the stream to end.

When content-type negotiation fails, Request.Watch returns an error
without closing the response body, leaking the connection. This test
fails without the fix in the following commit.

Originally identified as a TODO in kubernetes#131505.
When Request.Watch receives a 200 response but the negotiator has no
stream decoder for the response content type, the error was returned
without closing the response body, leaking the connection.

Close the body without draining it: a watch response can be a stream
held open by the server, so draining could block indefinitely, and the
connection cannot be reused anyway.

Originally identified as a TODO in kubernetes#131505.
@karlkfi karlkfi force-pushed the watch-b-close-body branch from fc0310a to d89d9bb Compare July 12, 2026 00:19
@kubernetes-prow kubernetes-prow Bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jul 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

client-go: Request.Watch leaks the response body when content-type negotiation fails

1 participant