Skip to content

Commit 6309f45

Browse files
solnicgithub-copilot
andcommitted
fix(active_job): don't clone hub when thread already has one
When a job runs inline inside a Rack request (e.g. ActiveStorage::AnalyzeJob with perform_enqueued_jobs = true in tests, or inline adapter), the rack thread already has a hub cloned by CaptureExceptions. Unconditionally calling clone_hub_to_current_thread on any non-main thread replaced that hub with a fresh clone of the main hub, wiping out the request transaction. Downstream instrumentation then called methods on nil and Rails returned 500. This manifested as a flaky failure on JRuby (where rack threads are real OS threads, never Thread.main) but not on CRuby (where rack-test runs on the main thread, so the Thread.current != Thread.main guard fires differently). Fix: only clone when the thread doesn't already have a hub. This preserves the lazy-init behaviour for genuine new worker threads (which have no hub yet) without destroying the hub of threads that were already set up by the Rack middleware. Co-Authored-By: github-copilot <noreply@example.com>
1 parent 58f8913 commit 6309f45

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

sentry-rails/lib/sentry/rails/active_job.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,14 @@ def record_producer_span(job)
9898
end
9999

100100
def record(job, trace_headers: nil, user: nil, &block)
101-
Sentry.clone_hub_to_current_thread if Thread.current != Thread.main
101+
# Only clone the hub onto this thread if it doesn't already have one.
102+
# Cloning unconditionally on any non-main thread would destroy the hub
103+
# that the Rack middleware set up for the current request, causing the
104+
# request transaction to be lost when a job runs inline (e.g. with
105+
# perform_enqueued_jobs = true in test or async adapter inline mode).
106+
unless Thread.current.thread_variable_get(Sentry::THREAD_LOCAL)
107+
Sentry.clone_hub_to_current_thread
108+
end
102109

103110
Sentry.with_scope do |scope|
104111
begin

0 commit comments

Comments
 (0)