Skip to content

fix(server): retry only failed repositories, with backoff - #194

Merged
sweepies merged 3 commits into
mainfrom
fix/server-retry-backoff
Nov 6, 2026
Merged

sweepies merged 3 commits into
mainfrom
fix/server-retry-backoff

Conversation

@sweepies

@sweepies sweepies commented Nov 6, 2026 •

Copy link
Copy Markdown
Owner

Refs #193. This fixes the retry storm. The steady memory growth in the same issue is being investigated separately.

Cause

Work is claimed one row per repository, but finish_work took a single success flag for the whole batch. One broken repository (a private npm package with no .npmrc) therefore requeued all six, and every failure waited a fixed 60 seconds, forever. Each attempt also added another retry row to server_work_causes, which is only cleared on success.

Changes

  • Per-repository results. finish_work takes a Finish for each claim: Done, Failed or Requeue. The server derives it from each repository's report, branch and PR errors. A batch that fails before any repository has a result retries every claim.
  • Backoff. Each failed repository waits 1, 2, 4 … minutes, capped at an hour. A new failures column (migration 009) holds the count; a success resets it.
  • New triggers aren't delayed. A push or other event during the backoff brings the retry forward to the coalescing window (available_at = MIN(…) on enqueue), so a fix runs promptly. Work that arrives during a failed run also waits only for coalescing.
  • Moved default branch. This is still requeued after a minute and doesn't count as a failure. If the same run also failed, the failure wins and the repository backs off.
  • Retry causes. A retry cause is written once per generation rather than once per attempt.
  • Logs. The batch logs list the retried and requeued repositories.

Docs: the queue section of guide/server.mdx.

Tests

  • Queue: backoff timing and cap, reset on success, a trigger pulling a retry forward, work arriving mid-run, requeue not counting as a failure, and causes not piling up.
  • Server: a batch with one broken and one healthy repository requeues only the broken one.

View guided diff Turn on auto-fix

One failing repository requeued its whole batch every 60 seconds forever:
finish_work took one success flag for every claim, and failures always
waited a fixed minute.

- Each claimed repository finishes on its own result; only failed ones
  retry.
- Retries back off from a minute, doubling per consecutive failure up to an
  hour (a new `failures` column, migration 009); success resets it.
- A new trigger brings a backed-off retry forward to the coalescing window,
  and work that arrived during a failed run waits only for coalescing.
- A moved default branch still requeues after a minute without counting as
  a failure.
- Retry causes are recorded once per generation instead of once per attempt.

Refs #193
@olivia-colomar-app

olivia-colomar-app Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

¡Oye! Sombra here — I poked around your retry queue. Nice work on the generation fencing; it's tighter than Lumerico's firewall. A few cracks below, mostly small ones. 😉

Scope: fix(server): retry only failed repositories, with backoff @ 8bb2e13. Verified cargo check and cargo test -p patchula-cache -p patchula-server — all green.

Resolved in e83a5b2 — all findings below were addressed; see the follow-up comment.

P0 / P1 / P2

Nothing. The core logic held up: generation fencing distinguished in-flight work from newly queued work, MIN(available_at) correctly brought backed-off retries forward while preserving the first event's coalescing window, per-repo failure matching was case-normalized, and the deduped retry cause kept server_work_causes bounded across repeated failures of one generation.

P3

  • Interrupted runs never count as failures. finish_work was the only writer of failures; recover_work cleared claimed but left failures/available_at untouched, so a run that died with the process came back as an immediate retry with no backoff growth. Fixed: recover_work(now) now counts each leftover claim as a failure and backs it off; shutdowns release claims via release_work without counting.

P4

  • No jitter — every claim in one finish_work got the same available_at. Fixed: retry_delay_ms adds a deterministic FNV-1a spread keyed on installation+repo.
  • Log fields may get large. Fixed: retried/requeued capped at 20 names + … N more.
  • Coverage gap. Mostly fixed: new tests cover the recover↔backoff interplay and success resetting failures on a surviving newer generation.

Nice touches

  • needs_requeue mapped to per-repository requeued instead of a batch-wide flag.
  • One retry cause per generation via WHERE NOT EXISTS.
  • Docs in server.mdx updated in the same commit. Buen trabajo.

— Sombra 🌐

Comment thread crates/cache/src/queue.rs Resolved
Comment thread crates/cache/src/queue.rs Resolved
Comment thread crates/server/src/runtime.rs Resolved
- A claim left over at start is a run that died with the process: it counts
  as a failure and backs off. A shutdown releases its claims without one
  (release_work), so deploys cost nothing.
- Each repository's retry delay adds a fixed share of up to a tenth of the
  backoff, so repositories that failed together do not retry in step.
- Batch log lines list at most 20 repository names.
- Tests for both, and for a success resetting the failures of work that
  arrived during it.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
count-patchula 7403ffe Commit Preview URL

Branch Preview URL
Nov 06 2026, 09:46 AM

Comment thread crates/server/src/runtime.rs Resolved
Comment thread crates/cache/src/queue.rs Resolved

@olivia-colomar-app olivia-colomar-app Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Volví — Sombra again. You sealed the cracks I found, so I went looking for new ones in e83a5b2. The recover/release split is clean; a couple of seams remain where the crash-loop protection can still leak.

Scope: follow-up on e83a5b2 (back off runs that die with the server, and spread retries). Re-ran cargo test --locked -p patchula-cache -p patchula-server — all green.

Resolved since 8bb2e13

  • P3 — interrupted runs: recover_work(now) now counts a claim left over at startup as a failure and backs it off; release_work handles the shutdown path without counting. Bien.
  • P4 — no jitter: retry_delay_ms adds a deterministic FNV-1a spread keyed on installation+repo, ≤ a tenth of the backoff — synchronized failures stay spread through every doubling.
  • P4 — log list size: retried/requeued capped at 20 names plus … N more; counts remain in batch finished.
  • P4 — coverage: new tests cover the recover↔backoff interplay, shutdown release without a failure, jitter spread, and a success resetting failures on a surviving newer generation.

P0 / P1 / P2

Nothing.

P3

  • release_work also runs on the failure exit, so a panic loop escapes the new backoff. In run_until the release is unconditional — it runs whether the loop ended on a signal or on failure (a worker task error or panic, the HTTP server dying). With unwind (there's no panic = "abort" profile), a repository whose run reliably panics the worker → run_until returns Err → its claim is released with no failure counted → the supervisor restarts → the same repository is claimed immediately → panic again. The new protection only engages when the claim survives to the next startup (OOM kill, SIGKILL). See the inline comment on the call site.

P4

  • recover_work applies the full backoff to newer-generation work. If a trigger arrived during the doomed run (generation > claimed), finish_work would give it only coalesce_ms; recover_work unconditionally writes available_at = now + retry_delay_ms. A push that landed seconds before the crash can wait an hour-plus — the one case where "a new event runs it after the usual coalescing window" doesn't hold. Inline comment has the details.
  • release_work hardcodes the attribution JSON while recover_work/finish_work serialize Attribution::reason("retry"). Identical today; silent drift if the shape changes.

Nice touches

  • Splitting recover vs. release makes "did it die or was it stopped" an explicit, testable distinction — and a_shutdown_releases_its_claims_without_counting_a_failure documents it.
  • FNV-1a keyed on installation+repo: deterministic across processes and releases, no wall-clock randomness needed.
  • The … N more suffix lives in the same field as the names, so counts and the truncated list stay together.

— Sombra 🌐

- When the server stops on a failure, such as a panicking batch, its runs
  count as failed and back off; only a clean shutdown releases them freely.
- Recovered work that a trigger renewed during the run keeps its time.
- release_work serialises its retry cause like the other paths.
Comment thread crates/cache/src/queue.rs Resolved

@olivia-colomar-app olivia-colomar-app Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Vuelvo, and the vault is tighter this time. Both second-round findings landed cleanly in 7403ffe — I ran cargo test -p patchula-cache -p patchula-server on the live head and everything is green. CI is nearly all pass; a few unit jobs still running at post time.

Scope: follow-up covering 7403ffe (live head). All threads from both rounds are resolved.

Second-round findings — confirmed fixed

  • P3 — failure exits released claims for free. run_until now calls recover_work when failure.is_some(), so a worker panic or a dead HTTP server counts in-flight claims as failures and backs them off, while the signal path still release_works without counting. Exactly the split needed — a repository that panics the worker can no longer crash-loop at restart rate.
  • P4 — work queued during a doomed run inherited the crash's full backoff. recover_work now applies available_at = CASE WHEN generation=claimed THEN now+delay ELSE available_at END, so a newer generation keeps its own time. Covered by work_that_arrived_before_a_crash_keeps_its_time_but_the_crash_counts; the generation=claimed branch is covered by restart_recovers_claimed_work_and_delivery_ids.

New

P4

  • Crash-recovered follow-up work is due immediately, not after coalescing. Small asymmetry with finish_work, marked inline. Cost is at most one coalescing window; arguably the better choice anyway.

Nice touches

  • release_work is a real transaction now, and the retry attribution goes through Attribution::reason instead of a hand-typed JSON literal.
  • server.mdx updated in the same commit again — the docs never drift. Consistent like a metronome.

Nothing blocking from me. Los hilos están limpios. 🌐

— Sombra

@sweepies
sweepies merged commit 1b6f031 into main Nov 6, 2026
12 checks passed
@sweepies
sweepies deleted the fix/server-retry-backoff branch November 6, 2026 09:54
@sweepies sweepies mentioned this pull request Nov 6, 2026
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.

1 participant