Skip to content

Fix duplicate key error in FetchedJobs when job has multiple queue entries#419

Merged
azygis merged 1 commit into
hangfire-postgres:masterfrom
nicolaj-hartmann:fix/duplicate-jobqueue-entries
Feb 7, 2026
Merged

Fix duplicate key error in FetchedJobs when job has multiple queue entries#419
azygis merged 1 commit into
hangfire-postgres:masterfrom
nicolaj-hartmann:fix/duplicate-jobqueue-entries

Conversation

@nicolaj-hartmann

Copy link
Copy Markdown
Contributor

Problem

When a job has multiple entries in the jobqueue table (e.g., due to re-queuing or race conditions), the Hangfire dashboard crashes with:

System.ArgumentException: An item with the same key has already been added. Key: 185

This occurs in PostgreSqlMonitoringApi.FetchedJobs because the SQL query returns duplicate job IDs which are then passed to ToDictionary().

Solution

  • Add DISTINCT to GetQueuedOrFetchedJobIds to ensure unique job IDs are returned
  • Add DISTINCT ON (j."id") to FetchedJobs and EnqueuedJobs SQL queries as a safeguard

Testing

Added integration test FetchedJobs_WithDuplicateJobQueueEntries_DoesNotThrow that:

  1. Creates a job with two jobqueue entries (same jobId)
  2. Calls FetchedJobs via the monitoring API
  3. Verifies no exception is thrown and only one job is returned

…tries

Add DISTINCT/DISTINCT ON to SQL queries in monitoring API to handle
cases where a job has multiple entries in the jobqueue table. This
prevents ArgumentException when the dashboard displays fetched jobs.
@nicolaj-hartmann

Copy link
Copy Markdown
Contributor Author

@azygis ? :)

@azygis

azygis commented Feb 5, 2026

Copy link
Copy Markdown
Collaborator

Will try to merge and publish this week.

@azygis azygis closed this Feb 7, 2026
@azygis azygis reopened this Feb 7, 2026
@azygis azygis merged commit 82d485e into hangfire-postgres:master Feb 7, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants