fix: prevent crash when scheduling a date slightly in the past#1045
Open
Syed-Zeeyan wants to merge 1 commit intokelektiv:mainfrom
Open
fix: prevent crash when scheduling a date slightly in the past#1045Syed-Zeeyan wants to merge 1 commit intokelektiv:mainfrom
Syed-Zeeyan wants to merge 1 commit intokelektiv:mainfrom
Conversation
Author
|
Hi maintainers, thank you for maintaining this library. Please let me know if any adjustments or additional tests are needed. |
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.
Problem
Scheduling a
CronJobwith aDatethat slips slightly into the past (even by a few milliseconds due to event loop delay) currently throws an uncaughtCronError('WARNING: Date in past. Will never be fired.').This error originates from
CronTime.sendAt()and propagates throughgetTimeout()intoCronJob.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 whenthis.realDateis true and the scheduled date is beforeDateTime.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 pastrealDatevalues.Instead, the scheduled date is returned normally, allowing
getTimeout()to produce a negative timeout when appropriate.2. src/job.ts
Added a
runOnceguard inside the negative-timeout branch ofstart().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 (
runOnceonly 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 valuesreturns expected date
getTimeout()returns negative valuecron 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.