Repository navigation
Close gzip Source/Sink wrappers in OkHttpHttpSender - #9081
Open
TimurRakhmatullin86 wants to merge 2 commits into
Open
TimurRakhmatullin86 wants to merge 2 commits into
TimurRakhmatullin86 wants to merge 2 commits into
Conversation
Pull request dashboard statusWaiting 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?
|
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 Report❌ Patch coverage is
❌ 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. 🚀 New features to boost your workflow:
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two related leaks of native compressor resources in
OkHttpHttpSender:Response path:
handleResponseleaksInflateron every gzip responsebodyis in the outer try-with-resources, sobody.source()gets closed. But theGzipSourcewrapper that owns a nativeInflateris only reached throughsourceand is never closed —source.close()is never called on any path. Every gzip-encoded response leaks oneInflater. The same path is taken on IO errors during reading (same leak).Request path:
RequestBodyImpl.writeToleaksDeflaterwhenwriteMessagethrowsIf
writeMessagethrows, control jumps overcompressedSink.close(). The underlyingGZIPOutputStreamholds a nativeDeflater; it's leaked on every failed send under compression.Fix
GzipSourcewrapper in afinallyblock. Only close it when we actually created it (decompress), becausebody.source()belongs to the outerResponseBodytry-with-resources — closing it twice from different owners is avoidable.compressedSinkin atry-with-resources so a throw fromwriteMessagestill releases it.Test
OkHttpHttpSenderTest#requestBody_compressedWriterThrows_closesCompressedSinkexercises the request path:Compressorwraps the output stream in a close-counting delegate.MessageWriterthrowsIOException("boom")fromwriteMessage.RequestBodyImplis constructed via reflection (it's a private inner class), andwriteTo(new Buffer())is called.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 wrappingGzipSourceat construction, which isn't reachable from the test code without production API changes.