Skip to content

fix(client): close the final failed response in errorHandler - #585

Closed
vuanhphung wants to merge 4 commits into
mainfrom
fix/retry-exhausted-body-leak
Closed

vuanhphung wants to merge 4 commits into
mainfrom
fix/retry-exhausted-body-leak

Conversation

@vuanhphung

@vuanhphung vuanhphung commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

When a request through the driver's retrying HTTP client fails, go-retryablehttp passes the last response to our custom errorHandler and leaves closing it to the handler (its default handler drains and closes; client.go:807-815 @v0.7.7). errorHandler returned that response, still open, along with the error. net/http discards a response that a RoundTripper returns with an error (client.go send: "RoundTripper returned a response & error; ignoring response"), so nothing ever closed it.

Exhausted retries are only one way to get there. RetryPolicy returns (false, checkErr) for any non-200 response it doesn't retry, so a 401 or 403, a 5xx on ExecuteStatement, and telemetry or feature-flag errors also reach errorHandler. Other Thrift RPCs get there after retries run out. The leaked body kept its HTTP/1.1 connection, which counted toward MaxConnsPerHost (100), or its HTTP/2 stream open. It stayed open until the request context ended or the 900s ClientTimeout fired. Empty bodies leaked only the body. net/http also printed the line above to stderr for every failure.

Fix

errorHandler closes resp.Body and returns a nil response, as retryablehttp's default handler does. The error is unchanged: reason phrase, Thrift error header, status text and 429 rate-limit category. Retry behavior is unchanged too.

There is no explicit drain. Since Go 1.27 (this module requires 1.27.3), http.Transport drains up to 256 KiB or 50 ms after an early Close and then reuses the connection. So Close doesn't wait on the body before the error is reported, and the connection stays reusable.

Verification

Scenario main This PR
25 failed requests per case (httptest server over HTTP/1.1, real RetryableClient): 503 and 429 exhausted, 500 and 401 not retried, 503 with a 64 KiB body, Thrift ExecuteStatement 503 25 bodies unclosed, 25 server connections open, 25 net/http log lines 0 unclosed, 1–2 idle pooled connections, 0 log lines
Live warehouse (HTTP/2) with an invalid token (403 on OpenSession), 10 pings 10 unclosed, 10 log lines 0 unclosed, 0 log lines, identical error text
  • New tests: TestErrorHandlerClosesResponse tests errorHandler directly. TestRetryableClientClosesFailedResponse and TestThriftExecuteClosesRetryExhaustedResponse run the real client stack, plus an empty-body case. TestRetryableClientRedirectLimitError covers the already-closed body net/http passes on a redirect-limit error. The live TestE2ERejectedRequestClosesResponse covers a real 403. All but the redirect test fail on main.
  • Passing: make test, make lint (0 issues), -race on internal/client, telemetry, internal/featureflags and the root package, the kernel-tagged tests (GOTOOLCHAIN=go1.27.3), live Thrift e2e, and live kernel e2e.
  • Two kernel-tagged failures predate this change and fail the same way on main: TestKernelCloudFetchDisabledPreservesUnsupportedServerError and the live TestKernelE2EErrorTelemetryCategory.
  • Performance: errorHandler runs only after a request has failed, so the success path is unchanged.

Also corrects a telemetry exporter comment. With SkipTransientRetries, a 429/503 arrives as a normal response with a nil error, not as a response plus an error.

Cross-driver state

Driver State Notes
JDBC Supported 429/503: the retry interceptor throws DatabricksRetryHandlerException (DatabricksHttpRetryHandler.java:98 @97dac8d), and Apache HttpClient 4.5.14 ProtocolExec closes the response (library behavior, checked in its sources). Other statuses close via try-with-resources (DatabricksHttpTTransport.java:128).
Python Supported finally: self._transport.close() runs drain_conn() and release_conn() (thrift_backend.py:533, thrift_http_client.py:154 @087b2bf). urllib3 drains the last response when retries run out.
Go Not supported → Supported (this PR) errorHandler closes the final response.
Node.js Not supported A non-retryable non-200 throws THTTPException (ThriftHttpConnection.ts:183-184 @7667e36). After retries, HttpRetryPolicy throws a RetryError holding the response (HttpRetryPolicy.ts:127,135). Neither path consumes or destroys the body. Separate follow-up.
Kernel Supported response.text() consumes the body on terminal failures (src/client/http.rs:976,1008 @8877579).

Adversarial review: converged in 1 round with no high findings. All 5 low findings were fixed: wording, cross-driver citations, a direct errorHandler test, race-safe log capture, and a stale telemetry comment.

This pull request and its description were written by Isaac.

vuanhphung and others added 3 commits November 20, 2026 04:47
retryablehttp passes the last response of a failed request to the
custom ErrorHandler and leaves closing it to the handler. errorHandler
returned it, still open, together with the error, and net/http discards
a response returned with an error, so nothing closed it. Every failed
request through RetryableClient leaked its response body and
connection until the 900s client timeout: retries exhausted on 429/503,
and also every non-200 that isn't retried (401, 500 on ExecuteStatement,
telemetry and feature-flag errors). net/http also logged "RoundTripper
returned a response & error; ignoring response" each time.

Close the body in errorHandler and return a nil response, as
retryablehttp's default handler does. The error is unchanged, and the
transport still drains a small body after Close, so the connection
stays reusable.

Signed-off-by: Vu Anh Phung <[email protected]>
Co-authored-by: Isaac <[email protected]>
Connect to the warehouse with an invalid token (403 on OpenSession)
through a body-counting transport, and assert every response body was
closed. Fails on the previous errorHandler with 3 of 3 left open.

Signed-off-by: Vu Anh Phung <[email protected]>
Co-authored-by: Isaac <[email protected]>
Address adversarial-review round 1:
- Test errorHandler directly: it returns no response, closes the body,
  and keeps the reason-phrase / Thrift error header and status text.
- Cover a redirect-limit failure, where net/http hands errorHandler an
  already closed body.
- Give the 429 case a body (so the connection check can fail) and keep
  an empty-body case; drop a nil-response assertion that couldn't fail.
- Make the std-log capture race-safe and print it on failure.
- Correct the telemetry exporter comment: with SkipTransientRetries a
  429/503 arrives as a normal response with a nil error.
- Changelog: the leak held an HTTP/1.1 connection or HTTP/2 stream
  until the request context ended or the client timeout fired.

Signed-off-by: Vu Anh Phung <[email protected]>
Co-authored-by: Isaac <[email protected]>
@vuanhphung
vuanhphung marked this pull request as ready for review November 20, 2026 06:06

@oss-reviewer-bot oss-reviewer-bot Bot 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.

🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).

Verdict: 1 Low

Looks good, with 1 low concern. errorHandler now closes the body and returns nil, which matches retryablehttp's default handler. Callers only see the error through StandardClient, and the error content is unchanged.

Comment thread driver_e2e_test.go Resolved
Add TestE2ERejectedRequestClosesResponse to the nightly Thrift E2E -run
allowlist so the live 403 check runs in CI.

Signed-off-by: Vu Anh Phung <[email protected]>
Co-authored-by: Isaac <[email protected]>

@oss-reviewer-bot oss-reviewer-bot Bot 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.

🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).

✅ No issues identified.

@vuanhphung

Copy link
Copy Markdown
Collaborator Author

Closing as superseded by #584. Its internal retry loop closes the final failed response itself, so the leak this PR fixes is fixed there too. This PR's live test (TestE2ERejectedRequestClosesResponse) and its nightly allowlist entry are moving to #584.

@vuanhphung vuanhphung closed this Nov 20, 2026
@vuanhphung
vuanhphung deleted the fix/retry-exhausted-body-leak branch November 23, 2026 22:45
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