Skip to content

fix(cron): out-of-order overlapping slot completion can regress schedule and duplicate execution #962

Closed

Description

@PierrunoYT

Version / branch / commit

main at 6fe0d1edcdbadea6d566800fbcca739c1cd54ea1

OS and environment

Cross-process cron store behavior; confirmed by source/call-path analysis on Linux with Go 1.26.6. The stale-state defect is platform-independent.

Steps to reproduce

Use a schedule with adjacent slots and two scheduler processes:

  1. Scheduler A reads and claims slot N. claimFire advances the stored NextRunAt to N+1, then A begins a long zero exec run.
  2. At N+1, scheduler B reads the updated job, claims slot N+1, and advances stored state to N+2 while A is still running.
  3. Let B and A complete out of order.
  4. Inspect the job state and run records.

fireJob increments the stale job copy loaded before execution, then its completion mutation assigns:

current.FireCount = job.FireCount
current.NextRunAt = job.NextRunAt

at internal/cli/cron_run.go:206-220. The per-job lock makes each mutation atomic but does not make values derived from an earlier slot current.

Depending on completion order, slot N's completion can move NextRunAt from N+2 back to N+1, allowing another scheduler to claim N+1 again. Both completions can also persist FireCount == 1 rather than 2.

The existing concurrent claim test (internal/cli/cron_claim_test.go:12-41) covers two schedulers racing for the same slot, not adjacent slots completing out of order.

Expected behavior

  • Each scheduled slot is claimed at most once.
  • NextRunAt advances monotonically and never moves backward when older work completes.
  • FireCount reflects every completed fire.

Actual behavior

Completion of an older slot can overwrite newer schedule/accounting state, exposing an already-claimed slot as due and causing duplicate automated actions and lost fire counts.

Recommended fix

Persist a claimed slot/run identity. At completion:

  • increment current.FireCount rather than assigning a stale count;
  • update schedule state with a slot-aware CAS or monotonic guard;
  • never move NextRunAt backward;
  • add adjacent-slot/out-of-order completion tests across independent stores/process locks.

Activity

  1. PierrunoYT commented on Aug 25, 2026

    @PierrunoYT
    ContributorAuthor

    PierrunoYT prepared and independently validated a focused fix locally while awaiting maintainer triage; no branch or PR has been published because this issue does not yet have issue-approved.

    Regression proof on exact base 6fe0d1edcdbadea6d566800fbcca739c1cd54ea1, with only the new test applied:

    go test ./internal/cli -run '^TestFireJobOutOfOrderAdjacentSlotsDoesNotRegress$' -count=1
    FireCount=1 NextRunAt=2026-06-09 09:01:00 +0000 UTC; want FireCount=2 NextRunAt=2026-06-09 09:02:00 +0000 UTC
    

    The focused change makes the claimed next-run value a completion CAS token, increments the current lock-protected fire count, and prevents an older completion from overwriting a newer adjacent-slot claim. The regression passes with the fix.

    Validation completed successfully:

    • make fmt-check
    • go vet ./...
    • go test ./...
    • make test (full race suite)
    • go run ./cmd/zero-release build
    • go run ./cmd/zero-release smoke
    • make lint-static (0 issues.)
    • make vulncheck (No vulnerabilities found.)
    • git diff HEAD --check

    If maintainers approve the issue for outside contribution, the focused commit is ready to publish as a PR.

  2. added
    bug
    issue-approved
    on Sep 12, 2026
  3. Vasanthdev2004 commented on Sep 12, 2026

    @Vasanthdev2004
    Collaborator

    Reopening this, and applying issue-approved so it is not blocked on the label again.

    @PierrunoYT you closed your own report on 2026-08-26 with an empty comment, after saying upthread that you were holding the branch back because this issue had no issue-approved label. The defect you described is still on main verbatim:

    internal/cli/cron_run.go:214   current.FireCount = job.FireCount
    internal/cli/cron_run.go:215   current.NextRunAt = job.NextRunAt
    

    Both assignments happen inside store.Mutate with no slot identity, no compare-and-swap and no monotonic guard, so a slot that finishes out of order still writes its stale view over a newer one. internal/cli/cron_claim_test.go covers same-slot racing through TestFireJobClaimPreventsDoubleFire and nothing covers the overlapping out-of-order case.

    Closing a live defect because the process gate had not been applied is the wrong outcome, and the gate was mine to apply. It is applied now, so a PR implementing this can merge on review. If you still have that branch, please open it; if you would rather not, say so and it goes in the pool with the label already on it.

    Found in a coverage audit that was looking for issues whose linked PR had closed, which this one would have escaped: it has no PR at all.

  4. Vasanthdev2004 commented on Sep 26, 2026

    @Vasanthdev2004
    Collaborator

    @PierrunoYT two weeks since the label went on. If the branch you validated is still around, open it as it is and I'll review it. If not, or if you'd rather not, I'll write the fix from Monday, Sep 29, so it doesn't sit any longer. Either is fine, just say which here.

  5. PierrunoYT commented on Sep 26, 2026

    @PierrunoYT
    ContributorAuthor

    Workin on it!

  6. added a commit that references this issue on Oct 12, 2026
    2eb9d94
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugissue-approved

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions