Skip to content

fix(cron): don't wedge a job when the lastStartDate write fails - #345

Merged
omegascorp merged 1 commit into
modelence:mainfrom
eeshsaxena:fix-cron-stuck-on-failed-write
Aug 28, 2026
Merged

omegascorp merged 1 commit into
modelence:mainfrom
eeshsaxena:fix-cron-stuck-on-failed-write

Conversation

@eeshsaxena

@eeshsaxena eeshsaxena commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #338

runCronJob set state.isRunning = true and then wrote lastStartDate to Mongo before entering the try/catch:

state.isRunning = true;
state.startTs = Date.now();

await cronJobsCollection.upsertOne(/* ... */);   // outside the try

const transaction = startTransaction('cron', `cron:${alias}`);
try {
  await handler();
  handleCronJobCompletion(state, params);
  // ...

If that write rejects, the throw skips both the handler and handleCronJobCompletion, so isRunning is never reset. tickCronJobs runs jobs in a fire-and-forget forEach(async ...), so the rejection isn't awaited or caught anywhere and just becomes an unhandled promise rejection. The job then hits the if (state.isRunning) return guard on every tick and stays wedged until startTs + timeout passes, which is the timeout-recovery path, not normal operation.

Moving the upsertOne inside the existing try makes a failed write behave like any other failure: it's captured, the run is marked errored, and isRunning is reset so the next interval reschedules normally. The success path is unchanged (the write still happens before the handler).

Added a regression test to jobs.test.ts that rejects upsertOne and asserts the error is captured and the transaction ends as error. It fails on main (the rejection escapes, captureError is never called) and passes with the fix. Full cron/jobs.test.ts suite is green (8 passed).


Note

Low Risk
Small, well-tested reliability fix that only relocates an existing write into the existing error path; success behavior is unchanged.

Overview
Prevents cron jobs from getting stuck when the lastStartDate Mongo write fails.

Moves the upsertOne in runCronJob inside the existing try/catch so a rejected write is captured, the run is marked errored, and isRunning is reset via handleCronJobCompletion instead of leaving the job wedged until timeout.

Adds a regression test that rejects upsertOne and asserts the error is captured and the transaction ends as error.

Reviewed by Cursor Bugbot for commit af2c46a. Bugbot is set up for automated code reviews on this repo. Configure here.

runCronJob wrote lastStartDate to Mongo before entering the try/catch, so a
failed write threw past the handler and past handleCronJobCompletion. Since
tickCronJobs runs jobs in a fire-and-forget forEach, the rejection went
unhandled, isRunning stayed true, and the job was skipped every tick until
its timeout elapsed.

Move the write inside the try so a failed write is captured, the run is
marked errored, and isRunning is reset like any other failure.

Fixes #338
@eeshsaxena

Copy link
Copy Markdown
Contributor Author

Hi! Gentle nudge on this one whenever you have some bandwidth. It's a small, self-contained fix (fix(cron): don't wedge a job when the lastStartDate write fails), and it's currently mergeable with no conflicts. No urgency at all, and I'm happy to make any changes you'd like. Thanks for maintaining modelence!

@omegascorp
omegascorp self-requested a review August 28, 2026 10:03

@omegascorp omegascorp left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@omegascorp
omegascorp merged commit d56381f into modelence:main Aug 28, 2026
3 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.

Failed lastStartDate write leaves cron job stuck until timeout

2 participants