Skip to content

Commit 1df8ec0

Browse files
authored
Merge pull request rails#55722 from kozy4324/fix-lease-sticky-flag-timing
Fix lease_connection to preserve pool state when checkout callbacks fail
2 parents 4fcc1dd + 8626c75 commit 1df8ec0

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,9 @@ def activated?
344344
# held in a cache keyed by a thread.
345345
def lease_connection
346346
lease = connection_lease
347-
lease.sticky = true
348347
lease.connection ||= checkout
348+
lease.sticky = true
349+
lease.connection
349350
end
350351

351352
def permanent_lease? # :nodoc:

activerecord/test/cases/connection_pool_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,25 @@ def test_inspect_does_not_show_secrets
14771477
pool&.disconnect!
14781478
end
14791479

1480+
def test_checkout_callback_error_does_not_affect_permanent_lease_and_active_connection_state
1481+
checkout_error = StandardError.new("error during checkout")
1482+
proc_to_raise = -> { raise checkout_error }
1483+
ActiveRecord::ConnectionAdapters::AbstractAdapter.set_callback(:checkout, :after, proc_to_raise)
1484+
1485+
assert_predicate @pool, :permanent_lease?
1486+
assert_not_predicate @pool, :active_connection?
1487+
1488+
error = assert_raises StandardError do
1489+
@pool.lease_connection
1490+
end
1491+
assert_same checkout_error, error
1492+
1493+
assert_predicate @pool, :permanent_lease?
1494+
assert_not_predicate @pool, :active_connection?
1495+
ensure
1496+
ActiveRecord::ConnectionAdapters::AbstractAdapter.skip_callback(:checkout, :after, proc_to_raise)
1497+
end
1498+
14801499
private
14811500
def active_connections(pool)
14821501
pool.connections.find_all(&:in_use?)

0 commit comments

Comments
 (0)