Skip to content

Catch ActiveRecord::ConnectionFailed in the failsafe#308

Open
ajaynomics wants to merge 1 commit into
rails:mainfrom
ajaynomics:fix/307-connection-failed-failsafe
Open

Catch ActiveRecord::ConnectionFailed in the failsafe#308
ajaynomics wants to merge 1 commit into
rails:mainfrom
ajaynomics:fix/307-connection-failed-failsafe

Conversation

@ajaynomics

Copy link
Copy Markdown

What

Adds ActiveRecord::ConnectionFailed to Failsafe::TRANSIENT_ACTIVE_RECORD_ERRORS so that a terminated cache database connection degrades to a cache miss instead of propagating to the caller.

Fixes #307.

Why

ActiveRecord::ConnectionFailed (added in Rails 7.1) is raised when a connection is terminated mid-query. Critically, it is a subclass of ActiveRecord::StatementInvalid, not of ActiveRecord::ConnectionNotEstablished — so even though the failsafe already rescues connection-related errors, this one slipped straight past the rescue list and surfaced to application code. Adding it alongside the existing transient connection errors restores the intended "cache is best-effort, never fatal" behavior.

How it's tested

A new SolidCacheConnectionFailedFailsafeTest reuses the shared FailureSafetyBehavior and mirrors the existing timeout test (the pattern moved to in #285), emulating a terminated connection by stubbing the adapter to raise ActiveRecord::ConnectionFailed. It guards the regression: cache operations drop to a miss rather than raising.

The test helpers were also tidied up — emulating_timeouts and emulating_connection_failures now delegate to a shared emulating_errors(error_class) helper to avoid the near-verbatim duplication.

All test configurations pass (TARGET_DB=sqlite bundle exec rake test): 400 runs, 0 failures, 0 errors across all 8 config matrices. RuboCop is clean.

Notes

No CHANGELOG entry — the repo has never maintained a CHANGELOG file, and recent bug-fix PRs (e.g. #286, #287) add none.

A terminated cache database connection raises ActiveRecord::ConnectionFailed,
a subclass of ActiveRecord::StatementInvalid added in Rails 7.1. It wasn't in
TRANSIENT_ACTIVE_RECORD_ERRORS, so instead of degrading to a cache miss the
error propagated to the caller. Add it alongside the other transient
connection errors and cover it with the failure safety behavior.

Fixes: rails#307
@ajaynomics ajaynomics closed this Jun 24, 2026
@ajaynomics ajaynomics reopened this Jun 24, 2026
@ajaynomics ajaynomics marked this pull request as ready for review June 24, 2026 14:18
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.

ActiveRecord::ConnectionFailed not caught by the failsafe (terminated connection surfaces as an error)

1 participant