Skip to content

fix: prevent crash when scheduling a date slightly in the past#1045

Open
Syed-Zeeyan wants to merge 1 commit intokelektiv:mainfrom
Syed-Zeeyan:fix-date-past-crash
Open

fix: prevent crash when scheduling a date slightly in the past#1045
Syed-Zeeyan wants to merge 1 commit intokelektiv:mainfrom
Syed-Zeeyan:fix-date-past-crash

Conversation

@Syed-Zeeyan
Copy link

Problem
Scheduling a CronJob with a Date that slips slightly into the past (even by a few milliseconds due to event loop delay) currently throws an uncaught
CronError('WARNING: Date in past. Will never be fired.').

This error originates from CronTime.sendAt() and propagates through getTimeout() into CronJob.start(), causing the application to crash with no opportunity for consumers to handle the error.

In practice, a date that is valid at construction time can become “past” by the time start() runs, leading to unexpected crashes in real-world usage.


Root Cause
CronTime.sendAt() unconditionally throws when this.realDate is true and the scheduled date is before DateTime.local().

Unlike cron-expression jobs — which already handle negative timeouts gracefully through the existing threshold logic — one-shot date jobs throw before getTimeout() can return, preventing the scheduler from applying its built-in handling for late executions.


Solution
This PR makes two minimal changes to allow one-shot date jobs to follow the same safe path already used for cron-expression jobs:

1. src/time.ts
Removed the hard throw inside sendAt() for past realDate values.
Instead, the scheduled date is returned normally, allowing getTimeout() to produce a negative timeout when appropriate.

2. src/job.ts
Added a runOnce guard inside the negative-timeout branch of start().
After the existing threshold logic determines whether to execute immediately or skip, one-shot jobs now deactivate instead of attempting to reschedule a date that will never occur.

This ensures:

  • no uncaught exception

  • no infinite rescheduling loop

  • consistent behavior with existing threshold handling


Why this is safe

  • Cron-expression jobs are unaffected (runOnce only applies to date-based jobs)

  • Future-date jobs behave exactly as before

  • Existing threshold logic is reused rather than replaced

  • No public API or configuration changes introduced

This simply allows one-shot jobs to use the same negative-timeout handling already implemented for recurring jobs.


Testing
All existing tests pass.

Additional tests were added to cover this scenario:

crontime tests

  • sendAt() does not throw for past realDate values

  • returns expected date

  • getTimeout() returns negative value

cron job tests

  • No crash with slightly past date

  • Executes immediately if within threshold

  • Skips if beyond threshold

  • Deactivates cleanly after handling

  • Works with both constructor and CronJob.from()

Total: all tests passing with new coverage for past-date handling.

@Syed-Zeeyan
Copy link
Author

Hi maintainers, thank you for maintaining this library.

Please let me know if any adjustments or additional tests are needed.
Happy to update the PR accordingly.

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.

1 participant