Repository navigation
fix(sync): stop 503-looping status polls for legacy failed backfill jobs - #18954
Merged
Merged
Conversation
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]>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
GET /v2/sync-local-files/<job_id>returned 503 forever for backfill jobs that reachedfailed/partial_failurebefore thesync_dead_lettersledger existed. The status poll now writes the missing ledger record aspending, 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)
9340efe6d9(17:1x–17:27Z) put the status-poll ledger gate in front of every failed backfill job.backendtraffic back tobackend-646b131-36113877287-1at 18:00Z. Workers stay on9340efe.Root cause
The gate came from #18855 (
9e6b55b9ab, "pace historical uploads without losing retryable failures"). #18906 then addedrelease_backfill_slotafter the gate. Neither commit is in646b131, so this deploy was the first time they ran in prod. The gate treated a missingsync_dead_letters/{job_id}doc as "finalization is still in progress" and returned 503 withRetry-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_pendingget_sync_jobA job that went terminal before that code shipped has no record. The only other writer,
ensure_dead_letter_confirmedon 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 terminalfailed/partial_failurejob on thebackfilllane: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 responsejob_id/uid, or a status other thanpending/dead_letterpending/dead_letterdocattempt_countis not touched)job['uid'] == uidis 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 syncdefroute; 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_pendingand notensure_dead_letter_confirmedI traced every consumer of
sync_dead_lettersonorigin/main(git grep, whole repo):database/sync_jobs.py: terminal publishers write pending, thenconfirm_dead_letterafter a winning CAS. Write-only.routers/sync.pyrun_sync_jobterminal redelivery:ensure_dead_letter_confirmedbefore ACK. It touches only its ownjob_id.routers/sync.pyget_sync_job_status: this gate. Point read.docs/omi-core-selfheal-dashboard.md: log metricomi_sync_backfill_dead_letter, built from thesync_backfill_dead_letterevent that onlyconfirm_dead_letteremits.No worker, cron, Modal job, sweeper, or admin tool lists or queries the collection, and no replay path reads it. A new
pendingdoc is therefore inert: it is a durable diagnostic row and nothing more.ensure_dead_letter_confirmedwould callconfirm_dead_letter, which emitssync_backfill_dead_letter. That would inflate the confirmed-dead-letter metric with 125+ historical jobs in one burst.record_dead_letter_pendingemits 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 nosync_backfill_dead_letterevent is emitted.test_poll_legacy_backfill_bounds_raw_reason_code: a raw reason code is mapped tounknown.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 withRetry-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.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:Result: 5 files, 0 failures (46 + 94 + 180 + 9 + 43 passed).
scripts/scan_async_blockers.py --dirs routers utilsreports 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