Skip to content

Commit 34301ca

Browse files
authored
Merge pull request rails#54738 from byroot/configure-connection-timeout
AbstractAdapter#attempt_configure_connection: handle Timeout.timeout
2 parents 2f6cc65 + c9758b2 commit 34301ca

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,7 @@ def configure_connection
12211221

12221222
def attempt_configure_connection
12231223
configure_connection
1224-
rescue
1224+
rescue Exception # Need to handle things such as Timeout::ExitException
12251225
disconnect!
12261226
raise
12271227
end

activerecord/test/cases/adapter_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,29 @@ def teardown
894894
connection&.disconnect!
895895
end
896896

897+
test "disconnect and recover on #configure_connection timeout" do
898+
connection = ActiveRecord::Base.connection_pool.send(:new_connection)
899+
900+
slow = [5]
901+
connection.singleton_class.define_method(:configure_connection) do
902+
if duration = slow.pop
903+
sleep duration
904+
end
905+
super()
906+
end
907+
908+
assert_raises Timeout::Error do
909+
Timeout.timeout(0.2) do
910+
connection.exec_query("SELECT 1")
911+
end
912+
end
913+
914+
assert_equal [[1]], connection.exec_query("SELECT 1").rows
915+
assert_empty failures
916+
ensure
917+
connection&.disconnect!
918+
end
919+
897920
private
898921
def raw_transaction_open?(connection)
899922
case connection.adapter_name

0 commit comments

Comments
 (0)