Skip to content

Chained exception reports the outer exception's type and message for a cause with a shared or missing backtrace #3220

Open

Description

@clucas

Issue Description

When an exception chain contains a cause whose backtrace is shared with an exception already processed, or a cause with no backtrace, the event reports that cause with the outermost exception's type, module and message instead of its own.

This is common with Rails: ActiveRecord translates PG::UniqueViolation into ActiveRecord::RecordNotUnique and copies the original backtrace with set_backtrace. When that error is wrapped again (for example by a gRPC interceptor raising GRPC::Internal), Sentry shows:

GRPC::Internal → GRPC::Internal → PG::UniqueViolation

instead of

GRPC::Internal → ActiveRecord::RecordNotUnique → PG::UniqueViolation

The cause is in Sentry::ExceptionInterface.build (sentry-ruby/lib/sentry/interfaces/exception.rb). The else branch, taken when a backtrace was already processed or is nil, builds the entry from exception (the outermost error) instead of the current cause e:

exceptions = exceptions.map do |e|
  if e.backtrace && !processed_backtrace_ids.include?(e.backtrace.object_id)
    processed_backtrace_ids << e.backtrace.object_id
    SingleExceptionInterface.build_with_stacktrace(exception: e, ...)
  else
    SingleExceptionInterface.new(exception: exception, mechanism: mechanism) # should be `e`
  end
end

This looks like a regression from #1296 (Feb 2021, "Refactor event interface construction"). Before that refactor, Event#add_exception_interface set type, value and module from e for every entry, and only skipped the duplicate stacktrace (the dedupe that fixed #514).

Reproduction Steps

inner = begin
  raise ArgumentError, "inner message"
rescue ArgumentError => e
  e
end
middle = begin
  raise TypeError, "middle message", inner.backtrace, cause: inner # shares inner's backtrace object
rescue TypeError => e
  e
end
outer = begin
  raise RuntimeError, "outer message", cause: middle
rescue RuntimeError => e
  e
end

Sentry.capture_exception(outer)

A stackless cause shows the same problem:

begin
  raise RuntimeError, "outer message", cause: Errno::ENOENT.new("cause message")
rescue RuntimeError => e
  Sentry.capture_exception(e)
end

Expected Behavior

exception.values types are ["ArgumentError", "TypeError", "RuntimeError"]. The middle entry carries the middle cause's own message (its detailed_message, for example middle message (TypeError) on Ruby 4.0.6). Its stacktrace stays omitted, because it is the same as the inner one.

For the stackless cause: ["Errno::ENOENT", "RuntimeError"], with module Errno and the cause's own message (for example No such file or directory - cause message (Errno::ENOENT) on Ruby 4.0.6).

Actual Behavior

exception.values types are ["ArgumentError", "RuntimeError", "RuntimeError"]. The middle entry carries the outer exception's type, module and message (outer message (RuntimeError)).

For the stackless cause: ["RuntimeError", "RuntimeError"].

Ruby Version

Observed on Ruby 4.0.6. I have not run other Ruby versions. The faulty branch is visible in the source and runs whenever a cause's backtrace was already processed or is nil. The shared-backtrace reproduction above depends on the middle exception holding the same Array object as the inner one.

SDK Version

7.2.0; also present on master at 1636faa (7.3.0)

Integration and Its Version

sentry-rails 7.2.0, sentry-sidekiq 7.2.0 (not required to reproduce)

Sentry Config

Default configuration.

I have a fix with regression specs ready and will open a PR.

Activity

  1. added a commit that references this issue on Nov 18, 2026
    5d3efc8
  2. linear-code commented on Nov 18, 2026

    @linear-code
  3. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Nov 18, 2026
  4. added a commit that references this issue on Nov 18, 2026
    13087e0
  5. sentry-junior commented on Nov 18, 2026

    @sentry-junior
    Contributor

    Junior stopped: an open PR already fixes this issue, #3221. This says nothing about the bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions