Skip to content

Commit 03ad132

Browse files
solnicgithub-copilot
andcommitted
fix(active_job): save and restore hub around job execution
This correctly handles all execution modes: - Dedicated async workers (new thread, nil hub): clone -> restore nil - Inline inside a Rack request (rack hub on thread): clone -> restore rack hub so the HTTP response completes normally - Thread-pool workers (recycled thread, stale hub): clone -> restore stale hub (irrelevant; next job will clone again) Co-Authored-By: github-copilot <noreply@example.com>
1 parent 58f8913 commit 03ad132

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

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

Lines changed: 10 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+
# Always give this thread a fresh hub cloned from the main hub so
102+
# the job's events are fully isolated. Save and restore whatever
103+
# hub was on the thread before (e.g. the Rack request hub set by
104+
# CaptureExceptions, or a stale hub left by a recycled thread-pool
105+
# thread) so the outer context continues working correctly after
106+
# the job finishes.
107+
original_hub = Thread.current.thread_variable_get(Sentry::THREAD_LOCAL)
108+
Sentry.clone_hub_to_current_thread
102109

103110
Sentry.with_scope do |scope|
104111
begin
@@ -142,6 +149,8 @@ def record(job, trace_headers: nil, user: nil, &block)
142149
raise
143150
end
144151
end
152+
ensure
153+
Thread.current.thread_variable_set(Sentry::THREAD_LOCAL, original_hub)
145154
end
146155

147156
def set_messaging_data(transaction, job)

0 commit comments

Comments
 (0)