Serious bug with Hangfire + SQL

Serious bug with Hangfire + SQL

avatar

Hi there, 

I found a serious bug with scheduled on time jobs. Basically if you create one time scheduled job and use Set-PsuSchedule or modify time through the UI, it leaves behind orphaned schedules that will fire on PSU restart. This can lead to bad consequences if the jobs are targeting production systems. This needs to be fixed before it screws somebodies environment up. Details below:


PSU 2026.3.2: One-time schedules can run again after restart with SQL persistence

With SQL persistence enabled, PowerShell Universal (PSU) can rerun one-time schedules that already ran, were renamed, or were cancelled or deleted. Three interacting defects leave stale rows in dbo.OneTimeSchedules or leave old jobs scheduled in Hangfire.

Steps to reproduce

1. Configure PSU 2026.3.2 with SQL persistence.

2. Create a one-time schedule with New-PSUSchedule -Name "Test - T1" -OneTime ....

3. Use Set-PSUSchedule to rename it to Test - T2 and change its -OneTime timestamp.

4. Check dbo.OneTimeSchedules and HangFire.[Set] (key: schedule). Both jobs remain scheduled in Hangfire, but the old Test - T1 schedule is no longer visible to Get-PSUSchedule or removable through Remove-PSUSchedule.

5. Cancel or delete the Hangfire job so its state is Deleted. Once the corresponding OneTime timestamp is in the past, confirm that its row still exists in dbo.OneTimeSchedules, then restart PSU.

6. Observe that LoadOneTimeSchedulesAsync() re-enqueues the past-due job and it runs immediately on startup.

What happens

1. Startup re-enqueues jobs in Succeeded or Deleted state

On startup, StartupService calls JobService.LoadOneTimeSchedulesAsync(). It reads every row in dbo.OneTimeSchedules and calls IsActiveHangfireJob(backgroundJobClient, oneTimeSchedule.HangfireId).

IsActiveHangfireJob returns false when the Hangfire job is missing or its state is Deleted or Succeeded. The startup path also ignores schedule.Paused. When the check returns false, startup calls ScheduleOneTimeJob(..., allowPast: true). If OneTime is in the past, ScheduleOneTimeJob calls backgroundJobClient.Enqueue(...) and the job runs immediately.

2. Renaming a schedule leaves the old Hangfire job in place

Set-PSUSchedule and PUT /api/v1/schedule/{id} call ScheduleService.UpdateAsync(), which updates the schedule in memory, including its Name, before calling JobService.ScheduleWorkflowJob or ScheduleScriptJob.

Those methods call RemoveStoredOneTimeSchedule(schedule, backgroundJobClient), which looks up the stored row using m.Name == schedule.Name. Because the lookup uses the new name, it misses the row stored under the original name and never cancels the old Hangfire job via BackgroundJobClient.Delete.

The old job can still fire at its original scheduled time, even if Remove-PSUSchedule is later called on the renamed schedule. The orphaned SQL row can also trigger another run on startup.

3. Hangfire marks the dispatcher job Succeeded before the PSU job finishes

ScheduleOneTimeJob schedules JobDispatcher.Execute(executionOptions) and saves the dispatcher job’s ID in dbo.OneTimeSchedules.HangfireId. When the schedule fires, the dispatcher enqueues ExecutionService.Execute(executionOptions) and transitions to Succeeded while the actual script or workflow is still running.

ExecutionService.Execute removes the one-time row through RemoveOneTimeJob only at the end of execution. If PSU restarts before that cleanup finishes, LoadOneTimeSchedulesAsync() sees the dispatcher as inactive and enqueues a second copy. IsJobRunningScheduleAsync blocks the second copy from starting only if the original PSU job is still in Running state.

Expected behavior

- On update: Remove the old dbo.OneTimeSchedules row and cancel its Hangfire job using the original schedule name or a persistent schedule ID before creating the replacement.

- On startup: Respect Paused and do not re-enqueue past-due one-time schedules or jobs already in Deleted or Succeeded state.

- On cancellation, deletion, or dispatch: Reliably remove the stored one-time schedule row so startup cannot dispatch it again.

Workaround

Avoid editing -OneTime schedules in place through Set-PSUSchedule, PUT /api/v1/schedule/{id}, or the Admin Console. Use Remove-PSUSchedule followed by New-PSUSchedule. If orphaned dbo.OneTimeSchedules rows or corresponding Hangfire scheduled jobs already exist, clean them up before restarting PSU.

All Comments (1)

avatar

Hi elsupermang,

Thank you for this very detailed report.

We read the PowerShell Universal 2026.3.2 source, and it confirms the following.

On startup, a stored one-time schedule whose Hangfire job is missing, Deleted or Succeeded is scheduled again, and if its time has passed it is queued to run right away.

That startup path does not check whether the schedule is paused.

When a one-time schedule is renamed, the stored row is looked up under the new name, so the row and the Hangfire job created under the old name stay in place.

The Hangfire job stored for the schedule only hands the work off, so it can complete before the step that runs the script or workflow removes the stored row.

The case where a paused one-time schedule still runs has already been corrected by our development team, and that correction will ship in an upcoming release.

Until a fix is available, removing the schedule and creating a new one, as you suggest, avoids the rename path described above.

A one-time schedule you already renamed can still have its old entry, which can run at the next PSU restart.

Best regards,

Patrick Ouimet