Skip to content

Close gzip Source/Sink wrappers in OkHttpHttpSender - #9081

Open
TimurRakhmatullin86 wants to merge 2 commits into
open-telemetry:mainfrom
TimurRakhmatullin86:fix/okhttp-sender-gzip-leak
Open

TimurRakhmatullin86 wants to merge 2 commits into
open-telemetry:mainfrom
TimurRakhmatullin86:fix/okhttp-sender-gzip-leak

Conversation

@TimurRakhmatullin86

Copy link
Copy Markdown
Contributor

Summary

Two related leaks of native compressor resources in OkHttpHttpSender:

Response path: handleResponse leaks Inflater on every gzip response

Source source = decompress ? new GzipSource(body.source()) : body.source();
while (buffer.size() <= maxResponseBodySize) {
  long n = source.read(buffer, readUpTo - buffer.size());
  // ...
}

body is in the outer try-with-resources, so body.source() gets closed. But the GzipSource wrapper that owns a native Inflater is only reached through source and is never closed — source.close() is never called on any path. Every gzip-encoded response leaks one Inflater. The same path is taken on IO errors during reading (same leak).

Request path: RequestBodyImpl.writeTo leaks Deflater when writeMessage throws

BufferedSink compressedSink =
    Okio.buffer(Okio.sink(compressor.compress(bufferedSink.outputStream())));
requestBodyWriter.writeMessage(compressedSink.outputStream());
compressedSink.close();

If writeMessage throws, control jumps over compressedSink.close(). The underlying GZIPOutputStream holds a native Deflater; it's leaked on every failed send under compression.

Fix

  • Response: close the GzipSource wrapper in a finally block. Only close it when we actually created it (decompress), because body.source() belongs to the outer ResponseBody try-with-resources — closing it twice from different owners is avoidable.
  • Request: wrap the compressedSink in a try-with-resources so a throw from writeMessage still releases it.

Test

OkHttpHttpSenderTest#requestBody_compressedWriterThrows_closesCompressedSink exercises the request path:

  • A Compressor wraps the output stream in a close-counting delegate.
  • A MessageWriter throws IOException("boom") from writeMessage.
  • RequestBodyImpl is constructed via reflection (it's a private inner class), and writeTo(new Buffer()) is called.
  • Assertion: close count == 1. Without the fix the count is 0 (the throw skipped compressedSink.close()).

The response-path change is covered by the existing gzip response tests (behavioral equivalence) plus the inspection of the finally-block invariant — reviewers should verify that reasoning; a close-count test there would need wrapping GzipSource at construction, which isn't reachable from the test code without production API changes.

@TimurRakhmatullin86
TimurRakhmatullin86 requested a review from a team as a code owner November 30, 2026 16:25
@opentelemetry-pr-dashboard

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

Copy link
Copy Markdown

Pull request dashboard status

Waiting on the author · refreshed 2026-11-30 17:09 UTC

Investigate required status check failures. Note: codecov/patch is also failing but is not a required check.

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 reviewers? Comment /dashboard route:reviewers to route it to them.
  • Anything wrong — including the routing? Report it with what you expected; it helps us improve the dashboard.

Two related leaks of native compressor resources:

* handleResponse wrapped the response body in a GzipSource but only
  closed the underlying body stream; the wrapper's native Inflater was
  never released. Every gzip response leaked one Inflater, and the
  same leak fires on IO errors while reading (same path).
* RequestBodyImpl.writeTo buffered the compressor's output stream and
  called compressedSink.close() at the end, but a throw from
  requestBodyWriter.writeMessage skipped the close() call, leaking the
  underlying GZIPOutputStream's native Deflater on every failed send
  under compression.

Fix: close the GzipSource in a finally block (it owns the wrapped
stream, which belongs to the outer ResponseBody try-with-resources,
so only close the wrapper when we actually created it); and move the
buffered sink into a try-with-resources so writeMessage throwing
still releases it.

Added OkHttpHttpSenderTest#requestBody_compressedWriterThrows_closesCompressedSink:
a tracking Compressor returns an OutputStream that counts close(),
a MessageWriter throws from writeMessage, writeTo is called via
reflection on the private RequestBodyImpl, and the test asserts that
close() fires exactly once (was zero before the fix).
@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 70.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.51%. Comparing base (2aec7ba) to head (20ca5d9).

Files with missing lines Patch % Lines
...orter/sender/okhttp/internal/OkHttpHttpSender.java 70.00% 2 Missing and 1 partial ⚠️

❌ Your patch check has failed because the patch coverage (70.00%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #9081      +/-   ##
============================================
- Coverage     90.52%   90.51%   -0.02%     
  Complexity    11243    11243              
============================================
  Files          1039     1039              
  Lines         30678    30681       +3     
  Branches       4015     4016       +1     
============================================
- Hits          27772    27770       -2     
- Misses         1928     1930       +2     
- Partials        978      981       +3     

☔ 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