Skip to content

fix(rails): ensure Rails.error.set_context is not lost#3024

Open
solnic wants to merge 3 commits into
masterfrom
2931-railserrorset_context-data-is-silently-lost-sentry-captures-before-the-rails-errorsubscriber-runs
Open

fix(rails): ensure Rails.error.set_context is not lost#3024
solnic wants to merge 3 commits into
masterfrom
2931-railserrorset_context-data-is-silently-lost-sentry-captures-before-the-rails-errorsubscriber-runs

Conversation

@solnic

@solnic solnic commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator

Use ActiveSupport::ExecutionContext to maintain in the context what's set via Rails.error.set_context.


Closes #2931

@solnic
solnic force-pushed the 2931-railserrorset_context-data-is-silently-lost-sentry-captures-before-the-rails-errorsubscriber-runs branch 3 times, most recently from 0b442b1 to c29006f Compare July 17, 2026 08:46
@solnic
solnic marked this pull request as ready for review July 17, 2026 08:48
Comment thread sentry-rails/lib/sentry/rails/structure_sanitizer.rb Outdated
Comment thread sentry-rails/lib/sentry/rails/error_reporter_context.rb Outdated
@solnic
solnic requested review from dingsdax and sl0thentr0py July 17, 2026 09:27

@dingsdax dingsdax left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Comment thread sentry-rails/lib/sentry/rails/structure_sanitizer.rb Outdated
@solnic
solnic force-pushed the 2931-railserrorset_context-data-is-silently-lost-sentry-captures-before-the-rails-errorsubscriber-runs branch from c29006f to 69d3b44 Compare July 17, 2026 10:45
Comment thread sentry-rails/lib/sentry/rails/structure_sanitizer.rb Outdated
Comment thread sentry-rails/lib/sentry/rails/structure_sanitizer.rb Outdated
end

{ "rails.error" => sanitized }
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tags and hint context ignored

Medium Severity

ErrorReporterContext.contexts dumps the full ExecutionContext into rails.error, but unlike ErrorSubscriber it never lifts tags or hint out of that hash. For unhandled exceptions and ActiveJob failures, CaptureExceptions/ActiveJob usually win the capture race, so Rails.error.set_context(tags: ...) / hint: ... still never become real Sentry tags or hints.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 22f13b0. Configure here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's material for a follow-up PR.

@solnic
solnic force-pushed the 2931-railserrorset_context-data-is-silently-lost-sentry-captures-before-the-rails-errorsubscriber-runs branch from 22f13b0 to bb0629b Compare July 20, 2026 12:54
Comment thread sentry-rails/lib/sentry/rails/error_reporter_context.rb Outdated
Comment thread sentry-rails/lib/sentry/rails/error_reporter_context.rb Outdated
Comment thread sentry-rails/lib/sentry/rails/structure_sanitizer.rb Outdated
Comment thread sentry-rails/lib/sentry/rails/error_reporter_context.rb Outdated
@solnic
solnic force-pushed the 2931-railserrorset_context-data-is-silently-lost-sentry-captures-before-the-rails-errorsubscriber-runs branch from b314e12 to de0ec18 Compare July 21, 2026 13:17

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

There are 2 total unresolved issues (including 1 from previous review).

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit de0ec18. Configure here.

return {} if execution_context.empty?

{ "rails.error" => Sentry::Rails::Serializer.serialize(execution_context) }
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsanitized ExecutionContext breaks event send

High Severity

ErrorReporterContext.contexts dumps the full ActiveSupport::ExecutionContext through Serializer, which leaves non-primitive values unchanged. Rails always stores the live controller (and job) instance there, so those objects land in rails.error and can make JSON.generate fail when the event is actually sent—dropping the report. Specs miss this because DummyTransport never serializes envelopes.

Additional Locations (2)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit de0ec18. Configure here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@neel so I reduced this to a regular serializer and this popped up again - it's legit, the context does include by default a full controller instance or an AJ job instance. They'd go through regular JSON serialization path ending up as "#<HelloController:0x0000ffff627e4610>" etc. - we OK with that?

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.

Rails.error.set_context data is silently lost — Sentry captures before the Rails ErrorSubscriber runs

3 participants