Repository navigation
fix(sync): keep job reads behind terminal ownership - #19379
Conversation
Reproduce five live-reader failures, including completed-result clobbering. Keep lookups read-only and let the existing run-lease finalizer own recovery. The real-route regression matrix preserves legacy, active, inline, standby and abandoned-task semantics. Verified: 379 focused backend tests; 0 pyright errors; local Uvicorn + curl with fake Redis and outbound network blocked; 32 repository preflight checks. Refs BasedHardware#19373 Failure-Class: FC-split-mutation-authority
|
Verified the ownership violation this fixes: on main, get_sync_job() in backend/database/sync_jobs.py performed an unconditional stale-state Redis write (status=failed + dead-letter pending + r.set) on every read and for every dispatch mode, before the authenticated route could enforce anything. The reader could fail a job whose worker still held an unexpired run lease and overwrite a concurrently written 'completed' result. The fix direction is correct and well-executed. File-by-file:
One product/ops tradeoff for a maintainer to sign off on: with the read-side self-heal gone, a dead inline coordinator's job stays 'processing' until the 24h job TTL / client WAL reconcile instead of healing in ~10 minutes on poll. That reverses the dispatch-agnostic self-heal added on 2026-07-23 for the 'Uploaded - processing on Omi for 12+ hours' reports (#10033). The runbook already frames inline recovery as a deliberately degraded durable-retry path (lease renewal is not a terminal-write fence, executor leaves can outlive coordinator cancellation), so this looks intentional and the correctness argument is sound — but the recovery-latency change for inline workers deserves an explicit maintainer decision, so I'm leaving this for human review rather than sign-off. Housekeeping: the branch currently conflicts with main (#19362 and #19421 both touched backend/database/sync_jobs.py after this head) — a rebase will be needed before merge, and the migrated callers are worth re-checking against the new sequencer code after it. Thanks @aryanorastar — the issue report (#19373) with failing-on-main regressions and the ownership-preserving test design made this a very reviewable change. [AI-generated review posted on behalf of the maintainers] |
…audit # Conflicts: # backend/database/sync_jobs.py
|
Conflicts resolved on One file conflicted, What the two sides were doingSince this branch's base, That is a subset of this PR's fix, not an alternative to it. Regressions #1 and #5 in the description — a live legacy worker marked
A resolution I tried first and reverted
That broke That was the signal I had over-reached: consolidating #19362's brand-new reader rewires its read accounting, which is refactoring someone else's fresh work inside a merge. The four Worth a follow-up: there are now two read-only readers with identical behaviour. Collapsing them is the right end state, but it wants its own PR that updates the sequencer fixtures deliberately rather than as merge fallout. Verified on the merged head
Diff scope is the PR's own 9-file list, byte-for-byte — checked by diffing the merged file list against the PR's. |
|
The reader consolidation mentioned above is now #19437, opened as a draft stacked on this PR. It removes Nothing on this branch changes; merging this PR first is what unblocks #19437. |
|
The housekeeping item in the review above is done — it was written against
The recovery-latency tradeoff for a dead inline coordinator is untouched and still the maintainer's call; nothing in the merge changes it. |
kodjima33
left a comment
There was a problem hiding this comment.
Confirmed the read-side stale-write race is still live on main (get_sync_job unconditionally flips processing->failed on read); this PR makes it read-only and moves recovery behind the lease-owning finalizer. 379 backend tests + new HTTP ownership regressions, CI green. Trusted contributor, confidence 5/5. Merging.
…job (#19437) * fix(sync): keep job reads behind terminal ownership Reproduce five live-reader failures, including completed-result clobbering. Keep lookups read-only and let the existing run-lease finalizer own recovery. The real-route regression matrix preserves legacy, active, inline, standby and abandoned-task semantics. Verified: 379 focused backend tests; 0 pyright errors; local Uvicorn + curl with fake Redis and outbound network blocked; 32 repository preflight checks. Refs #19373 Failure-Class: FC-split-mutation-authority * refactor(sync): collapse the two read-only job readers into get_sync_job Stacked on #19379. Once that change makes get_sync_job read-only, main's get_raw_sync_job (added by #19362 for the per-UID sequencer) is the same function: both read the job key, return None on a missing, corrupt or non-dict blob, and write nothing. Checked structurally rather than by eye -- the two bodies are AST-identical modulo the local variable name. Two copies of one read-only primitive is exactly how read-side mutation creeps back into one of them, so this keeps a single reader. - database/sync_jobs.py: remove get_raw_sync_job and _read_raw_sync_job. - routers/sync.py, utils/sync/uid_sequencer.py: point the five callers at get_sync_job and drop the now-duplicate import name. - Tests that patched the removed name now patch get_sync_job. One fixture needed a deliberate change rather than a rename. test_sync_task_persistence_fence_terminalizes_backfill_and_releases_its_slot runs a backfill task with no sequencer epoch, which first reads its own job to decide whether the UID sequencer should adopt it. Its harness stubs the whole database.sync_jobs module with a bare MagicMock, so before this change that read returned an auto-mock whose status never equalled 'queued' and the migration branch was skipped by accident. After consolidation the read consumes from the test's explicit get_sync_job sequence, sees the real 'queued' job, and migrates. Rather than feed it a sentinel, the test now states its scenario: the legacy direct path with the sequencer disabled, no owner and nothing pending, and the migration read gets its own entry ahead of the processor's three. Flipping the sequencer on makes the test fail, so the gate is load-bearing rather than decorative. Verified: every test_sync_* suite, 952 passed and 0 failed; typecheck 0 errors. No behaviour change. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(sync): finish consolidating read-only job lookup --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
Summary
Fixes #19373. Reading an upload's status could turn a live worker's job into a failure or overwrite its completed result.
get_sync_job()performed an unconditional stale-state Redis write before the authenticated status route could enforce its existing run-lease ownership checks.This fixes the backend ownership boundary; it does not change mobile or hardware code.
What changed
get_sync_joba read-only lookup. A stale progress timestamp is not permission to publish a terminal result.Reproduction and verification
On unchanged
main(ab7f480795), the initial five regressions all failed:failedby a status poll.After the fix:
processingand never-startedqueuedCloud Tasks still recover withsync_worker_stale/sync_dispatch_lost.BACKEND_UNIT_TEST_FILE_LIST=/tmp/omi-19373-tests.txt bash backend/test.sh.bash backend/scripts/typecheck.sh: 0 errors (existing warnings remain).127.0.0.1:10273exercises the actual status endpoint: a lease-owned worker and inline worker remainprocessing; an abandoned worker and lost dispatch become reasoned failures through the finalizer; a completed job remains completed with its result. Fake Redis/identity only; outbound network blocked. No production requests, user recordings, or quota changes.OMI_PR_BODY_FILE=/tmp/omi-19373-pr-body.md make preflight: 32 checks passed. The history-dependent failure-class recurrence check explicitly skips on this shallow checkout; CI's full history remains authoritative.Why some existing tests change
The old database-reader tests explicitly expected blind read-side failure writes. That contradicts the existing ownership and inline-recovery contract in
backend/docs/runbooks/sync-two-lane.md, and the already-present route checks. The route-only tests mockedget_sync_job, hiding the contradictory implementations. The new HTTP composition tests execute both, including the positive recovery cases, rather than merely rewriting expected status values.Scope and limitations
get_sync_jobif this lands first.Product invariants affected
none — preserves the existing sync terminal-ownership contract; no locked product policy is changed.
Failure class
Failure-Class: FC-split-mutation-authority
The shared primitive is the read-only job lookup plus the already-existing lease-owning finalizer. The new behavioral guard composes those production paths; no source-string checker or competing recovery owner is added.