fix(cron): prevent recurring job re-fire on gateway crash/restart loop#3396
Merged
fix(cron): prevent recurring job re-fire on gateway crash/restart loop#3396
Conversation
When a gateway crashes mid-job execution (before mark_job_run can persist the updated next_run_at), the job would fire again on every restart attempt within the grace window. For a daily 6:15 AM job with a 2-hour grace, rapidly restarting the gateway could trigger dozens of duplicate runs. Fix: call advance_next_run() BEFORE run_job() in tick(). For recurring jobs (cron/interval), this preemptively advances next_run_at to the next future occurrence and persists it to disk. If the process then crashes during execution, the job won't be considered due on restart. One-shot jobs are left unchanged — they still retry on restart since there's no future occurrence to advance to. This changes the scheduler from at-least-once to at-most-once semantics for recurring jobs, which is the correct tradeoff: missing one daily message is far better than sending it dozens of times.
StreamOfRon
pushed a commit
to StreamOfRon/hermes-agent
that referenced
this pull request
Mar 29, 2026
NousResearch#3396) When a gateway crashes mid-job execution (before mark_job_run can persist the updated next_run_at), the job would fire again on every restart attempt within the grace window. For a daily 6:15 AM job with a 2-hour grace, rapidly restarting the gateway could trigger dozens of duplicate runs. Fix: call advance_next_run() BEFORE run_job() in tick(). For recurring jobs (cron/interval), this preemptively advances next_run_at to the next future occurrence and persists it to disk. If the process then crashes during execution, the job won't be considered due on restart. One-shot jobs are left unchanged — they still retry on restart since there's no future occurrence to advance to. This changes the scheduler from at-least-once to at-most-once semantics for recurring jobs, which is the correct tradeoff: missing one daily message is far better than sending it dozens of times.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a gateway crashes mid-job execution (before
mark_job_runcan persist the updatednext_run_at), recurring cron jobs fire again on every restart attempt within the grace window. For a daily 6:15 AM job with a 2-hour grace period, rapidly restarting the gateway could trigger dozens of duplicate runs.Root cause:
tick()callsrun_job()(which spawns a full agent session — potentially minutes of execution) beforemark_job_run()updatesnext_run_aton disk. If the process dies between these two calls, the next restart finds the job still due and fires it again.Reported by: ludw1OP (DietPi user) — their gateway was unstable due to missing
dbus-user-sessionpackage, causing repeated restarts that flooded their Telegram with duplicate morning wake-up messages.Changes
cron/jobs.py— Addedadvance_next_run(job_id): for recurring jobs (cron/interval), preemptively computes and persists the next futurenext_run_atbefore execution begins. One-shot jobs are left alone so they can retry on restart.cron/scheduler.py—tick()now callsadvance_next_run()beforerun_job(). If the process crashes mid-run, the persistednext_run_atis already in the future, preventing re-fire.Semantics change
Recurring jobs move from at-least-once to at-most-once delivery. Missing one scheduled run due to a crash is far better than sending dozens of duplicates in a crash loop.
mark_job_run()still runs after successful execution and re-confirms the next run time.Test plan