-
Notifications
You must be signed in to change notification settings - Fork 52.4k
fix(cron): Normalize timestamps to UTC to prevent double-firing after timezone migration #28951
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
shanewas
wants to merge
3
commits into
NousResearch:main
from
shanewas:fix/cron-timezone-double-fire
+65
−13
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
9b8f553
fix(cron): Normalize timestamps to UTC to prevent double-firing after…
shanewas 74c8a87
test(cron): Add regression tests for UTC timestamp normalization (#28…
shanewas 956185c
refactor(cron): Add safe UTC migration helper for legacy timestamps (…
shanewas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ | |
| import os | ||
| import re | ||
| import uuid | ||
| from datetime import datetime, timedelta | ||
| from datetime import datetime, timedelta, timezone | ||
| from pathlib import Path | ||
| from hermes_constants import get_hermes_home | ||
| from typing import Optional, Dict, List, Any, Union | ||
|
|
@@ -274,24 +274,43 @@ def parse_schedule(schedule: str) -> Dict[str, Any]: | |
|
|
||
|
|
||
| def _ensure_aware(dt: datetime) -> datetime: | ||
| """Return a timezone-aware datetime in Hermes configured timezone. | ||
| """Return a timezone-aware datetime in UTC. | ||
|
|
||
| Backward compatibility: | ||
| - Older stored timestamps may be naive. | ||
| - Naive values are interpreted as *system-local wall time* (the timezone | ||
| `datetime.now()` used when they were created), then converted to the | ||
| configured Hermes timezone. | ||
| All cron timestamps are now normalized to UTC to prevent double-firing | ||
| after system timezone changes, DST transitions, or config migrations. | ||
|
|
||
| This preserves relative ordering for legacy naive timestamps across | ||
| timezone changes and avoids false not-due results. | ||
| Naive timestamps from older versions are treated as UTC for safety. | ||
| """ | ||
| target_tz = _hermes_now().tzinfo | ||
| if dt.tzinfo is None: | ||
| local_tz = datetime.now().astimezone().tzinfo | ||
| return dt.replace(tzinfo=local_tz).astimezone(target_tz) | ||
| return dt.astimezone(target_tz) | ||
| # Treat legacy naive timestamps as UTC (migration safety) | ||
| return dt.replace(tzinfo=timezone.utc) | ||
| return dt.astimezone(timezone.utc) | ||
|
|
||
|
|
||
| def _migrate_timestamps_to_utc(jobs: List[Dict[str, Any]]) -> List[Dict[str, Any]]: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This helper is not called anywhere in the PR diff, so it never migrates persisted |
||
| """One-time migration: convert legacy naive timestamps to UTC. | ||
|
|
||
| Prevents double-firing after timezone changes, DST, or config migrations. | ||
| This is a non-destructive, idempotent migration. | ||
| """ | ||
| migrated = False | ||
| for job in jobs: | ||
| for field in ("last_run_at", "next_run_at"): | ||
| value = job.get(field) | ||
| if value and isinstance(value, str): | ||
| try: | ||
| dt = datetime.fromisoformat(value) | ||
| if dt.tzinfo is None: | ||
| job[field] = dt.replace(tzinfo=timezone.utc).isoformat() | ||
| migrated = True | ||
| except Exception: | ||
| pass | ||
|
|
||
| if migrated: | ||
| logger.info("Cron: migrated legacy naive timestamps to UTC (issue #28934)") | ||
|
|
||
| return jobs | ||
|
|
||
| def _recoverable_oneshot_run_at( | ||
| schedule: Dict[str, Any], | ||
| now: datetime, | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Normalizing an old-offset aware timestamp to UTC does not preserve the cron wall-clock intent from #28934. In the reported case,
21:00+10still becomes due at13:02+02after both sides are converted to UTC, so this should be handled in the due-job migration/repair path instead.