Skip to content

fix: close the response body in the wait command's poll loop - #2700

Open
rootkiller6788 wants to merge 2 commits into
GoogleCloudPlatform:mainfrom
rootkiller6788:fix-wait-response-body-leak
Open

rootkiller6788 wants to merge 2 commits into
GoogleCloudPlatform:mainfrom
rootkiller6788:fix-wait-response-body-leak

Conversation

@rootkiller6788

Copy link
Copy Markdown

The wait subcommand polls the other proxy's /startup endpoint once a second until it gets a 200, and it never closed the response body. Go only returns a connection to the pool if the body is read and closed, so every poll opened a fresh connection to the proxy instead of reusing one. With the default 30s --max that's up to 30 connections, and the command is meant to be run in tight sidecar/init-container loops.

The fix reads the body into io.Discard and closes it before checking the status code, so both the ready and the still-starting paths release it. Behaviour is otherwise unchanged: same retry cadence, same timeout.

The test installs a RoundTripper on http.DefaultClient that keeps a handle on the bodies it hands out, then runs wait against a 200 and a 503 so both exit paths are covered. It fails on current main with response body was left open and passes with the fix.

The wait subcommand hits the startup endpoint once a second until it
gets a 200 and never closed the response body. Go can't hand the
connection back to the pool unless the body is read and closed, so
each poll burned a new connection to the proxy.

Read the body into io.Discard and close it before checking the status
code, so both the ready and the still-starting paths release it.
Adds a test that swaps in a RoundTripper which keeps a handle on the
response bodies it returns, then runs wait against both a 200 and a
503 so both exit paths are covered. Fails without the fix.

@blacksoxx blacksoxx left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Checked this locally at 4754348: it merges clean with current main, go test -race ./cmd -run TestWaitCommand passes, and with only the new test applied on main both cases fail with "response body was left open", so the test does pin the fix.

One mechanics note: the conventionalcommits.org check is failing on the PR title. A fix: prefix (for example "fix: close the response body in the wait command's poll loop") should clear it.

@rootkiller6788 rootkiller6788 changed the title Close the response body in the wait command's poll loop fix: close the response body in the wait command's poll loop Nov 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants