Skip to content

Commit a146ee9

Browse files
committed
all_open_transactions should not include invalidated transactions
invalidated transactions aren't really open. This can happen when a [transaction is retried][1], so the transaction is invalidated and when executing the block again, we would get an exception. As there is no open transaction, the `after_all_transactions_commit` blocks should just execute as if there were no open transactions. [1]: https://github.com/rails/rails/blob/d3e3eefeef20bfbec69a8358a7c39d3a7f20574c/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb#L1032
1 parent d3e3eef commit a146ee9

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

activerecord/lib/active_record.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,10 @@ def self.all_open_transactions # :nodoc:
548548
open_transactions = []
549549
Base.connection_handler.each_connection_pool do |pool|
550550
if active_connection = pool.active_connection
551-
if active_connection.current_transaction.open? && active_connection.current_transaction.joinable?
552-
open_transactions << active_connection.current_transaction
551+
current_transaction = active_connection.current_transaction
552+
553+
if current_transaction.open? && current_transaction.joinable? && !current_transaction.state.invalidated?
554+
open_transactions << current_transaction
553555
end
554556
end
555557
end

activerecord/test/cases/transactions_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ def test_after_all_transactions_commit
6767
raise ActiveRecord::Rollback
6868
end
6969
assert_equal 0, called
70+
71+
called = 0
72+
Topic.transaction do |transaction|
73+
transaction.instance_variable_get(:@internal_transaction).invalidate!
74+
ActiveRecord.after_all_transactions_commit { called += 1 }
75+
assert_equal 1, called
76+
end
77+
assert_equal 1, called
7078
end
7179

7280
def test_after_current_transaction_commit_multidb_nested_transactions

0 commit comments

Comments
 (0)