Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug Report: Regression in Hangfire After Version 1.8.12 #2494

Open
VmPeter opened this issue Jan 9, 2025 · 1 comment
Open

Bug Report: Regression in Hangfire After Version 1.8.12 #2494

VmPeter opened this issue Jan 9, 2025 · 1 comment

Comments

@VmPeter
Copy link

VmPeter commented Jan 9, 2025

Summary:
A regression in Hangfire versions after 1.8.12 introduces a subtle bug where the constructor of GlobalJobFilters is not run during initialization. This causes issues with custom global filters, such as disabling automatic retries.

Affected Versions:

Good version: 1.8.12

Bug introduced: Commit f052283 ("Sergey Odinokov 06/06/2024, 13.42 Use C# 7.3's static anonymous functions to have less allocations")

Bug present in: 1.8.18 (latest tested version)

Steps to Reproduce:

Call HangfireServiceCollectionExtensions.AddHangfire with a typical setup, for example:

.UseFilter(
    new AutomaticRetryAttribute
    {
        Attempts = 0
    });

Add a custom global filter after AddHangfire is called, for example:

Hangfire.GlobalJobFilters.Filters.Add(new HangfireJobCreationMetricAttribute(app.Services.GetRequiredService<IMonitoringClient>()));
Observe that a new AutomaticRetry filter with default retries 10 is added despite the custom filter.

Root Cause: The issue seems to stem from this line in the AddHangfire method:

services.TryAddSingleton<IJobFilterProvider>(static _ => JobFilterProviders.Providers);
Adding the static keyword prevents the constructor of GlobalJobFilters from running, leading to incomplete initialization.

Impact:
This bug causes unexpected behavior in tests and applications that rely on custom global filters, such as those that set up no automatic retry for failing jobs. For instance, tests relying on a failing Hangfire job after a set time may fail unexpectedly.

Suggested Fix:
Investigate the use of the static keyword in the lambda expression and consider reverting to a non-static version to ensure proper initialization.

@odinserj
Copy link
Member

Thanks for reporting this! There were also changes related to the filter execution pipeline that could potentially affect the behavior, but unfortunately I can't reproduce the issue using the reproduction code above. May I ask you to prepare a standalone reproduction in some repository to reproduce the issue and understand what to do next?

@odinserj odinserj added this to the Hangfire 1.8.18 milestone Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants