Skip to content

fix(sync): stop 503-looping status polls for legacy failed backfill jobs - #18954

Merged
Git-on-my-level merged 1 commit into
mainfrom
fix/sync-status-legacy-dead-letter-503
Sep 25, 2026
Merged

Git-on-my-level merged 1 commit into
mainfrom
fix/sync-status-legacy-dead-letter-503

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Sep 25, 2026 •

Copy link
Copy Markdown
Member

Summary

GET /v2/sync-local-files/<job_id> returned 503 forever for backfill jobs that reached failed/partial_failure before the sync_dead_letters ledger existed. The status poll now writes the missing ledger record as pending, releases the backfill slot, and returns the normal terminal response. A 503 is still returned when the ledger read or write fails, or when the record belongs to a different job or uid.

Incident (prod, 2026-09-25)

  • The prod deploy of 9340efe6d9 (17:1x–17:27Z) put the status-poll ledger gate in front of every failed backfill job.
  • Over about 35 minutes: 1,030 × 503 from 125 distinct job ids, which was 43% of status polls. The same job returned 200 at 16:53Z on the previous revision.
  • The coordinator rolled prod backend traffic back to backend-646b131-36113877287-1 at 18:00Z. Workers stay on 9340efe.

Root cause

The gate came from #18855 (9e6b55b9ab, "pace historical uploads without losing retryable failures"). #18906 then added release_backfill_slot after the gate. Neither commit is in 646b131, so this deploy was the first time they ran in prod. The gate treated a missing sync_dead_letters/{job_id} doc as "finalization is still in progress" and returned 503 with Retry-After: 10.

That reasoning holds for jobs written by current code, because every current terminal publisher writes the pending record before Redis turns terminal:

  • mark_job_failed / finalize_sync_job / the fenced variants, via _backfill_dead_letter_pending
  • the stale self-heal inside get_sync_job

A job that went terminal before that code shipped has no record. The only other writer, ensure_dead_letter_confirmed on Cloud Tasks terminal redelivery, runs only while a task is still being redelivered. Legacy jobs' tasks were ACKed long ago. So no record would ever appear, and those jobs would poll 503 until their 24h Redis TTL expired.

Fix

In get_sync_job_status (backend/routers/sync.py), for a terminal failed/partial_failure job on the backfill lane:

Ledger state Before After
Read raises 503 503 (unchanged; a failed read is never treated as a missing record)
Missing doc 503 forever record_dead_letter_pending(job_id, uid, conversation_id, failure_code=dead_letter_failure_code(reason_code)), then release the slot and return the terminal response
Missing doc and the write raises 503 503; the slot is not released
Doc exists with a different job_id/uid, or a status other than pending/dead_letter 503 503 (unchanged, fails closed)
Existing pending/dead_letter doc 200 terminal 200 terminal (unchanged; no rewrite and attempt_count is not touched)

job['uid'] == uid is already enforced by the 403 check above, so the record is written under the job's own uid. The Firestore calls run directly, like every other call in this sync def route; FastAPI runs the route in its threadpool. The four identical 503 constructions in the route now share one _sync_finalization_retrying() helper.

Once the record exists, later polls read it and write nothing more. The path also retires itself: legacy jobs drop out of Redis within their 24h TTL, and each one causes at most one Firestore write.

Why record_dead_letter_pending and not ensure_dead_letter_confirmed

I traced every consumer of sync_dead_letters on origin/main (git grep, whole repo):

  1. database/sync_jobs.py: terminal publishers write pending, then confirm_dead_letter after a winning CAS. Write-only.
  2. routers/sync.py run_sync_job terminal redelivery: ensure_dead_letter_confirmed before ACK. It touches only its own job_id.
  3. routers/sync.py get_sync_job_status: this gate. Point read.
  4. docs/omi-core-selfheal-dashboard.md: log metric omi_sync_backfill_dead_letter, built from the sync_backfill_dead_letter event that only confirm_dead_letter emits.

No worker, cron, Modal job, sweeper, or admin tool lists or queries the collection, and no replay path reads it. A new pending doc is therefore inert: it is a durable diagnostic row and nothing more.

ensure_dead_letter_confirmed would call confirm_dead_letter, which emits sync_backfill_dead_letter. That would inflate the confirmed-dead-letter metric with 125+ historical jobs in one burst. record_dead_letter_pending emits nothing and keeps the doc honest: it records that a terminal failure was seen, without claiming the terminal delivery path confirmed it. If a Cloud Task for the job is still being redelivered, it confirms the doc through the normal path.

Tests

backend/tests/unit/test_sync_dead_letters.py. The new poll tests run the real ledger module against the in-memory Firestore fake.

  • test_poll_legacy_backfill_without_ledger_records_pending_and_serves_terminal[failed|partial_failure]: the response is the terminal status the client retries on, a pending doc is written with the job's uid, conversation id, and bounded failure code (attempt_count == 1), the slot is released, and no sync_backfill_dead_letter event is emitted.
  • test_poll_legacy_backfill_bounds_raw_reason_code: a raw reason code is mapped to unknown.
  • test_poll_legacy_backfill_second_poll_reads_recorded_doc: a second poll does not write again.
  • test_poll_legacy_backfill_record_write_error_is_503: 503 with Retry-After, no doc, slot not released.
  • test_poll_ledger_read_error_fails_closed: 503, and now also asserts no write and no slot release.
  • test_poll_failed_backfill_with_mismatched_ledger_is_503[wrong_uid|wrong_job_id|wrong_status|missing_job_id]: 503, and now also asserts no write and no slot release.
  • test_poll_existing_ledger_doc_is_not_rewritten[pending|dead_letter]: behaviour is unchanged and the doc is byte-identical.
  • The two tests that encoded "missing doc → 503" (test_poll_failed_backfill_without_ledger_is_503_not_ack, ..._does_not_release_slot) are replaced by the cases above.

Command, from backend/ in a Python 3.11.15 uv venv:

BACKEND_UNIT_TEST_FILE_LIST=<list> bash test.sh
# list: tests/unit/test_sync_dead_letters.py tests/unit/test_sync_cloud_tasks.py
#       tests/unit/test_sync_silent_failure.py tests/unit/test_sync_v2.py
#       tests/unit/test_sync_backfill_inflight_limit.py

Result: 5 files, 0 failures (46 + 94 + 180 + 9 + 43 passed). scripts/scan_async_blockers.py --dirs routers utils reports 0 selected findings.

This was measured at the unit layer only. I did not run it against a live backend, and I made no prod reads or writes.

Product invariants affected

none

Failure-Class: FC-schema-adopted-only-at-resource-creation

The fail-closed ledger gate (#18855) was enforced on every terminal backfill poll, but only jobs created after it shipped had the record. There was no adoption path for jobs that already existed, so each poll failed closed, forever, behind a "retrying" 503 that looked transient. This PR adds the idempotent adoption on the gate's own missing-record branch. It keeps tests for adoption, for no-op on an existing record, and for a failed write still failing closed.

Line count

Line-Count-Exception: backend/routers/sync.py | 2163 -> 2182 | prod hotfix: legacy-record adoption branch on the status poll; the four duplicated 503 constructions were folded into one helper to offset part of it. Splitting sync.py is out of scope for an incident fix.

🤖 Generated with Claude Code

Review in cubic

Backfill jobs that reached failed/partial_failure before the dead-letter
ledger shipped (#18855) have no sync_dead_letters doc and no publisher left
to write one, so GET /v2/sync-local-files/<job_id> returned 503 forever.
Record the missing doc as pending on read (no confirmed-cohort event, no
replay consumer), then release the backfill slot and serve the terminal
status. 503 remains only for a failed ledger read/write or an identity
mismatch.

Failure-Class: FC-schema-adopted-only-at-resource-creation

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Git-on-my-level
Git-on-my-level merged commit 3e23ff6 into main Sep 25, 2026
64 checks passed
@Git-on-my-level
Git-on-my-level deleted the fix/sync-status-legacy-dead-letter-503 branch September 25, 2026 18:22
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