fix: harden bounded response-body capture against zero-read stall and double-close#80
Merged
Merged
Conversation
…ouble-close Two defects in the bounded response-body capture path: - The bounded drain loop subtracted the read count from its remaining budget and handled only EOF (-1) and positive reads. A delegate Source that returns 0 for a positive byteCount — a Source.read contract violation — made the subtraction a no-op, so the loop spun forever and hung whatever thread triggered the capture. The loop now fails fast with an IOException on a 0-read, matching TeeSink.writeAll; the existing failure path caches it and source() re-throws it deterministically while snapshot() still returns the partial bytes. A 0-read is not treated as EOF, which would silently truncate the body. - On the over-cap path the wrapper retains the delegate's source as the live tail and hands it out inside a one-shot prefix+tail stream. That stream's close() closed the tail directly while the wrapper's own close() independently closed the delegate, so for a delegate whose source() returns the same instance and whose close() closes it, the underlying source was closed twice. Both paths now funnel through a single closeDelegateOnce() guard under the existing lock, so whichever closes first wins and the other is a no-op. The peek-based prefix view is still closed directly. This was masked by the self-guarded close in the shipped Okio adapter but was latent for a bring-your-own IoProvider. Also tightens the over-cap source() to assert the live-tail invariant with checkNotNull rather than silently falling back to a peek view, since the tail is always present once control reaches that point.
This was referenced Jun 16, 2026
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
Fixes two related defects in
LoggableResponseBody's bounded body-capture path.1. Bounded drain can spin forever on a zero-read source
drainAndCache()only handled two outcomes from the capturedSource: end-of-stream (-1) and a positive read. A source that returns0for a positivebyteCount— a violation of theSource.readcontract — leftremainingunchanged, so the loop made no progress and spun forever, hanging the thread that triggered the capture (a caller thread under synchronous logging, or an async completion thread).The two sibling pump loops written alongside this one (
TeeSink.writeAll,RequestBody.copyExactly) already reject a zero-read; this loop was the one that didn't. It now throws anIOException, which the existing drain-error handling caches andsource()re-throws — consistent with the documented failure semantics, andsnapshot()still returns the bytes captured so far. A zero-read is deliberately not treated as EOF, which would silently truncate the body.2. Over-cap capture can close the underlying source twice
On the over-cap path the wrapper keeps the delegate's source as a live tail and hands it out inside a
PrefixThenTailSource. That inner source closed the tail directly, while the wrapper's ownclose()independently closed the delegate — and thedelegateClosedguard was never set on the inner-source path. For a delegate whosesource()returns the same instance and whoseclose()closes it, the underlying source was closed twice. It is masked today because the bundled Okio adapter'sclose()is self-guarded, but it is a latent hazard for a customIoProvider(and the class's own comments already warn that some sockets/streams throw on double-close).Both close paths now funnel through a single
closeDelegateOnce()guard, so whichever fires first wins and the second is a no-op.Tests
0for a positivebyteCount: the drain now fails fast instead of timing out (previously hung).source()returns the same instance and whoseclose()closes it: asserts the underlying source is closed exactly once, in both close orders (previously closed twice).Internal change only — no public API change.
Closes #14
Closes #15