Skip to content

Commit 5b2ac4a

Browse files
Patch ActionController:Live in the test environment to prevent clearing thread locals as the new_controller_thread actually yields the current thread
Co-authored-by: Ates Goral <[email protected]>
1 parent 0196c62 commit 5b2ac4a

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

actionpack/lib/action_controller/metal/live.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,8 @@ def process(name)
301301
error = e
302302
end
303303
ensure
304-
# Ensure we clean up any thread locals we copied so that the thread can reused.
305304
ActiveSupport::IsolatedExecutionState.clear
306-
locals.each { |k, _| t2[k] = nil }
305+
clean_up_thread_locals(locals, t2)
307306

308307
@_response.commit!
309308
end
@@ -377,6 +376,11 @@ def new_controller_thread # :nodoc:
377376
end
378377
end
379378

379+
# Ensure we clean up any thread locals we copied so that the thread can reused.
380+
def clean_up_thread_locals(locals, thread) # :nodoc:
381+
locals.each { |k, _| thread[k] = nil }
382+
end
383+
380384
def self.live_thread_pool_executor
381385
@live_thread_pool_executor ||= Concurrent::CachedThreadPool.new(name: "action_controller.live")
382386
end

actionpack/lib/action_controller/test_case.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ def new_controller_thread # :nodoc:
2929
yield
3030
end
3131

32+
# Because of the above, we need to prevent the clearing of thread locals, since
33+
# no new thread is actually spawned in the test environment.
34+
alias_method :original_clean_up_thread_locals, :clean_up_thread_locals
35+
36+
silence_redefinition_of_method :clean_up_thread_locals
37+
def clean_up_thread_locals(*args) # :nodoc:
38+
end
39+
3240
# Avoid a deadlock from the queue filling up
3341
Buffer.queue_size = nil
3442
end

actionpack/test/controller/live_stream_test.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,10 @@ def setup
360360
def @controller.new_controller_thread(&block)
361361
original_new_controller_thread(&block)
362362
end
363+
364+
def @controller.clean_up_thread_locals(*args)
365+
original_clean_up_thread_locals(*args)
366+
end
363367
end
364368

365369
def test_set_cookie
@@ -674,7 +678,7 @@ def greet
674678
tests TestController
675679

676680
def test_thread_locals_do_not_get_reset_in_test_environment
677-
Thread.current[:setting] = "aaron"
681+
Thread.current[:setting] = "aaron"
678682

679683
get :greet
680684

0 commit comments

Comments
 (0)