From e8835a14dd6cceb9ea7dc315973c169f95f0e2c4 Mon Sep 17 00:00:00 2001 From: Malcolm Locke Date: Fri, 12 Jul 2024 14:52:49 +1200 Subject: [PATCH] Use lock syncronize on transaction rollback (#73) Avoids race conditions between counting the number of open transactions and calling rollback. Co-authored-by: Malcolm Locke --- lib/database_cleaner/active_record/transaction.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/database_cleaner/active_record/transaction.rb b/lib/database_cleaner/active_record/transaction.rb index 5e3d7eb..85e7c3d 100644 --- a/lib/database_cleaner/active_record/transaction.rb +++ b/lib/database_cleaner/active_record/transaction.rb @@ -19,8 +19,10 @@ def start def clean connection_class.connection_pool.connections.each do |connection| - next unless connection.open_transactions > 0 - connection.rollback_transaction + connection.lock.synchronize do + next unless connection.open_transactions > 0 + connection.rollback_transaction + end end end end