Repository navigation
fix(hangfire): run each job in its own scope and trace so earlier jobs' breadcrumbs don't fill its events - #6074
Conversation
…adcrumbs An exception breadcrumb is skipped when the scope already holds one with the same message and data. Closes #3632 Co-Authored-By: Claude <noreply@anthropic.com>
PR risk: mediumFor commit Main concerns: data sent to Sentry, host performance, reach. This label is a hint for reviewers, not an approval. |
There was a problem hiding this comment.
Good to approve, with optional comments
This fixes #3632 at the cause: each Hangfire job now runs in its own scope and trace, pushed in OnPerforming and popped by SentryJobScopeCloser (order int.MaxValue) once the job leaves Processing, so other filters' OnStateApplied still see the job's scope. Tests cover retries, per-job traces, later filters, the leftover-scope fallback with a real Hub, two workers, and the closer's registration from both public UseSentry overloads. No public API change.
- Optional: in global mode with
Debugon, every job logs a "Push scope called in global mode" warning (SentryServerFilter.PushJobScope, viaSentryScopeManager.PushScope). Harmless, but noisy for desktop-style apps that run Hangfire; the description already calls it out. - Optional: the "didn't push, so keep the shared trace" guard in
PushJobScopeis tested only for global mode (ServerFilterTests.OnPerforming_GlobalMode_KeepsSharedScopesTrace); the locked-scope case takes the same path but has no test.
This is an automated preliminary review.
|
Mutation testing breaks the lines a PR changes on purpose, one mutation at a time, and checks that a test fails each time. A changed line no test runs fails the check, and a mutant that survives the tests only warns. Full report Sentry.Hangfire: tests caught 10 of 10 mutants on the changed linesStryker couldn't build 11 mutants on the changed lines, so no test ran against them. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #6074 +/- ##
==========================================
+ Coverage 78.46% 78.60% +0.13%
==========================================
Files 537 540 +3
Lines 20167 20218 +51
Branches 4056 4065 +9
==========================================
+ Hits 15825 15893 +68
+ Misses 3494 3481 -13
+ Partials 848 844 -4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Breadcrumbs and exception breadcrumbs from one Hangfire job no longer leak into the events of later jobs on the same server. Replaces the earlier approach of deduplicating exception breadcrumbs in Hub, which didn't match Hangfire's failure messages: they include the job ID and retry attempt. The scope is pushed in OnPerforming and popped in OnStateApplied when the job leaves the Processing state, because Hangfire logs a job's failure while electing its next state, after OnPerformed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each job now fails once, is retried and fails again, so the test covers the Processing -> Scheduled transition behind the original report as well as Processing -> Failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Jobs shared the root scope's trace ID, so events from unrelated jobs ended up in one trace. The job scope now gets its own trace, like Sentry.Quartz. The trace is only reset when PushScope actually pushed: in global mode, or when the current scope is locked, it returns without pushing, and resetting would change the trace of the scope the whole app shares. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…Applied The job scope was closed in SentryServerFilter's own OnStateApplied, which runs at the default order, so filters ordered later or added after UseSentry no longer saw the job's scope. A separate filter registered at int.MaxValue now closes it. Also covers two workers sharing the filter, checks the leftover-scope case against a real Hub, and avoids the closure allocation in GetCurrentScope. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Summary
Hangfire jobs ran on whatever scope the worker thread happened to have. In practice that is the root scope, shared by every job. Breadcrumbs from one job, including the
Exceptionbreadcrumb left by each captured failure, ended up on the events of every later job. A job that keeps failing ends up with a breadcrumb trail of nothing but its earlier failures.SentryServerFilternow pushes a new scope for each job, with its own trace, and pops it when the job leaves theProcessingstate.Notes for review
OnPerformed: Hangfire logs a job's failure (AutomaticRetryAttribute, the source of the events in Hangfire job failures result in SentryCheckInIdKey error #3626) while electing the job's next state, which runs afterOnPerformed. Popping there would put the error event and its breadcrumb back on the shared scope. Popping inOnStateAppliedkeeps them in the job's scope. A separateSentryJobScopeCloserfilter does the popping, registered at orderint.MaxValue, so other filters'OnStateApplied(job attributes, higher orders, global filters added afterUseSentry) still run inside the job's scope.SentryServerFilteritself stays at the default order so its scope starts before other filters'OnPerforming. Checked against Hangfire.Core 1.8.12 and 1.8.25: the election runs on the same worker thread, and the scope set inOnPerformingis still current.Processinghappens on another thread (for example, a job deleted from the dashboard mid-run), the nextOnPerformingon that worker disposes the leftover scope before pushing a new one.PushScopeis a no-op there, so jobs keep sharing the one scope and trace.Sentry.Hangfireisn't strong-named, so unlikeSentry.Quartzit can't readIsGlobalModeEnabledto skip the call. In global mode withDebugon, each job logs a "Push scope called in global mode" warning.Hubby message. Hangfire's failure messages include the job ID and retry attempt (Failed to process the job '838861': … Retry attempt 2 of 5 …), so none of the breadcrumbs in the original report would have matched.Sentry.Quartz, each job's scope starts a new trace, using the publicIHub.ContinueTrace(null, null)(Sentry.Hangfirecan't seeScope.SetPropagationContext). This is skipped whenPushScopedidn't push (global mode or a locked scope), so the shared scope's trace isn't reset.Closes #3632
🤖 Generated with Claude Code