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

Fixed duplicate key exception for Hangfire jobs with AutomaticRetry #3631

Merged
merged 3 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
Sentry will reject all metrics sent after October 7, 2024.
Learn more: https://sentry.zendesk.com/hc/en-us/articles/26369339769883-Upcoming-API-Changes-to-Metrics ([#3619](https://github.com/getsentry/sentry-dotnet/pull/3619))

## Fixes

- Fixed duplicate key exception for Hangfire jobs with AutomaticRetry ([#3631](https://github.com/getsentry/sentry-dotnet/pull/3631))

### Features

- Added a flag to options `DisableFileWrite` to allow users to opt-out of all file writing operations. Note that toggling this will affect features such as offline caching and auto-session tracking and release health as these rely on some file persistency ([#3614](https://github.com/getsentry/sentry-dotnet/pull/3614))
Expand Down
6 changes: 5 additions & 1 deletion src/Sentry.Hangfire/SentryServerFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ public void OnPerforming(PerformingContext context)
}

var checkInId = _hub.CaptureCheckIn(monitorSlug, CheckInStatus.InProgress);
context.Items.Add(SentryCheckInIdKey, checkInId);

// Note that we may be overwriting context.Items[SentryCheckInIdKey] here, which is intentional. If that happens
// then implicitly OnPerforming was called previously with the same context, but we never made it to OnPerformed
// This might happen if a Hangfire job failed at least once, with automatic retries configured.
context.Items[SentryCheckInIdKey] = checkInId;
}

public void OnPerformed(PerformedContext context)
Expand Down
52 changes: 52 additions & 0 deletions test/Sentry.Hangfire.Tests/ServerFilterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Hangfire;
using Hangfire.Common;
using Hangfire.Server;
using Hangfire.Storage;

namespace Sentry.Hangfire.Tests;

public class ServerFilterTests
{
[Fact]
public void OnPerforming_IsReentrant()
{
// Arrange
const string jobId = "test-id";
const string monitorSlug = "test-monitor-slug";

var storageConnection = Substitute.For<IStorageConnection>();
storageConnection.GetJobParameter(jobId, SentryServerFilter.SentryMonitorSlugKey).Returns(
SerializationHelper.Serialize(monitorSlug)
);

var backgroundJob = new BackgroundJob(jobId, null, DateTime.Now);
var cancellationToken = Substitute.For<IJobCancellationToken>();
var performContext = new PerformContext(
null,
storageConnection,
backgroundJob,
cancellationToken
);
var performingContext = new PerformingContext(performContext);

var hub = Substitute.For<IHub>();
hub.CaptureCheckIn(monitorSlug, CheckInStatus.InProgress).Returns(SentryId.Create());

var logger = Substitute.For<IDiagnosticLogger>();
var filter = new SentryServerFilter(hub, logger);

// Act
filter.OnPerforming(performingContext);

// Assert
performContext.Items.ContainsKey(SentryServerFilter.SentryCheckInIdKey).Should().BeTrue();
var firstKey = performingContext.Items[SentryServerFilter.SentryCheckInIdKey];

// Act
filter.OnPerforming(performingContext);

// Assert
performContext.Items.ContainsKey(SentryServerFilter.SentryCheckInIdKey).Should().BeTrue();
performingContext.Items[SentryServerFilter.SentryCheckInIdKey].Should().NotBeSameAs(firstKey);
}
}
Loading