Skip to content

Commit 3a98e7d

Browse files
committed
Merge pull request rails#41609
2 parents cb52350 + f6fb700 commit 3a98e7d

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

actionpack/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Fix ActionController::Live controller test deadlocks by removing the body buffer size limit for tests.
2+
3+
*Dylan Thacker-Smith*
4+
15
* New `ActionController::ConditionalGet#no_store` method to set HTTP cache control `no-store` directive.
26

37
*Tadas Sasnauskas*

actionpack/lib/action_controller/metal/live.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ class ClientDisconnected < RuntimeError
127127
class Buffer < ActionDispatch::Response::Buffer #:nodoc:
128128
include MonitorMixin
129129

130+
class << self
131+
attr_accessor :queue_size
132+
end
133+
@queue_size = 10
134+
130135
# Ignore that the client has disconnected.
131136
#
132137
# If this value is `true`, calling `write` after the client
@@ -136,7 +141,7 @@ class Buffer < ActionDispatch::Response::Buffer #:nodoc:
136141
attr_accessor :ignore_disconnect
137142

138143
def initialize(response)
139-
super(response, SizedQueue.new(10))
144+
super(response, build_queue(self.class.queue_size))
140145
@error_callback = lambda { true }
141146
@cv = new_cond
142147
@aborted = false
@@ -219,6 +224,10 @@ def each_chunk(&block)
219224
yield str
220225
end
221226
end
227+
228+
def build_queue(queue_size)
229+
queue_size ? SizedQueue.new(queue_size) : Queue.new
230+
end
222231
end
223232

224233
class Response < ActionDispatch::Response #:nodoc: all

actionpack/lib/action_controller/test_case.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ module Live
2424
def new_controller_thread # :nodoc:
2525
yield
2626
end
27+
28+
# Avoid a deadlock from the queue filling up
29+
Buffer.queue_size = nil
2730
end
2831

2932
# ActionController::TestCase will be deprecated and moved to a gem in the future.

actionpack/test/controller/live_stream_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,13 @@ def overfill_buffer_and_die
264264
end
265265
end
266266

267+
def overfill_default_buffer
268+
("a".."z").each do |char|
269+
response.stream.write(char)
270+
end
271+
response.stream.close
272+
end
273+
267274
def ignore_client_disconnect
268275
response.stream.ignore_disconnect = true
269276

@@ -368,7 +375,15 @@ def test_async_stream
368375
assert t.join(3), "timeout expired before the thread terminated"
369376
end
370377

378+
def test_infinite_test_buffer
379+
get :overfill_default_buffer
380+
assert_equal ("a".."z").to_a.join, response.stream.body
381+
end
382+
371383
def test_abort_with_full_buffer
384+
old_queue_size = ActionController::Live::Buffer.queue_size
385+
ActionController::Live::Buffer.queue_size = 10
386+
372387
@controller.latch = Concurrent::CountDownLatch.new
373388
@controller.error_latch = Concurrent::CountDownLatch.new
374389

@@ -389,6 +404,8 @@ def test_abort_with_full_buffer
389404
@controller.error_latch.wait
390405
assert_match "Error while streaming", output.rewind && output.read
391406
end
407+
ensure
408+
ActionController::Live::Buffer.queue_size = old_queue_size
392409
end
393410

394411
def test_ignore_client_disconnect

0 commit comments

Comments
 (0)