Conversation
When wrapping a body into a stream for compression, BodyIntoStream polls the underlying body to completion (None) to read data frames into the encoder. WrapBody then invokes poll_frame to collect any remaining trailers or non-data frames. Previously, poll_frame unconditionally re-polled the inner body if no trailers were buffered. When the underlying body is backed by a non-fused stream (such as futures::stream::unfold), this re-poll panics after EOF. Track when the inner body has yielded EOF (None), and return Ready(None) directly rather than re-polling the inner body. Fixes tower-rs#732.
cratelyn
reviewed
Sep 9, 2026
There was a problem hiding this comment.
if we want to avoid polling the underlying body after it yields Ready(None), i think that it would be more prudent to use http_body_util::combinators::Fuse<B>.
we can wrap B in that combinator, rather than reimplementing the logic for fusing a body here.
Comment on lines
289
to
+291
| yielded_all_data: false, | ||
| non_data_frame: None, | ||
| body_eof: false, |
There was a problem hiding this comment.
it smells weird to me that this introduces a body_eof flag when there is already a yielded_all_data flag here. how do these differ?
De-ASI-INTERFACE
approved these changes
Sep 15, 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.
Motivation
Closes #732
When compressing a streaming response body backed by a non-fused stream (e.g.
Body::from_stream(futures::stream::unfold(...))),Compressionpanics at the end of the stream with:Unfold must not be polled after it returned Poll::Ready(None).In
BodyIntoStream,poll_nextdrives the underlying body to read all data frames into the encoder. Oncethis.body.poll_frame(cx)returnsNone,yielded_all_datais set. After the encoder finishes writing all compressed output,WrapBody::poll_framecallsBodyIntoStream::poll_frameto forward any remaining frames (such as trailers).If no trailers were buffered,
BodyIntoStream::poll_frameunconditionally fell through tothis.body.poll_frame(cx). For bodies backed by non-fused streams, re-polling an already-exhausted body violates the stream contract and panics.Solution
Track whether the inner body has already returned
Nonevia a boolean field (body_eof). Whenbody_eofis true,BodyIntoStream::poll_frameimmediately returnsPoll::Ready(None)instead of re-polling the inner body.