Skip to content

fix(scheduler): a cron's next fire time never goes backwards at a DST fall-back (#3940) - #3951

Merged
dolho merged 1 commit into
devfrom
fix/3940-cron-dst-fold
Oct 26, 2026
Merged

dolho merged 1 commit into
devfrom
fix/3940-cron-dst-fold

Conversation

@dolho

@dolho dolho commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Summary

At a DST fall-back, APScheduler 3.11.0's CronTrigger answers a fire time earlier than the previous one for a minute-stepped cron with hour='*' in a zone leaving summer time. For */30 in Europe/Lisbon it cycles 01:00Z, 00:30Z, 01:00Z, …, and it does the same with pytz and zoneinfo zones. Job._get_run_times then never returns: the scheduler pins a core and grows without bound. That took down two production hosts on 2026-10-25. The US fall-back is 2026-11-01 06:00 UTC.

Verified against the local stack

I reproduced the job loop (Job._get_run_times just after the fold, trigger built the way _add_job builds it) in two images, each with --network none and a 256 MiB limit. The live scheduler was not touched.

Image New York */30, 11-01 Lisbon */30, 10-25
live trinity-661-scheduler (APScheduler 3.11.0) never returns: one core at 100%, memory 32 → 54 → 81 → 117 MiB in 20 s never returns
this branch (3.11.3 + guard) <1 ms, 1 due run (06:00Z), next 06:30Z, 27 MiB <1 ms, 1 due run (01:00Z), next 01:30Z

The branch image's real SchedulerService._add_job registers MonotonicCronTrigger.

Changes

  • docker/scheduler/requirements.txt, docker/backend/Dockerfile, src/scheduler/pyproject.toml, tests/requirements-test.txt: the version pins
  • src/scheduler/cron_trigger.py (new) and src/scheduler/service.py: the guard and its wiring
  • tests/unit/test_3940_cron_dst_fold.py (new, 28 tests), tests/registry.json
  • tests/unit/test_1823_scheduler_permanent_classification.py: its 4 stubs patch MonotonicCronTrigger, the name _add_job now constructs
  • docs/memory/requirements/scheduling.md, plus a learnings fragment on the version-floor trap

Test Plan

⚠️ Shipping

Merging to dev is not enough: this has to be on main and deployed before 2026-11-01 06:00 UTC. Any instance still on 3.11.0 with an America/* */N schedule will spin at the US fall-back. Until it's deployed, the stopgap is to pause such schedules or move them to UTC.

Follow-ups, out of scope here: a mem_limit for scheduler in docker-compose.prod.yml, and a healthcheck that reflects the job loop (/health stayed green while the process spun).

Fixes #3940

🤖 Generated with Claude Code

… fall-back (#3940)

APScheduler 3.11.0's CronTrigger answered a fire time earlier than the
previous one at every fall-back for minute-stepped crons with hour='*'
in a zone leaving summer time (*/30 in Europe/Lisbon: 01:00Z, 00:30Z,
01:00Z, ...), with pytz and zoneinfo zones alike. Job._get_run_times
never returned: the scheduler pinned a core and grew without bound, and
two hosts ran out of memory on 2026-10-25. The US fall-back is
2026-11-01.

- APScheduler 3.11.0 -> 3.11.3 (fixed since 3.11.1) in the scheduler
  and backend images, kept equal per the #1823 parity guard.
- tests/requirements-test.txt pins apscheduler==3.11.3 exactly. Its
  >=3.11.0 floor let CI run a fixed release while the images ran the
  broken one, so a fold test could pass on a broken production.
- MonotonicCronTrigger (src/scheduler/cron_trigger.py), registered for
  every user schedule: an answer not after the previous fire time is
  retried from previous + 1s in UTC (on 3.11.0 that alone yields the
  correct schedule), else clamped to previous + 1 minute in UTC. Logged
  once per trigger; never returns None. Compared and added on UTC
  instants: same-zone datetimes compare by wall clock, and local
  arithmetic resets fold.
- The #1823 _add_job tests stub MonotonicCronTrigger, the name the
  service now constructs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@obasilakis obasilakis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approving. Mutation-checked: on APScheduler 3.11.0 all 10 fall-back cases go red; reverting the trigger registration, the UTC comparison, the UTC fallback or the retry each turns specific tests red. Ran the real job loop for Lisbon, London, Berlin, New York and Los Angeles under both tz libraries; fire times are monotonic and match 3.11.3.

Before merge:

  • Add the ui label. The PR touches docker/, so the high-risk lane needs frontend-e2e, which was skipped.
  • dev-ci is red (#3931); wait for green.
  • US fall-back is 2026-11-01 06:00 UTC, so this needs to reach main and deploy before then.

Non-blocking:

  • docs/memory/feature-flows/scheduling.md:728 still says apscheduler==3.11.0.
  • docs/memory/feature-flows/scheduler-service.md:180 and :747 describe _add_job registering a plain CronTrigger; should name MonotonicCronTrigger and #3940.
  • #3940 items 3 (scheduler memory limit) and 4 (healthcheck that sees the job loop) have no follow-up issue yet.
  • src/scheduler/cron_trigger.py: when the retry returns None because end_date has passed, the guard still synthesizes a fire one minute later. Unreachable today (user schedules set no end date); returning None there would close it.

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.

2 participants