fix(cron): prevent spin loop when job completes within firing second#17967
Merged
steipete merged 2 commits intoopenclaw:mainfrom Feb 16, 2026
Merged
fix(cron): prevent spin loop when job completes within firing second#17967steipete merged 2 commits intoopenclaw:mainfrom
steipete merged 2 commits intoopenclaw:mainfrom
Conversation
…penclaw#17821) When a cron job fires at 13:00:00.014 and completes at 13:00:00.021, computeNextRunAtMs was flooring nowMs to 13:00:00.000 and asking croner for the next occurrence from that exact boundary. Croner could return 13:00:00.000 (same second) since it uses >= semantics, causing the job to be immediately re-triggered hundreds of times. Fix: Ask croner for the next occurrence starting from the NEXT second (e.g., 13:00:01.000). This ensures we always skip the current/elapsed second and correctly return the next day's occurrence. This also correctly handles the before-match case: if nowMs is 11:59:59.500, we ask from 12:00:00.000, and croner returns today's 12:00:00.000 match. Added regression tests for the spin loop scenario.
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.
Fixes #17821
Problem
When a cron job fires at 13:00:00.014 and completes at 13:00:00.021,
computeNextRunAtMswas flooringnowMsto 13:00:00.000 and asking croner for the next occurrence from that exact boundary. Croner could return 13:00:00.000 (same second) since it uses >= semantics, causing the job to be immediately re-triggered hundreds of times per second.Solution
Ask croner for the next occurrence starting from the NEXT second (e.g., 13:00:01.000). This ensures we always skip the current/elapsed second and correctly return the next day's occurrence.
This also correctly handles the before-match case: if
nowMsis 11:59:59.500, we ask from 12:00:00.000, and croner returns today's 12:00:00.000 match.Testing
Greptile Summary
Fixed critical spin loop bug where cron jobs that fired and completed within the same second would immediately retrigger hundreds of times per second.
Key Changes
computeNextRunAtMsto ask croner for the next occurrence starting from the NEXT second (current second + 1000ms) instead of the current secondnextMs > nowSecondMssafety check since asking from the next second already guarantees the result is in the futureHow It Works
The old code floored
nowMsto the current second boundary and asked croner from that point. Due to croner's >= semantics, it would return the current second if it matched the schedule. The code then filtered this out withnextMs > nowSecondMs, which caused it to returnundefined. WhennextRunAtMsis undefined, the job is immediately considered "due" on the next tick, creating a spin loop.The fix ensures croner is always asked from at least 1 second in the future, so it never returns the current/elapsed second. This prevents the undefined return value and correctly schedules the job for the next valid occurrence (e.g., next day for daily jobs).
Confidence Score: 5/5
Last reviewed commit: 4dd449c
(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!