Skip to content

Prevent false leaked-connection reports during reaper maintenance#57370

Open
curi wants to merge 1 commit into
rails:mainfrom
curi:fix-ar-leak-check-reaper-race
Open

Prevent false leaked-connection reports during reaper maintenance#57370
curi wants to merge 1 commit into
rails:mainfrom
curi:fix-ar-leak-check-reaper-race

Conversation

@curi
Copy link
Copy Markdown

@curi curi commented May 15, 2026

Motivation / Background

ActiveRecord::TestCase#check_connection_leaks scans each pool during teardown to catch connections left owned by background threads.

ConnectionPool::Reaper can legitimately hold a connection under reaper_lock while it performs maintenance. The leak check currently inspects pool.connections without taking that lock, so teardown can race the reaper, report a leaked connection owned by AR Pool Reaper, and kill the maintenance thread even though no test leaked anything.

Detail

Synchronize the leak scan with pool.reaper_lock so teardown waits for any in-flight maintenance to finish before classifying in-use connections as leaks.

Add a regression test that reproduces the false positive by holding a connection for maintenance from a thread named AR Pool Reaper. The new test fails against origin/main and passes with this patch.

Additional information

This surfaced as an unrelated CI failure while validating #57367, where the reported leaked owner was the pool reaper thread.

Tests

  • cd activerecord && ARCONN=postgresql bundle exec ruby -Itest test/cases/test_case_test.rb -v
  • cd activerecord && ARCONN=postgresql bundle exec ruby -Itest test/cases/reaper_test.rb
  • cd activerecord && ARCONN=postgresql bundle exec ruby -Itest test/cases/associations/has_and_belongs_to_many_associations_test.rb
  • bundle exec rubocop activerecord/test/cases/test_case.rb activerecord/test/cases/test_case_test.rb

Copilot AI review requested due to automatic review settings May 15, 2026 00:03
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR prevents ActiveRecord::TestCase#check_connection_leaks from falsely reporting leaked connections when ConnectionPool::Reaper is legitimately holding a connection for maintenance by synchronizing the leak scan with pool.reaper_lock.

Changes:

  • Wrap the teardown leak scan (pool.reap + pool.connections inspection) in pool.reaper_lock to avoid racing ongoing reaper maintenance.
  • Add a regression test that simulates a reaper-maintenance hold from a thread named AR Pool Reaper and asserts check_connection_leaks does not raise.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
activerecord/test/cases/test_case.rb Synchronizes leak scanning with reaper_lock to avoid misclassifying maintenance-held connections as leaks.
activerecord/test/cases/test_case_test.rb Adds regression coverage for the reaper-maintenance race that previously caused false leak reports.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread activerecord/test/cases/test_case_test.rb Outdated
Comment thread activerecord/test/cases/test_case_test.rb
@curi curi force-pushed the fix-ar-leak-check-reaper-race branch from 10cfbe2 to 74686d9 Compare May 15, 2026 00:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants