New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update ActionDispatch::Response
to support streaming bodies.
#47092
Conversation
I think this is an area that we want to refactor in Rails 8. There is a lot of cruft, and this mostly just does the bare minimum to get things working. |
Rack 3 introduces streaming bodies, which don't respond to `#each` and MUST respond to `#call`. Ensure that the methods are correctly delegated. `#to_ary` must also work correctly for enumerable bodies, and is used by middleware like `Rack::ETag` to buffer enumerable bodies correctly.
aad1d39
to
89df368
Compare
@rafaelfranca this is also another relatively straight forward one which aligns |
@@ -396,9 +396,9 @@ def test_only_set_charset_still_defaults_to_text_html | |||
test "[response.to_a].flatten does not recurse infinitely" do | |||
Timeout.timeout(1) do # use a timeout to prevent it stalling indefinitely | |||
status, headers, body = [@response.to_a].flatten |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This issue was actually resolved in Rack 3 and is no longer possible due to an updated SPEC (IIRC), so in theory we could just remove this entire test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rack/rack#1453 (comment) for reference.
Changes to support Rack 3 broke Streaming responses rails#47092 Streaming responses are failing with undefined method `to_ary' Live::Response should not respond to `to_ary` since they cannot be buffered safely.
The downside to this is that we cannot generate ETags for these types of responses, but are assuming that by using an enumerator they don't expect a buffered response to be cacheable. This means you cannot use Enumerator to generate streaming responses. Fixes rails#49588 See also: rails#47092 Co-authored-by: Samuel Williams <samuel.williams@oriontransfer.co.nz>
There is an inherit complexity with wrapping the Rack body inside Rails which can lead to bugs, like rails#49588. While rails#49616 fixed the bug, it's probably not a good long-term solution. If we go all the way back to 6a89850, we can see this was the original behavior. However, we were trying to solve a separate issue with streaming bodies during disconnect. The `live_stream_test#test_abort_with_full_buffer` test fails in this PR, but I wanted to raise that maybe this could be handled a different way. Also there are two failures in `response_test` which are questions to me: * `ResponseTest#test_[response.to_a].flatten_does_not_recurse_infinitely` * `ResponseTest#test_compatibility_with_Rack::ContentLength` The first seems it is actually resolved upstream, per this comment: rails#47092 (comment) The second means we broke `Rack::ContentLength` and there is a bit on this in rails#44953. FWIW: I'm not proposing this PR exactly, but looking for a path forward and would love some feedback. 🙇
Rack 3 introduces streaming bodies, which don't respond to
#each
and MUST respond to#call
. Ensure that the methods are correctly delegated.#to_ary
must also work correctly for enumerable bodies, and is used by middleware likeRack::ETag
to buffer enumerable bodies correctly.