Repository navigation
fix(rails): Only patch background worker when ActiveRecord::Base is defined - #3170
Open
ryanatwork wants to merge 1 commit into
Open
ryanatwork wants to merge 1 commit into
ryanatwork wants to merge 1 commit into
Conversation
…efined The railtie applied ActiveRecordConnectionPatch whenever ActiveRecord was defined, but the patch calls ActiveRecord::Base after every job. Apps that load only part of ActiveRecord (e.g. active_record/errors without the railtie) have ActiveRecord but not ActiveRecord::Base, so every job raised NameError in the patch's ensure and the worker logged "exception happened in background worker: uninitialized constant ActiveRecord::Base". Check for ActiveRecord::Base instead. defined? sees the autoload that require "active_record" registers without loading Base, so full ActiveRecord apps still get the patch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Sentry::RailtieappliesSentry::BackgroundWorker::ActiveRecordConnectionPatchwheneverActiveRecordis defined:The patch then calls
ActiveRecord::Basein anensureafter every job:Some apps load only part of ActiveRecord, for example
require "active_record/errors"orrequire "active_record/validations"for the error and validation classes, withoutactive_record/railtieorrequire "active_record". In those appsActiveRecordis defined, butActiveRecord::Baseis neither defined nor registered for autoload. So every background-worker job raisesNameErrorin theensure, andBackgroundWorker#performlogs:Events are still delivered, because the block runs before the
ensure. But the SDK logs an error for every envelope it sends.This PR checks
defined?(ActiveRecord::Base)instead.defined?returns"constant"for the autoload thatrequire "active_record"registers, and it doesn't loadactive_record/base, so apps with full ActiveRecord still get the patch with no change in load order.Related: #1618 (ActiveRecord loaded but misconfigured, which led to the
connected?guard). That isn't the same problem: hereActiveRecord::Baseisn't loaded at all.Tests
Adds
spec/isolated/background_worker_spec.rb. The patch is applied at most once per process, so each case boots a bare app in a child process, likestreaming_reporter_spec.rb:active_record/railtie: the patch is applied, and a job runs without logging an error.active_model+active_record/errors: the patch is skipped, and a job runs without logging an error. Without the railtie change this case fails with"patched,ran,error".The existing connection-release specs in
spec/sentry/rails/client_spec.rbstill pass for full ActiveRecord apps.🤖 Generated with Claude Code