From 33e615d7a3b03e9e3bde539566b2e11e76ce95ce Mon Sep 17 00:00:00 2001 From: Peter Solnica Date: Mon, 23 Nov 2026 13:12:19 +0000 Subject: [PATCH 1/4] test(rails): restore ActiveSupport load hook state after each app boot --- .../test_rails_app/config/application.rb | 20 ++++++++++++++ .../sentry/rails/test_application_spec.rb | 26 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 sentry-rails/spec/sentry/rails/test_application_spec.rb diff --git a/sentry-rails/spec/dummy/test_rails_app/config/application.rb b/sentry-rails/spec/dummy/test_rails_app/config/application.rb index 715a91ac6..5002fde32 100644 --- a/sentry-rails/spec/dummy/test_rails_app/config/application.rb +++ b/sentry-rails/spec/dummy/test_rails_app/config/application.rb @@ -13,7 +13,25 @@ module Sentry module Rails module Test class Application < ::Rails::Application + LOAD_HOOK_IVARS = %i[@load_hooks @loaded].freeze + + def self.snapshot_load_hook_state + @load_hook_snapshot ||= LOAD_HOOK_IVARS.to_h do |name| + [name, ActiveSupport.instance_variable_get(name).transform_values(&:dup)] + end + end + + def self.restore_load_hook_state + snapshot_load_hook_state.each do |name, saved| + current = ActiveSupport.instance_variable_get(name) + current.clear + saved.each { |key, entries| current[key] = entries.dup } + end + end + def self.define + snapshot_load_hook_state + klass = Class.new(Sentry::TestRailsApp) klass.define_singleton_method(:name) { @@ -189,6 +207,8 @@ def after_initialize! end def cleanup! + Test::Application.restore_load_hook_state + ::Rails.application = nil end diff --git a/sentry-rails/spec/sentry/rails/test_application_spec.rb b/sentry-rails/spec/sentry/rails/test_application_spec.rb new file mode 100644 index 000000000..64234de5c --- /dev/null +++ b/sentry-rails/spec/sentry/rails/test_application_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe Sentry::Rails::Test::Application do + include Sentry::Rails::TestHelper + + def load_hook_state + %i[@load_hooks @loaded].map do |name| + ActiveSupport.instance_variable_get(name).transform_values(&:dup) + end + end + + describe "#cleanup!" do + it "restores the ActiveSupport load hook state to what it was before the boot" do + before_boot = load_hook_state + + app = make_basic_app + expect(load_hook_state).not_to eq(before_boot) + + app.cleanup! + + expect(load_hook_state).to eq(before_boot) + end + end +end From b9c24c36304666179f868160a80c371ed82e168f Mon Sep 17 00:00:00 2001 From: Peter Solnica Date: Mon, 23 Nov 2026 13:30:11 +0000 Subject: [PATCH 2/4] test(rails): release global state that pins old test apps after each boot Reset the ActiveRecord, Action Cable, route helper, cache and run-once load hook state that each booted test app leaves behind in cleanup!. --- .../test_rails_app/config/application.rb | 17 ++++- .../sentry/rails/test_application_spec.rb | 70 ++++++++++++++++++- 2 files changed, 84 insertions(+), 3 deletions(-) diff --git a/sentry-rails/spec/dummy/test_rails_app/config/application.rb b/sentry-rails/spec/dummy/test_rails_app/config/application.rb index 5002fde32..0c243306d 100644 --- a/sentry-rails/spec/dummy/test_rails_app/config/application.rb +++ b/sentry-rails/spec/dummy/test_rails_app/config/application.rb @@ -13,10 +13,10 @@ module Sentry module Rails module Test class Application < ::Rails::Application - LOAD_HOOK_IVARS = %i[@load_hooks @loaded].freeze + LOAD_HOOK_IVARS = %i[@load_hooks @loaded @run_once].freeze def self.snapshot_load_hook_state - @load_hook_snapshot ||= LOAD_HOOK_IVARS.to_h do |name| + @load_hook_snapshot ||= LOAD_HOOK_IVARS.select { |name| ActiveSupport.instance_variable_defined?(name) }.to_h do |name| [name, ActiveSupport.instance_variable_get(name).transform_values(&:dup)] end end @@ -209,6 +209,19 @@ def after_initialize! def cleanup! Test::Application.restore_load_hook_state + ::ActiveRecord::Base.filter_attributes = [] + ::ActiveRecord::Encryption.encrypted_attribute_declaration_listeners = nil if defined?(::ActiveRecord::Encryption) + if defined?(::ActiveRecord::FilterAttributeHandler) + ::ActiveRecord::FilterAttributeHandler.remove_instance_variable(:@sensitive_attribute_declaration_listeners) if ::ActiveRecord::FilterAttributeHandler.instance_variable_defined?(:@sensitive_attribute_declaration_listeners) + end + ::ActionCable.remove_instance_variable(:@server) if ::ActionCable.instance_variable_defined?(:@server) + mounted_helpers = ::ActionDispatch::Routing::RouteSet::MountedHelpers + %i[main_app _main_app].each do |name| + mounted_helpers.send(:remove_method, name) if mounted_helpers.method_defined?(name, false) + end + + ::Rails.cache = nil + ::ActionView::PartialRenderer.collection_cache = ::ActiveSupport::Cache::MemoryStore.new ::Rails.application = nil end diff --git a/sentry-rails/spec/sentry/rails/test_application_spec.rb b/sentry-rails/spec/sentry/rails/test_application_spec.rb index 64234de5c..227630ce4 100644 --- a/sentry-rails/spec/sentry/rails/test_application_spec.rb +++ b/sentry-rails/spec/sentry/rails/test_application_spec.rb @@ -6,7 +6,7 @@ include Sentry::Rails::TestHelper def load_hook_state - %i[@load_hooks @loaded].map do |name| + %i[@load_hooks @loaded @run_once].select { |name| ActiveSupport.instance_variable_defined?(name) }.map do |name| ActiveSupport.instance_variable_get(name).transform_values(&:dup) end end @@ -22,5 +22,73 @@ def load_hook_state expect(load_hook_state).to eq(before_boot) end + + it "releases the app's filter parameters from ActiveRecord" do + app = make_basic_app + expect(ActiveRecord::Base.filter_attributes).not_to be_empty + + app.cleanup! + + expect(ActiveRecord::Base.filter_attributes).to be_empty + end + + it "releases the encrypted attribute listeners the app registered" do + skip "ActiveRecord::Encryption is not available" unless defined?(ActiveRecord::Encryption) + + app = make_basic_app + expect(ActiveRecord::Encryption.encrypted_attribute_declaration_listeners).not_to be_empty + + app.cleanup! + + expect(ActiveRecord::Encryption.encrypted_attribute_declaration_listeners).to be_blank + end + + it "releases the sensitive attribute listeners the app registered" do + skip "ActiveRecord::FilterAttributeHandler is not available" unless defined?(ActiveRecord::FilterAttributeHandler) + + app = make_basic_app + listeners = -> { ActiveRecord::FilterAttributeHandler.instance_variable_get(:@sensitive_attribute_declaration_listeners) } + expect(listeners.call).not_to be_empty + + app.cleanup! + + expect(listeners.call).to be_blank + end + + it "releases the Action Cable server built for the app" do + app = make_basic_app + server = ActionCable.server + + app.cleanup! + + expect(ActionCable.server).not_to equal(server) + end + + it "releases the main_app route helper bound to the app" do + app = make_basic_app + expect(ActionDispatch::Routing::RouteSet::MountedHelpers.method_defined?(:main_app)).to be(true) + + app.cleanup! + + expect(ActionDispatch::Routing::RouteSet::MountedHelpers.method_defined?(:main_app)).to be(false) + end + + it "releases the cache store built for the app" do + app = make_basic_app + expect(Rails.cache).not_to be_nil + + app.cleanup! + + expect(Rails.cache).to be_nil + end + + it "releases the partial rendering cache built for the app" do + app = make_basic_app + cache = ActionView::PartialRenderer.collection_cache + + app.cleanup! + + expect(ActionView::PartialRenderer.collection_cache).not_to equal(cache) + end end end From c700170e535c7a478cc5df8bb430960d1ad798ca Mon Sep 17 00:00:00 2001 From: Peter Solnica Date: Mon, 23 Nov 2026 14:09:11 +0000 Subject: [PATCH 3/4] test(rails): extract release_global_state and guard version-specific constants Move the global-state resets out of cleanup! into release_global_state, mirroring restore_load_hook_state. Guard every reset on the constant or setter existing so older Rails cells cannot fail every example in cleanup!. The Rails 6.1 and 7.0 cells were not run: they need Rubies that are not installed here (only 3.4 and 4.0 are). Ruby 4.0 / Rails 8.1.3 shows the same 7 pre-existing failures as before. The reviewer's note on the lockfile edit was right: it was an unrelated working-tree change, reverted rather than committed. --- .../test_rails_app/config/application.rb | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/sentry-rails/spec/dummy/test_rails_app/config/application.rb b/sentry-rails/spec/dummy/test_rails_app/config/application.rb index 0c243306d..85fb6f364 100644 --- a/sentry-rails/spec/dummy/test_rails_app/config/application.rb +++ b/sentry-rails/spec/dummy/test_rails_app/config/application.rb @@ -209,20 +209,40 @@ def after_initialize! def cleanup! Test::Application.restore_load_hook_state - ::ActiveRecord::Base.filter_attributes = [] - ::ActiveRecord::Encryption.encrypted_attribute_declaration_listeners = nil if defined?(::ActiveRecord::Encryption) + release_global_state + ::Rails.application = nil + end + + def release_global_state + if ::ActiveRecord::Base.respond_to?(:filter_attributes=) + ::ActiveRecord::Base.filter_attributes = [] + end + + if defined?(::ActiveRecord::Encryption) + ::ActiveRecord::Encryption.encrypted_attribute_declaration_listeners = nil + end + if defined?(::ActiveRecord::FilterAttributeHandler) - ::ActiveRecord::FilterAttributeHandler.remove_instance_variable(:@sensitive_attribute_declaration_listeners) if ::ActiveRecord::FilterAttributeHandler.instance_variable_defined?(:@sensitive_attribute_declaration_listeners) + handler = ::ActiveRecord::FilterAttributeHandler + if handler.instance_variable_defined?(:@sensitive_attribute_declaration_listeners) + handler.remove_instance_variable(:@sensitive_attribute_declaration_listeners) + end + end + + if defined?(::ActionCable) && ::ActionCable.instance_variable_defined?(:@server) + ::ActionCable.remove_instance_variable(:@server) end - ::ActionCable.remove_instance_variable(:@server) if ::ActionCable.instance_variable_defined?(:@server) + mounted_helpers = ::ActionDispatch::Routing::RouteSet::MountedHelpers %i[main_app _main_app].each do |name| mounted_helpers.send(:remove_method, name) if mounted_helpers.method_defined?(name, false) end ::Rails.cache = nil - ::ActionView::PartialRenderer.collection_cache = ::ActiveSupport::Cache::MemoryStore.new - ::Rails.application = nil + + if defined?(::ActionView::PartialRenderer) && ::ActionView::PartialRenderer.respond_to?(:collection_cache=) + ::ActionView::PartialRenderer.collection_cache = ::ActiveSupport::Cache::MemoryStore.new + end end require_relative "applications/#{application_file}" From a3f142749667a1e3fb0521c5989a08bd8854a4d5 Mon Sep 17 00:00:00 2001 From: Peter Solnica Date: Mon, 23 Nov 2026 15:13:39 +0000 Subject: [PATCH 4/4] test(rails): skip filter_attributes release spec where Rails lacks it ActiveRecord::Base.filter_attributes does not exist on Rails 5.2, so the spec raised NoMethodError on the Ruby 2.7 / Rails 5.2.0 cell. Skip it there, matching the respond_to? guard in release_global_state. --- sentry-rails/spec/sentry/rails/test_application_spec.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sentry-rails/spec/sentry/rails/test_application_spec.rb b/sentry-rails/spec/sentry/rails/test_application_spec.rb index 227630ce4..b888d84dd 100644 --- a/sentry-rails/spec/sentry/rails/test_application_spec.rb +++ b/sentry-rails/spec/sentry/rails/test_application_spec.rb @@ -24,6 +24,8 @@ def load_hook_state end it "releases the app's filter parameters from ActiveRecord" do + skip "ActiveRecord::Base.filter_attributes is not available" unless ActiveRecord::Base.respond_to?(:filter_attributes) + app = make_basic_app expect(ActiveRecord::Base.filter_attributes).not_to be_empty