Skip to content

Close GZIPInputStream wrapper in JdkHttpSender#toHttpResponse - #9080

Open
TimurRakhmatullin86 wants to merge 1 commit into
open-telemetry:mainfrom
TimurRakhmatullin86:fix/jdk-http-sender-gzip-leak
Open

TimurRakhmatullin86 wants to merge 1 commit into
open-telemetry:mainfrom
TimurRakhmatullin86:fix/jdk-http-sender-gzip-leak

Conversation

@TimurRakhmatullin86

Copy link
Copy Markdown
Contributor

Summary

JdkHttpSender#toHttpResponse wraps the response body in a GZIPInputStream for gzip-encoded responses but only includes the underlying rawIs in the try-with-resources block. Closing rawIs does not close the wrapper, so the gzip stream's native Inflater is never released — one Inflater leaks on every gzip response. The same leak fires on IO errors while reading the body (same code path) and on ResponseBodyTooLargeException (thrown after the try block closes rawIs but after the wrapper was created).

Before:

try (InputStream rawIs = response.body()) {
  InputStream is = decompress ? new GZIPInputStream(rawIs) : rawIs;
  // ...read is...
}

After:

try (InputStream rawIs = response.body();
    InputStream is = decompress ? new GZIPInputStream(rawIs) : rawIs) {
  // ...read is...
}

Closing the gzip stream also closes rawIs, so the second declaration is harmless when decompress is false (the two resources point at the same stream).

Test

JdkHttpSenderTest#sendInternal_gzipResponse_closesGzipWrapper: feeds a valid gzip payload through a mocked HTTP response whose body stream tracks close() calls.

  • Without the fix: the outer try-with-resources closes rawIs once; the gzip wrapper is never closed — count == 1.
  • With the fix: the gzip wrapper closes rawIs as part of its own close(), and the outer try-with-resources closes it a second time — count >= 2.

The assertion isGreaterThanOrEqualTo(2) captures that the gzip wrapper's close() was called. I verified the test fails with count == 1 against the previous version of the file.

@TimurRakhmatullin86
TimurRakhmatullin86 requested a review from a team as a code owner November 30, 2026 16:20
The gzip decompression path wraps the response body in a GZIPInputStream
but keeps only the underlying rawIs in the try-with-resources. Closing
rawIs does not close the wrapper, so its native Inflater is never
released. Every gzip response leaks one Inflater — and the same leak
fires on IO errors while reading, which already use this path.

Include the wrapper in the try-with-resources. Closing the gzip stream
also closes rawIs, so the second declaration in the resource list is
harmless when decompression is off.

Added JdkHttpSenderTest#sendInternal_gzipResponse_closesGzipWrapper:
the mocked body stream tracks close() calls. Without the fix the count
is 1 (outer try-with-resources only). With the fix the gzip wrapper
forwards its own close to the body, so the count is >= 2.
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on reviewers · refreshed 2026-11-30 16:52 UTC

Review the latest changes.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Should this be with the author? Comment /dashboard route:author to route it back.
  • Anything wrong — including the routing? Report it with what you expected; it helps us improve the dashboard.

@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.51%. Comparing base (2aec7ba) to head (a17a15e).

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #9080      +/-   ##
============================================
- Coverage     90.52%   90.51%   -0.01%     
  Complexity    11243    11243              
============================================
  Files          1039     1039              
  Lines         30678    30678              
  Branches       4015     4015              
============================================
- Hits          27772    27769       -3     
- Misses         1928     1929       +1     
- Partials        978      980       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
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