Repository navigation
Close GZIPInputStream wrapper in JdkHttpSender#toHttpResponse - #9080
TimurRakhmatullin86 wants to merge 1 commit into
Conversation
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.
Pull request dashboard statusWaiting on maintainers · refreshed 2026-12-04 18:00 UTC Merge when ready. Status above doesn't look right?
|
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
breedx-splk
left a comment
There was a problem hiding this comment.
Thanks. I think this is mostly a reasonable change, but I'm not convinced how necessary it is. The agents say this about this:
On OpenJDK, the existing code delays cleanup of one native Inflater per gzip response. Calling it a permanent leak overstates the issue.
The old code closes the raw HTTP body and leaves the GZIPInputStream unclosed. Reading to EOF leaves the inflater allocated. Once the wrapper and its inflater become unreachable, OpenJDK’s Cleaner can free the native zlib state on the JVM’s schedule. During a burst of gzip responses, that native memory can accumulate before cleanup runs. PR diff, gzip EOF handling, OpenJDK 11 cleanup, OpenJDK 25 cleanup
So I'm slightly worried that this unnecessarily complicates things without fully understanding the performance implications. My instinct on matters like this is to defer to the decisions of the underlying runtime implementers....
Curious what the maintainers think.
|
Thanks for pushing back. You and the agents are right that "permanent leak" overstates it — the inflater becomes unreachable with the wrapper, and OpenJDK's The change still buys three things on this burst-heavy exporter path:
The test asserts the wrapper's |
|
This seems like a pretty harmless change to me; I'm not opposed to it if it fixes a potential short-term leak (although when GC runs, it may not matter at all really). |
|
I analyzed #9081, which is similar. It also prevents temporary leakage, not permanent leakage. Good to have, and I think we should proceed, but not mission critical (which explains why we haven't heard about it more loudly before this!) |
Problem
JdkHttpSender#toHttpResponsewraps the response body in aGZIPInputStreamfor gzip-encoded responses, drains it into aByteArrayOutputStream, and never closes the wrapper. Only the underlyingrawIsis declared in the try-with-resources (JdkHttpSender.java#L410-L412):Closing
rawIsdoes not close the wrapper, soInflater#end()is never called for theInflaterthat theGZIPInputStreamallocates. This applies to every exit from the block: normal completion, anIOExceptionwhile reading, and the early exit when the read limit is reached.Why it matters
This is missing explicit cleanup, not a permanent leak. Once the wrapper is unreachable, its
Inflateris unreachable too, and OpenJDK releases the native zlib state through theInflater'sCleaner(finalize()on JDK 8). Nothing is lost forever.What the explicit
close()changes:Inflaterand the cleaner runs. With the fix it is released on the response thread as soon as the body has been read.GZIPInputStreamisAutoCloseableand was the only resource created in this block that was not managed by the try-with-resources.Low severity, consistent with the review discussion: a temporary retention that the runtime eventually cleans up, made deterministic by this change.
Fix
Declare the wrapper as a second resource in the existing try-with-resources:
Close semantics:
GZIPInputStream#close()ends its ownInflaterand then closes the stream it wraps, sorawIsis closed by the wrapper and then a second time by the outer resource. The second close is a no-op.decompressis false both declarations reference the same stream, which is likewise closed twice.GZIPInputStreamconstructor throws (malformed gzip header),rawIsis still closed by the first resource. Same behavior as before.No change to what is read, to the size limit handling, or to the returned response.
Tests
JdkHttpSenderTest#sendInternal_gzipResponse_closesGzipWrappersends a valid gzip payload through a mockedjava.net.http.HttpResponsewhose body stream countsclose()calls.close()forwards to the body stream and the outer resource closes it again, count >= 2.The test fails with count == 1 against the previous version of the file.