Skip to content

Commit 0174283

Browse files
committed
Undefine #to_ary in ActionController::Live::Buffer
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
1 parent f642505 commit 0174283

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

actionpack/lib/action_controller/metal/live.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ def initialize(response)
167167
@ignore_disconnect = false
168168
end
169169

170+
# ActionDispatch::Response delegates #to_ary to the internal ActionDispatch::Response::Buffer,
171+
# defining #to_ary is an indicator that the response body can be buffered and/or cached by
172+
# Rack middlewares, this is not the case for Live responses so we undefine it for this Buffer subclass.
173+
undef_method :to_ary
174+
170175
def write(string)
171176
unless @response.committed?
172177
@response.headers["Cache-Control"] ||= "no-cache"

actionpack/test/controller/live_stream_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,11 @@ def ignore_client_disconnect
319319
logger.info "Work complete"
320320
latch.count_down
321321
end
322+
323+
def buffer_do_not_respond_to_to_ary
324+
response.stream.write "response.stream.respond_to? = #{response.stream.respond_to?(:to_ary)}"
325+
response.stream.close
326+
end
322327
end
323328

324329
tests TestController
@@ -596,6 +601,11 @@ def test_stale_with_etag
596601
get :with_stale
597602
assert_equal 304, response.status.to_i
598603
end
604+
605+
def test_response_buffer_do_not_respond_to_to_ary
606+
get :buffer_do_not_respond_to_to_ary
607+
assert_equal "response.stream.respond_to? = false", response.body
608+
end
599609
end
600610

601611
class BufferTest < ActionController::TestCase

0 commit comments

Comments
 (0)