Skip to content

Commit d21345b

Browse files
committed
Add guard for calling finalize on NullSession
rails#41372 Added `ActiveRecord::AsynchronousQueriesTracker::NullSession` which replaced a use of `nil` in `AsynchronousQueriesTracker`. This commit changes the `finalize_session` method to match that change from `nil` and properly handle cases where it is called with a `NullSession` present.
1 parent 89b52a8 commit d21345b

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

activerecord/lib/active_record/asynchronous_queries_tracker.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def start_session
5050
end
5151

5252
def finalize_session
53-
@current_session&.finalize
53+
@current_session.finalize if @current_session.respond_to?(:finalize)
5454
@current_session = NullSession
5555
end
5656
end

activerecord/test/cases/adapter_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,20 @@ def test_async_query_foreground_fallback
408408
ensure
409409
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
410410
end
411+
412+
def test_async_query_finalize_with_null_session
413+
assert_nothing_raised do
414+
@connection.select_all "SELECT * FROM posts", async: true
415+
end
416+
417+
@connection.transaction do
418+
assert_raises AsynchronousQueryInsideTransactionError do
419+
@connection.select_all "SELECT * FROM posts", async: true
420+
end
421+
end
422+
ensure
423+
ActiveRecord::Base.asynchronous_queries_tracker.finalize_session
424+
end
411425
end
412426

413427
class AsynchronousQueriesTest < ActiveRecord::TestCase

0 commit comments

Comments
 (0)