Skip to content

fix(backend): a batch purge that loses the account gate to a peer hands off instead of failing its task - #25393

Merged
kodjima33 merged 3 commits into
mainfrom
fix/batch-purge-task-500
Nov 15, 2026
Merged

kodjima33 merged 3 commits into
mainfrom
fix/batch-purge-task-500

Conversation

@kodjima33

@kodjima33 kodjima33 commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

On Cloud Run backend-sync, POST /v1/conversations/deletion-batch-purges/run has answered HTTP 500 1,461 times since 2026-11-12T18:05Z. Per day: 53 on 11-12, 1,179 on 11-13, 85 on 11-14 and 144 on 11-15 up to about 11:07Z. The first 500 came an hour after backend-sync-83c5eaa (2026-11-12T17:04Z), the first revision to serve the route from #25124. The 11-13 peak was before #25299 (queue switch) and #25298, so neither of those caused it.

The handler did log every failure, but only the exception class, and the line is not attached to the request trace. Across the WARNING lines (kept in full), 1,293 of 1,462 failures are DestructiveOperationInProgress. The rest are scattered transient Firestore errors (DeadlineExceeded, ServiceUnavailable, ValueError commit contention, ReadTimeout).

Root cause

A purge unit waits UNIT_GATE_WAIT_SECONDS (30 s) for the account's destructive gate. A same-account multi-delete arms one obligation per conversation, and the reconciler dispatches all of them at once. They then queue on that one gate together with the account's accepted-delete erasures and foreground deletes. A unit that waits past 30 s raises DestructiveOperationInProgress. run_job treated that as a hard failure: last_outcome=failed, a 600 s redrive, and a 500 that also burns the queue's 5 retries. Most 500s therefore take about 30 s, the wait budget. #25197 added the busy outcome for erasures (run_erasure_job), but batch purges never got it.

Fix

  • run_job maps a unit that lost the gate to a same-kind peer (_lost_gate_to_peer, the helper run_erasure_job already uses) to the existing busy outcome. That means a 200, the lease released, and a redrive in BUSY_RETRY_SECONDS.
  • busy is safe because a unit acquires the gate before erasing anything, so the losing unit erased nothing. The scan position and segment records were already checkpointed under the run's ownership, and the next generation resumes from them.
  • Privacy invariants from fix(backend): conversation deletes wait for the account gate and return fast #25124 are unchanged:
    • An account wipe, retention or audio-deletion gate is never a peer. It still fails the run (tested for each kind).
    • A legal hold still ends as held.
    • Every erasure is still fenced by its unit's authority.
    • Run and unit deadlines are unchanged.
    • A stuck remote-erasure gate is still settled by settle() before each unit, and is still reported by event=destructive_gate_remote_unresolved.
  • The handler's failure line now includes the obligation kind, generation, exception type and sanitize(str(e)), so every 500 states its reason.
  • Re-pinned the run_job BODY_DIGEST. It adds no query: its only stream is still segment_records, and the gate check is the existing point read.

Blast radius (read-only)

  • 301 distinct obligations failed at least one attempt since 11-12, 202 of them with DestructiveOperationInProgress.
  • None is stuck: all 301 are gone from conversation_batch_purge_jobs and conversation_erasure_jobs, so they completed.
  • Success lines look missing for 237 of them only because app stdout INFO is kept at 10% by the _Default sink exclusion app-stdout-info-sample90. 2xx request logs are also kept at 10% (cost-audit-cloud-run-success-sample).
  • Live right now: 19 batch-purge and 5 erasure obligations, all from one account and all under 1 h old. 18 tasks are queued on conversation-erasure.
  • The roughly 300 403s a day on this path are the reconciler's unauthenticated readiness probe (conversation_batch_purge_consumer_ready), which is expected.

Not in this PR

  • /v2/audio-merge-jobs/run 500s on 11-15 (171 so far) have a different cause. All of them are stamped playback source could not be read (utils/other/storage.py, strict-source download failure), across 7 conversations, and each fails in under 1 s. They have nothing to do with the gate.
  • The reconciler still dispatches every due batch-purge obligation of one account at once. A per-account spread like MAX_ERASURES_PER_ACCOUNT would cut how many gate waiters there are. With this fix those waiters hand off with busy instead of failing, so the spread is a follow-up.

Tests

  • test_unit_losing_its_gate_wait_to_a_same_account_peer_hands_off_busy_and_resumes reproduces the prod failure. On main it raises DestructiveOperationInProgress: another destructive operation owns the account gate. With the fix it returns busy, erases nothing without the gate, keeps progress, and the next generation purges.
  • test_unit_blocked_by_another_destructive_kind_still_fails_the_run[account_deletion|retention_cleanup|audio_conversation_deletion].
  • test_task_handler_maps_run_outcomes_to_queue_retry gains a busy case (200). test_task_handler_logs_why_a_run_failed checks the new failure line and that it is sanitized.
  • Purge, gate and accept suites pass (111 tests): test_conversation_batch_purge_{resumable,deadlines,authority}.py, test_conversation_delete_gate_wait.py, test_conversation_delete_accept.py. test_firestore_query_shapes.py also passes. make preflight was run.

Product invariants affected

none

Failure class (fixes)

Failure-Class: FC-user-intent-queued-behind-account-exclusive-work

🤖 Generated with Claude Code

View guided diff Turn on auto-fix

…ds off instead of failing its task

A purge unit waits 30 s for the account's destructive gate. When a peer of
the same kind (another of the account's purges, an accepted delete's
erasure, a user delete) held it longer, run_job raised
DestructiveOperationInProgress and the Cloud Tasks handler answered 500:
1,293 of 1,462 purge task failures on backend-sync since 2026-11-12.
The run now records the existing busy outcome (redriven in 30 s), as
run_erasure_job already does. The unit erased nothing because it never
acquired the gate, and the scan position is already checkpointed. Legal
holds, account wipes and other gate kinds still stop the run as before.

The handler's failure line now names the obligation kind, generation,
error type and sanitized message.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kodjima33 kodjima33 added the mux-proposal Mobile UX improver (MUX) proposal; waits for Nik's ship label Nov 15, 2026
@kodjima33
kodjima33 merged commit bff7120 into main Nov 15, 2026
62 checks passed
@kodjima33
kodjima33 deleted the fix/batch-purge-task-500 branch November 15, 2026 17:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mux-proposal Mobile UX improver (MUX) proposal; waits for Nik's ship

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant