Skip to content

[Hangfire] Fix NullReferenceException for unresolvable jobs#4731

Merged
martincostello merged 4 commits into
open-telemetry:mainfrom
reeloadead:fix/hangfire-metrics-null-job-nre
Jul 13, 2026
Merged

[Hangfire] Fix NullReferenceException for unresolvable jobs#4731
martincostello merged 4 commits into
open-telemetry:mainfrom
reeloadead:fix/hangfire-metrics-null-job-nre

Conversation

@reeloadead

@reeloadead reeloadead commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Changes

The metrics instrumentation registers HangfireMetricsStateFilter, a global IApplyStateFilter. It formats a job name on every state transition, and JobNameFormatter dereferences BackgroundJob.Job with no null check.

When a job's invocation data cannot be deserialized (missing assembly, renamed type/method), Job is null. The filter throws NullReferenceException.

Affects all versions incl. 1.16.0-beta.1.

Note

This typically happens in platforms where the Hangfire scheduler (enqueues jobs) and the worker (executes jobs) are separate applications.
The job's type/method can exist in the scheduler but be missing in the worker, so the worker can't deserialize the job and Job is null.

Observed in production (stack trimmed):

"10 state change attempt(s) failed... moving job to the FailedState"
System.NullReferenceException
   at ...JobNameFormatter.FormatJobName(Job job)
   at ...JobNameFormatter.FormatJobName(BackgroundJob backgroundJob)
   at ...HangfireTagBuilder.GetDefinitionName(BackgroundJob backgroundJob)

Preceded by System.IO.FileNotFoundException: Could not resolve assembly '...' at InvocationData.DeserializeJob().

Fix: null-guard JobNameFormatter and return a sentinel when the job definition cannot be resolved.

Merge requirement checklist

  • CONTRIBUTING guidelines followed (license requirements, nullable enabled, static analysis, etc.)
  • Unit tests added/updated
  • Appropriate CHANGELOG.md files updated for non-trivial changes
  • Changes in public API reviewed (if applicable) — n/a, no public API change

@linux-foundation-easycla

linux-foundation-easycla Bot commented Jul 10, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

@github-actions

Copy link
Copy Markdown
Contributor

Welcome, contributor! Thank you for your contribution to opentelemetry-dotnet-contrib.

Important reminders:

  • Read our Contributing Guidelines.
  • Sign the CLA if you haven't already.
  • Follow the OpenTelemetry Generative AI policy: disclose any AI use in your contribution, and communicate (PR descriptions, review replies) in your own words rather than AI-generated text.
  • Give reviewers at least a few days before pinging them for feedback.
  • If you need help with general setup, development process, or contributor etiquette, ask in #opentelemetry-new-contributors.

@github-actions github-actions Bot added the comp:instrumentation.hangfire Things related to OpenTelemetry.Instrumentation.Hangfire label Jul 10, 2026
@reeloadead
reeloadead force-pushed the fix/hangfire-metrics-null-job-nre branch from 00e09f7 to 16dab96 Compare July 10, 2026 20:10
@reeloadead
reeloadead marked this pull request as ready for review July 10, 2026 20:24
@reeloadead
reeloadead requested a review from a team as a code owner July 10, 2026 20:24
Comment thread src/OpenTelemetry.Instrumentation.Hangfire/CHANGELOG.md Outdated
Comment thread src/OpenTelemetry.Instrumentation.Hangfire/Implementation/JobNameFormatter.cs Outdated
@reeloadead
reeloadead force-pushed the fix/hangfire-metrics-null-job-nre branch from 65ad91d to da7e278 Compare July 13, 2026 08:31
@github-actions
github-actions Bot requested a review from fred2u July 13, 2026 08:32
@codecov

codecov Bot commented Jul 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.32%. Comparing base (41e7326) to head (da7e278).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #4731   +/-   ##
=======================================
  Coverage   77.32%   77.32%           
=======================================
  Files         466      466           
  Lines       19710    19712    +2     
=======================================
+ Hits        15240    15242    +2     
  Misses       4470     4470           
Flag Coverage Δ
unittests-Instrumentation.Hangfire 88.88% <100.00%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...tation.Hangfire/Implementation/JobNameFormatter.cs 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@martincostello
martincostello added this pull request to the merge queue Jul 13, 2026
@martincostello
martincostello removed this pull request from the merge queue due to a manual request Jul 13, 2026
@martincostello martincostello changed the title Fix NullReferenceException in Hangfire metrics filter for unresolvable jobs [Hangfire] Fix NullReferenceException for unresolvable jobs Jul 13, 2026
@martincostello
martincostello added this pull request to the merge queue Jul 13, 2026
This was referenced Jul 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp:instrumentation.hangfire Things related to OpenTelemetry.Instrumentation.Hangfire

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants