Skip to content
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

Merged
merged 1 commit into from Feb 15, 2023

Conversation

ioquatix
Copy link
Contributor

@ioquatix ioquatix commented Jan 21, 2023

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.

@rails-bot rails-bot bot added the actionpack label Jan 21, 2023
@ioquatix
Copy link
Contributor Author

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.
@ioquatix
Copy link
Contributor Author

@rafaelfranca this is also another relatively straight forward one which aligns ActionDispatch::Response with the Rack 3 spec.

@@ -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
Copy link
Contributor Author

@ioquatix ioquatix Jan 25, 2023

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.

Copy link
Contributor Author

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.

@tenderlove tenderlove merged commit 218ec10 into rails:main Feb 15, 2023
@ioquatix ioquatix deleted the rack-3-streaming-body branch February 15, 2023 01:05
JoeDupuis added a commit to JoeDupuis/rails that referenced this pull request Jun 11, 2023
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.
zzak added a commit to zzak/rails that referenced this pull request Oct 13, 2023
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>
zzak added a commit to zzak/rails that referenced this pull request Oct 13, 2023
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. 🙇
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants