Skip to content

ESD-1546: Close response body on Do error returns - #167

Open
Phil-Browne wants to merge 5 commits into
mainfrom
fix/do-close-body-on-error
Open

ESD-1546: Close response body on Do error returns#167
Phil-Browne wants to merge 5 commits into
mainfrom
fix/do-close-body-on-error

Conversation

@Phil-Browne

@Phil-Browne Phil-Browne commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Client.Do returned on its error paths (failed CheckResponse, or a copy / decode error) without closing resp.Body, leaking the underlying connection.

Adds a deferred close guarded by a success flag so the body is released on every error return. On success the caller still owns the body, so behavior there is unchanged.

doDiscard, which backs the mutations whose response payload is ignored, no longer returns the body-close error. A close failure means the connection was already broken, not that the request failed, so it must not turn a completed mutation into a reported failure.

Includes regression tests that assert the body is closed when Do returns an error, that the original body is still closed exactly once when response-body logging swaps in a replacement reader, and that doDiscard ignores a close error. The first and last fail without the fix.

Client.Do returned without closing resp.Body on its error paths (failed
CheckResponse, copy or decode error), leaking the underlying connection.
Add a deferred close guarded by a success flag so the body is released on
every error return; on success the caller still owns the body.
@Phil-Browne Phil-Browne changed the title fix: close response body on Do error returns ESD-1543: Close response body on Do error returns Jun 26, 2026
@Phil-Browne Phil-Browne changed the title ESD-1543: Close response body on Do error returns ESD-1546: Close response body on Do error returns Jul 5, 2026
Reorder the LogResponseBody read-error path so the body is closed exactly
once, add a doDiscard helper that drains and closes the body, and switch
the call sites that discarded the response to it.
The existing regression test only covered the CheckResponse failure
path. Add coverage for the other two error returns that the deferred
close protects.
A close error means the connection was already broken, not that the
request failed. doDiscard backs mutations whose response payload is
ignored, so returning that error turned a completed mutation into a
reported failure.

Adds a test for the close-error path and one for the response-body
logging branch, where Do closes the original body and swaps in a
replacement reader.
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.

1 participant