Skip to content

Commit

Permalink
Undefine #to_ary in ActionController::Live::Buffer
Browse files Browse the repository at this point in the history
ActionDispatch::Response delegates #to_ary to the internal ActionDispatch::Response::Buffer,
defining #to_ary is an indicator that the response body can be buffered and/or cached by
Rack middlewares, this is not the case for Live responses so we undefine it for this Buffer subclass.

Puma raises an exception trying to call #to_ary in Live::Buffer
expecting it to return an array if defined:

https://github.com/puma/puma/blob/188f5da1920ff99a8689b3e9b46f2f26b7c62d66/lib/puma/request.rb#L183-L186
  • Loading branch information
guilleiguaran committed Jun 11, 2023
1 parent f642505 commit 0174283
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions actionpack/lib/action_controller/metal/live.rb
Expand Up @@ -167,6 +167,11 @@ def initialize(response)
@ignore_disconnect = false
end

# ActionDispatch::Response delegates #to_ary to the internal ActionDispatch::Response::Buffer,
# defining #to_ary is an indicator that the response body can be buffered and/or cached by
# Rack middlewares, this is not the case for Live responses so we undefine it for this Buffer subclass.
undef_method :to_ary

def write(string)
unless @response.committed?
@response.headers["Cache-Control"] ||= "no-cache"
Expand Down
10 changes: 10 additions & 0 deletions actionpack/test/controller/live_stream_test.rb
Expand Up @@ -319,6 +319,11 @@ def ignore_client_disconnect
logger.info "Work complete"
latch.count_down
end

def buffer_do_not_respond_to_to_ary
response.stream.write "response.stream.respond_to? = #{response.stream.respond_to?(:to_ary)}"
response.stream.close
end
end

tests TestController
Expand Down Expand Up @@ -596,6 +601,11 @@ def test_stale_with_etag
get :with_stale
assert_equal 304, response.status.to_i
end

def test_response_buffer_do_not_respond_to_to_ary
get :buffer_do_not_respond_to_to_ary
assert_equal "response.stream.respond_to? = false", response.body
end
end

class BufferTest < ActionController::TestCase
Expand Down

0 comments on commit 0174283

Please sign in to comment.