From 20dad7e60427e34197252194f29b23922b2a296e Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Sat, 31 Oct 2026 10:57:25 -0400 Subject: [PATCH 1/5] fix(ruby): do not capture an exception whose cause chain was already captured Hub#capture_exception only skipped the exact exception object it had marked. With register_error_subscriber, the middleware captures an ActionView::Template::Error and the error subscriber later reports its cause, which is a different object, so a view error became two events. An event already carries the whole cause chain, so mark every exception in the chain when capturing and skip any exception whose chain contains a marked one. Fixes #3166 Co-Authored-By: Claude Sonnet 5.5 --- sentry-ruby/lib/sentry-ruby.rb | 6 +++++- sentry-ruby/lib/sentry/hub.rb | 4 +++- sentry-ruby/spec/sentry_spec.rb | 31 +++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/sentry-ruby/lib/sentry-ruby.rb b/sentry-ruby/lib/sentry-ruby.rb index 52afec20e..23d8f74a6 100644 --- a/sentry-ruby/lib/sentry-ruby.rb +++ b/sentry-ruby/lib/sentry-ruby.rb @@ -577,7 +577,11 @@ def last_event_id # @return [Boolean] def exception_captured?(exc) return false unless initialized? - !!exc.instance_variable_get(CAPTURED_SIGNATURE) + # An event already carries the whole cause chain, so capturing any part of a + # chain that was captured would duplicate it. + Utils::ExceptionCauseChain.exception_to_array(exc).any? do |e| + e.instance_variable_get(CAPTURED_SIGNATURE) + end end # Add a global event processor [Proc]. diff --git a/sentry-ruby/lib/sentry/hub.rb b/sentry-ruby/lib/sentry/hub.rb index f8730e517..4284fc0c3 100644 --- a/sentry-ruby/lib/sentry/hub.rb +++ b/sentry-ruby/lib/sentry/hub.rb @@ -181,7 +181,9 @@ def capture_exception(exception, **options, &block) capture_event(event, **options, &block).tap do # mark the exception as captured so we can use this information to avoid duplicated capturing - exception.instance_variable_set(Sentry::CAPTURED_SIGNATURE, true) + Utils::ExceptionCauseChain.exception_to_array(exception).each do |e| + e.instance_variable_set(Sentry::CAPTURED_SIGNATURE, true) + end end end diff --git a/sentry-ruby/spec/sentry_spec.rb b/sentry-ruby/spec/sentry_spec.rb index 77430216b..a1c5ba2e5 100644 --- a/sentry-ruby/spec/sentry_spec.rb +++ b/sentry-ruby/spec/sentry_spec.rb @@ -1009,6 +1009,37 @@ expect(described_class.exception_captured?(exception)).to eq(true) end + + context "with an exception that has a cause" do + let(:cause) { RuntimeError.new("boom") } + let(:wrapper) do + raise cause + rescue + begin + raise ArgumentError, "wrapper" + rescue => e + e + end + end + + it "does not capture the cause after the wrapper was captured" do + expect(wrapper.cause).to eq(cause) + + described_class.capture_exception(wrapper) + described_class.capture_exception(cause) + + expect(sentry_events.count).to eq(1) + end + + it "does not capture the wrapper after its cause was captured" do + expect(wrapper.cause).to eq(cause) + + described_class.capture_exception(cause) + described_class.capture_exception(wrapper) + + expect(sentry_events.count).to eq(1) + end + end end describe ".get_traceparent" do From da51d2062e0a32870fa61aa57a4517990222a6a4 Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Tue, 3 Nov 2026 07:04:45 -0500 Subject: [PATCH 2/5] Revert "fix(ruby): do not capture an exception whose cause chain was already captured" This reverts commit 20dad7e60427e34197252194f29b23922b2a296e. --- sentry-ruby/lib/sentry-ruby.rb | 6 +----- sentry-ruby/lib/sentry/hub.rb | 4 +--- sentry-ruby/spec/sentry_spec.rb | 31 ------------------------------- 3 files changed, 2 insertions(+), 39 deletions(-) diff --git a/sentry-ruby/lib/sentry-ruby.rb b/sentry-ruby/lib/sentry-ruby.rb index 23d8f74a6..52afec20e 100644 --- a/sentry-ruby/lib/sentry-ruby.rb +++ b/sentry-ruby/lib/sentry-ruby.rb @@ -577,11 +577,7 @@ def last_event_id # @return [Boolean] def exception_captured?(exc) return false unless initialized? - # An event already carries the whole cause chain, so capturing any part of a - # chain that was captured would duplicate it. - Utils::ExceptionCauseChain.exception_to_array(exc).any? do |e| - e.instance_variable_get(CAPTURED_SIGNATURE) - end + !!exc.instance_variable_get(CAPTURED_SIGNATURE) end # Add a global event processor [Proc]. diff --git a/sentry-ruby/lib/sentry/hub.rb b/sentry-ruby/lib/sentry/hub.rb index 4284fc0c3..f8730e517 100644 --- a/sentry-ruby/lib/sentry/hub.rb +++ b/sentry-ruby/lib/sentry/hub.rb @@ -181,9 +181,7 @@ def capture_exception(exception, **options, &block) capture_event(event, **options, &block).tap do # mark the exception as captured so we can use this information to avoid duplicated capturing - Utils::ExceptionCauseChain.exception_to_array(exception).each do |e| - e.instance_variable_set(Sentry::CAPTURED_SIGNATURE, true) - end + exception.instance_variable_set(Sentry::CAPTURED_SIGNATURE, true) end end diff --git a/sentry-ruby/spec/sentry_spec.rb b/sentry-ruby/spec/sentry_spec.rb index a1c5ba2e5..77430216b 100644 --- a/sentry-ruby/spec/sentry_spec.rb +++ b/sentry-ruby/spec/sentry_spec.rb @@ -1009,37 +1009,6 @@ expect(described_class.exception_captured?(exception)).to eq(true) end - - context "with an exception that has a cause" do - let(:cause) { RuntimeError.new("boom") } - let(:wrapper) do - raise cause - rescue - begin - raise ArgumentError, "wrapper" - rescue => e - e - end - end - - it "does not capture the cause after the wrapper was captured" do - expect(wrapper.cause).to eq(cause) - - described_class.capture_exception(wrapper) - described_class.capture_exception(cause) - - expect(sentry_events.count).to eq(1) - end - - it "does not capture the wrapper after its cause was captured" do - expect(wrapper.cause).to eq(cause) - - described_class.capture_exception(cause) - described_class.capture_exception(wrapper) - - expect(sentry_events.count).to eq(1) - end - end end describe ".get_traceparent" do From a240ac1350ee328d328bccc7c9ca088ace6ba86d Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Tue, 3 Nov 2026 07:06:49 -0500 Subject: [PATCH 3/5] fix(rails): dedupe the cause of a view error in CaptureExceptions only Narrow the fix to sentry-rails as requested in review: the previous commit is reverted, so Sentry.exception_captured? and Hub#capture_exception keep their original semantics. Rails::CaptureExceptions now marks the causes of the exception it captured, so ErrorSubscriber does not report them again, and it skips an exception whose cause the error reporter already reported (development order). Fixes #3166 Co-Authored-By: Claude Sonnet 5.5 --- .../lib/sentry/rails/capture_exceptions.rb | 27 +++++++++ .../sentry/rails/capture_exceptions_spec.rb | 59 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 sentry-rails/spec/sentry/rails/capture_exceptions_spec.rb diff --git a/sentry-rails/lib/sentry/rails/capture_exceptions.rb b/sentry-rails/lib/sentry/rails/capture_exceptions.rb index 756990ee7..1400f68ef 100644 --- a/sentry-rails/lib/sentry/rails/capture_exceptions.rb +++ b/sentry-rails/lib/sentry/rails/capture_exceptions.rb @@ -33,15 +33,42 @@ def capture_exception(exception, env) # the exception will be swallowed by ShowExceptions middleware return unless Sentry.initialized? return if show_exceptions?(exception, env) && !Sentry.configuration.rails.report_rescued_exceptions + # Rails' error reporter (see ErrorSubscriber) may already have reported the cause of + # this exception, e.g. ActionView::Template::Error's cause. The event carries the + # whole cause chain, so capturing the wrapper too would produce a duplicate. + return if cause_captured?(exception) options = {} options[:contexts] = execution_context if Sentry.configuration.data_collection.user_info Sentry::Rails.capture_exception(exception, **options).tap do |event| env[ERROR_EVENT_ID_KEY] = event.event_id if event + mark_causes_as_captured(exception) end end + def cause_captured?(exception) + each_cause(exception).any? { |cause| Sentry.exception_captured?(cause) } + end + + # The event already includes the causes, so Rails.error reports of a cause + # (ErrorSubscriber) shouldn't be sent as separate events. + def mark_causes_as_captured(exception) + each_cause(exception).each { |cause| cause.instance_variable_set(Sentry::CAPTURED_SIGNATURE, true) } + end + + def each_cause(exception) + causes = [] + cause = exception.cause + + while cause && !causes.include?(cause) + causes << cause + cause = cause.cause + end + + causes + end + def start_transaction(env, scope) options = { name: scope.transaction_name, diff --git a/sentry-rails/spec/sentry/rails/capture_exceptions_spec.rb b/sentry-rails/spec/sentry/rails/capture_exceptions_spec.rb new file mode 100644 index 000000000..b75bcf217 --- /dev/null +++ b/sentry-rails/spec/sentry/rails/capture_exceptions_spec.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +require "spec_helper" +require "sentry/rails/error_subscriber" + +RSpec.describe Sentry::Rails::CaptureExceptions do + let(:transport) { Sentry.get_current_client.transport } + + before do + make_basic_app + end + + def wrapper_with_cause + cause = RuntimeError.new("the cause") + wrapper = begin + begin + raise cause + rescue + raise ArgumentError, "the wrapper" + end + rescue => e + e + end + + [wrapper, cause] + end + + def run_middleware(exception) + middleware = described_class.new(->(_env) { raise exception }) + expect { middleware.call(Rack::MockRequest.env_for("/")) }.to raise_error(exception) + end + + it "doesn't let the error reporter send the cause of an exception it captured" do + wrapper, cause = wrapper_with_cause + + run_middleware(wrapper) + Sentry::Rails::ErrorSubscriber.new.report(cause, handled: false, severity: :error, context: {}) + + expect(transport.events.count).to eq(1) + end + + it "doesn't capture an exception whose cause was already reported by the error reporter" do + wrapper, cause = wrapper_with_cause + + Sentry::Rails::ErrorSubscriber.new.report(cause, handled: false, severity: :error, context: {}) + run_middleware(wrapper) + + expect(transport.events.count).to eq(1) + end + + it "still captures unrelated exceptions" do + wrapper, _cause = wrapper_with_cause + + run_middleware(wrapper) + run_middleware(RuntimeError.new("other")) + + expect(transport.events.count).to eq(2) + end +end From b0fe83b7fbd66adffc8a79a619dc932a7129649c Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Tue, 3 Nov 2026 10:42:26 -0500 Subject: [PATCH 4/5] fix(rails): mark the causes as captured only when an event was sent CaptureExceptions marked the causes of the exception it handled even when capture_exception returned no event (e.g. an excluded wrapper), so a later ErrorSubscriber report of a non-excluded cause was dropped as a duplicate. Co-Authored-By: Claude Sonnet 5.5 --- sentry-rails/lib/sentry/rails/capture_exceptions.rb | 4 +++- .../spec/sentry/rails/capture_exceptions_spec.rb | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/sentry-rails/lib/sentry/rails/capture_exceptions.rb b/sentry-rails/lib/sentry/rails/capture_exceptions.rb index 1400f68ef..d3f7c8654 100644 --- a/sentry-rails/lib/sentry/rails/capture_exceptions.rb +++ b/sentry-rails/lib/sentry/rails/capture_exceptions.rb @@ -42,7 +42,9 @@ def capture_exception(exception, env) options[:contexts] = execution_context if Sentry.configuration.data_collection.user_info Sentry::Rails.capture_exception(exception, **options).tap do |event| - env[ERROR_EVENT_ID_KEY] = event.event_id if event + next unless event + + env[ERROR_EVENT_ID_KEY] = event.event_id mark_causes_as_captured(exception) end end diff --git a/sentry-rails/spec/sentry/rails/capture_exceptions_spec.rb b/sentry-rails/spec/sentry/rails/capture_exceptions_spec.rb index b75bcf217..d9e36693a 100644 --- a/sentry-rails/spec/sentry/rails/capture_exceptions_spec.rb +++ b/sentry-rails/spec/sentry/rails/capture_exceptions_spec.rb @@ -48,6 +48,16 @@ def run_middleware(exception) expect(transport.events.count).to eq(1) end + it "doesn't mark the causes as captured when the wrapper is not reported" do + wrapper, cause = wrapper_with_cause + Sentry.configuration.excluded_exceptions += ["ArgumentError"] + + run_middleware(wrapper) + Sentry::Rails::ErrorSubscriber.new.report(cause, handled: false, severity: :error, context: {}) + + expect(transport.events.count).to eq(1) + end + it "still captures unrelated exceptions" do wrapper, _cause = wrapper_with_cause From 30b910f84f0030e4f156f1d0c2dc9cd35cf0eb89 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 2 Dec 2026 12:17:02 -0400 Subject: [PATCH 5/5] fix(rails): don't report unhandled exceptions from ErrorSubscriber Unhandled exceptions are already captured by the CaptureExceptions middleware, which reports the outermost exception with the request data. With register_error_subscriber enabled, Rails' executor also reports them (for a view error, the cause of ActionView::Template::Error), producing a second, less complete event. ErrorSubscriber now skips reports with handled: false. This replaces the previous CaptureExceptions cause-marking approach, which is reverted. Co-Authored-By: Claude Sonnet 5.5 --- .../lib/sentry/rails/capture_exceptions.rb | 31 +-------- .../lib/sentry/rails/error_subscriber.rb | 5 ++ .../sentry/rails/capture_exceptions_spec.rb | 69 ------------------- .../sentry/rails/error_subscriber_spec.rb | 6 +- sentry-rails/spec/sentry/rails_spec.rb | 19 ++++- 5 files changed, 25 insertions(+), 105 deletions(-) delete mode 100644 sentry-rails/spec/sentry/rails/capture_exceptions_spec.rb diff --git a/sentry-rails/lib/sentry/rails/capture_exceptions.rb b/sentry-rails/lib/sentry/rails/capture_exceptions.rb index d3f7c8654..756990ee7 100644 --- a/sentry-rails/lib/sentry/rails/capture_exceptions.rb +++ b/sentry-rails/lib/sentry/rails/capture_exceptions.rb @@ -33,42 +33,13 @@ def capture_exception(exception, env) # the exception will be swallowed by ShowExceptions middleware return unless Sentry.initialized? return if show_exceptions?(exception, env) && !Sentry.configuration.rails.report_rescued_exceptions - # Rails' error reporter (see ErrorSubscriber) may already have reported the cause of - # this exception, e.g. ActionView::Template::Error's cause. The event carries the - # whole cause chain, so capturing the wrapper too would produce a duplicate. - return if cause_captured?(exception) options = {} options[:contexts] = execution_context if Sentry.configuration.data_collection.user_info Sentry::Rails.capture_exception(exception, **options).tap do |event| - next unless event - - env[ERROR_EVENT_ID_KEY] = event.event_id - mark_causes_as_captured(exception) - end - end - - def cause_captured?(exception) - each_cause(exception).any? { |cause| Sentry.exception_captured?(cause) } - end - - # The event already includes the causes, so Rails.error reports of a cause - # (ErrorSubscriber) shouldn't be sent as separate events. - def mark_causes_as_captured(exception) - each_cause(exception).each { |cause| cause.instance_variable_set(Sentry::CAPTURED_SIGNATURE, true) } - end - - def each_cause(exception) - causes = [] - cause = exception.cause - - while cause && !causes.include?(cause) - causes << cause - cause = cause.cause + env[ERROR_EVENT_ID_KEY] = event.event_id if event end - - causes end def start_transaction(env, scope) diff --git a/sentry-rails/lib/sentry/rails/error_subscriber.rb b/sentry-rails/lib/sentry/rails/error_subscriber.rb index 39ba5be42..dc6c7a3e5 100644 --- a/sentry-rails/lib/sentry/rails/error_subscriber.rb +++ b/sentry-rails/lib/sentry/rails/error_subscriber.rb @@ -9,6 +9,11 @@ class ErrorSubscriber SKIP_SOURCES = Regexp.union([/.*_cache_store.active_support/]) def report(error, handled:, severity:, context:, source: nil) + # Unhandled exceptions are already captured by the CaptureExceptions middleware, + # which reports the outermost exception with the request data. Reporting them here + # too would send a second event, e.g. for the cause of an ActionView::Template::Error. + return unless handled + tags = { handled: handled } if source diff --git a/sentry-rails/spec/sentry/rails/capture_exceptions_spec.rb b/sentry-rails/spec/sentry/rails/capture_exceptions_spec.rb deleted file mode 100644 index d9e36693a..000000000 --- a/sentry-rails/spec/sentry/rails/capture_exceptions_spec.rb +++ /dev/null @@ -1,69 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" -require "sentry/rails/error_subscriber" - -RSpec.describe Sentry::Rails::CaptureExceptions do - let(:transport) { Sentry.get_current_client.transport } - - before do - make_basic_app - end - - def wrapper_with_cause - cause = RuntimeError.new("the cause") - wrapper = begin - begin - raise cause - rescue - raise ArgumentError, "the wrapper" - end - rescue => e - e - end - - [wrapper, cause] - end - - def run_middleware(exception) - middleware = described_class.new(->(_env) { raise exception }) - expect { middleware.call(Rack::MockRequest.env_for("/")) }.to raise_error(exception) - end - - it "doesn't let the error reporter send the cause of an exception it captured" do - wrapper, cause = wrapper_with_cause - - run_middleware(wrapper) - Sentry::Rails::ErrorSubscriber.new.report(cause, handled: false, severity: :error, context: {}) - - expect(transport.events.count).to eq(1) - end - - it "doesn't capture an exception whose cause was already reported by the error reporter" do - wrapper, cause = wrapper_with_cause - - Sentry::Rails::ErrorSubscriber.new.report(cause, handled: false, severity: :error, context: {}) - run_middleware(wrapper) - - expect(transport.events.count).to eq(1) - end - - it "doesn't mark the causes as captured when the wrapper is not reported" do - wrapper, cause = wrapper_with_cause - Sentry.configuration.excluded_exceptions += ["ArgumentError"] - - run_middleware(wrapper) - Sentry::Rails::ErrorSubscriber.new.report(cause, handled: false, severity: :error, context: {}) - - expect(transport.events.count).to eq(1) - end - - it "still captures unrelated exceptions" do - wrapper, _cause = wrapper_with_cause - - run_middleware(wrapper) - run_middleware(RuntimeError.new("other")) - - expect(transport.events.count).to eq(2) - end -end diff --git a/sentry-rails/spec/sentry/rails/error_subscriber_spec.rb b/sentry-rails/spec/sentry/rails/error_subscriber_spec.rb index 6b2e36d80..f047b47a7 100644 --- a/sentry-rails/spec/sentry/rails/error_subscriber_spec.rb +++ b/sentry-rails/spec/sentry/rails/error_subscriber_spec.rb @@ -81,10 +81,8 @@ end context 'when handled: false' do - it 'sets mechanism.handled to false' do - expect(Sentry::Rails).to receive(:capture_exception) do |_, hint:, **_| - expect(hint[:mechanism].handled).to eq(false) - end + it 'does not capture the exception' do + expect(Sentry::Rails).not_to receive(:capture_exception) described_class.new.report(StandardError.new, handled: false, severity: :error, context: {}) end end diff --git a/sentry-rails/spec/sentry/rails_spec.rb b/sentry-rails/spec/sentry/rails_spec.rb index b93435cdd..5f8fa1a3c 100644 --- a/sentry-rails/spec/sentry/rails_spec.rb +++ b/sentry-rails/spec/sentry/rails_spec.rb @@ -331,15 +331,30 @@ end it "registers Sentry::Rails::ErrorSubscriber to Rails" do - Rails.error.report(Exception.new, handled: false) + Rails.error.report(Exception.new, handled: true) expect(transport.events.count).to eq(1) - ActiveSupport.error_reporter.report(Exception.new, handled: false) + ActiveSupport.error_reporter.report(Exception.new, handled: true) expect(transport.events.count).to eq(2) end + it "doesn't report unhandled exceptions, which CaptureExceptions already captures" do + Rails.error.report(Exception.new, handled: false) + + expect(transport.events.count).to eq(0) + end + + it "reports a view error only once" do + get "/view_exception" + + expect(transport.events.count).to eq(1) + + event = transport.events.first + expect(event.exception.values.last.type).to eq("ActionView::Template::Error") + end + it "sets correct contextual data to the reported event" do Rails.error.handle(severity: :info, context: { foo: "bar" }) do 1/0