Skip to content

Forward discard! to the replica connection - #178

Merged
lloydwatkin merged 2 commits into
mainfrom
fix-issue-176-item-2
Sep 4, 2026
Merged

lloydwatkin merged 2 commits into
mainfrom
fix-issue-176-item-2

Conversation

@lloydwatkin

Copy link
Copy Markdown
Member

Fixes finding 2 of #176.

ActiveRecord registers ActiveSupport::ForkTracker.after_fork { PoolConfig.discard_pools! }, which calls discard! on every adapter in a forked child so the child never closes a socket it shares with the parent. Janus forwarded connect!, reconnect!, disconnect! and clear_cache! to the replica but not discard!, so in a forked child (Puma cluster, Sidekiq swarm, Spring, parallel test runners) the replica adapter kept a live Mysql2::Client / Trilogy object. Garbage collecting it in the child sends COM_QUIT down the socket the parent is still using.

def discard!(...)
  replica_connection.discard!(...)
  super
end

Tests

New shared example an adapter forwarding lifecycle calls, included from both the mysql2 and trilogy adapter specs. Verified red before the fix (received: 0 times) and green after. RuboCop clean.

Note on the audit text

The issue says "throw_away! calls discard! internally so it is covered by the same override". That is not true in AR 8.1.3.1 — throw_away! is pool.remove self; disconnect!. disconnect! is already forwarded to the replica, so that path is fine, but it does not go through discard!. No extra override needed.

The remaining findings in #176 are not addressed here.

🤖 Generated with Claude Code

ActiveRecord's ForkTracker calls discard! on every adapter in a forked
child (PoolConfig.discard_pools!) so the child never closes a socket
shared with the parent. Janus forwarded connect!, reconnect!,
disconnect! and clear_cache! to the replica but not discard!, so a
forked child kept a live replica client. Garbage collecting it in the
child sent COM_QUIT down the socket the parent was still using.

Refs #176 (finding 2).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@lloydwatkin lloydwatkin changed the title Forward discard! to the replica connection Forward discard! to the replica connection Sep 4, 2026
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@lloydwatkin
lloydwatkin merged commit 3e0549c into main Sep 4, 2026
12 checks passed
@lloydwatkin
lloydwatkin deleted the fix-issue-176-item-2 branch September 4, 2026 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant