fix(android-core): recover stale upload batches, serialize runs, retry transient 5xx #1341
Loading…
Reference in a new issue
No description provided.
Delete branch "fix/upload-engine"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Three correctness fixes in the Android upload engine (
:coreonly —apps/android/app,apps/web,apps/apiuntouched).1. Session lifecycle (stale-batch recovery)
The engine reused a persisted open batch (
upload_batch) with no validation. When the server answered 403Not the owner of this upload session(session created under the API credential but presented with cookie auth, or vice versa), the run aborted, the batch survived restart, and every retry looped on the same rejected session forever.GET /api/upload-sessions/{id}probe.SessionGoneExceptionnow; 403 newly mapped inUploadSessionClient) or terminal status (completed*/cancelled/failed/expired) → discard batch, create a fresh session.(upload_session_id, client_file_key)).2. Single-flight guard
Periodic (
memories-backup-periodic) and immediate (memories-backup-now) work have distinct unique names and could run concurrently in one process, streaming the same files into the same session (observed interleaved duplicates).BackupRunGate: process-wideMutexon a module-level singleton, acquired byBackupEngine.run.3. Transient 5xx retry
A single 502 from the reverse proxy aborted the whole run; WorkManager then restarted it from scratch every ~30 s against a struggling server.
UploadRetryPolicy(4 retries, 2 s → 60 s cap, ±25% jitter) around file upload and commit.UploadRetryPolicy.NONEso the pre-existing abort-semantics contracts stay pinned byte-for-byte.Behavior changes to existing tests (deliberate)
crash retry reuses the session…GETprobe (revalidation) before reuse — same session + key, still exactly one create5xx aborts the run…NONEpolicy so request counts stay exactsession gone clears batch…Tests
Environment limitation: no Android SDK on this box, so
:appcould not be compiled locally. The change is source-compatible by inspection:AndroidSyncControlleruses named constructor args that all still exist; the two new engine params (retryPolicy,retrySleeper) have defaults. Please let CI's android job (:app:testDebugUnitTest,:app:assembleDebug) confirm.KNOWN_ISSUES.md: ISSUE-176/177/178 appended (
mitigated, reffix/upload-engine).Three correctness fixes in the :core upload engine (apps/android/core only): 1. Session lifecycle: an open batch is validated via GET /api/upload-sessions/{id} before reuse. 403/404/409/410 (now all SessionGoneException — 403 covers the server's 'Not the owner of this upload session' credential-mismatch case) and terminal statuses (completed*/cancelled/failed/expired) discard the batch so a fresh session is created; a live session is reused with its original idempotency key, preserving crash-retry replay. Client-side recovery only, per SEC-4 — ownership is never 'fixed' server-side. 2. Single-flight guard: BackupRunGate (process-wide Mutex on a module singleton) serializes every BackupEngine.run in one process. Wait-not- skip semantics: the second run queues and then proceeds incrementally; a skip would drop the periodic run's work until the next 6h tick, while the persisted batch keeps a waiter's run idempotent. 3. Transient retry: bounded exponential backoff with jitter (UploadRetryPolicy: 4 retries, 2s->60s cap, ±25%) around file upload and commit for 5xx/network faults before falling back to abort -> WorkManager retry. 4xx (except the session-gone family), auth and session-gone failures are never retried in-run. Replayed file uploads are server-side idempotent (upsert on upload_session_id+client_file_key). Tests: BackupEngineTest 14→24 (probe-before-reuse, 403-upload/commit clearing the batch, stale-batch recovery for rejected and terminal sessions, commit/file 5xx in-run retry incl. budget exhaustion, 4xx never retried, gate serialization), UploadSessionClientTest +3 (403 mappings), UploadRetryPolicyTest (new, backoff math). Existing tests updated for the new behavior: crash-retry and fresh-batch runs now expect a GET probe; BatchEngine default test policy disables in-run retry so abort semantics stay pinned. KNOWN_ISSUES.md: append ISSUE-176/177/178 (mitigated, fix/upload-engine).c477e8d72310805a3aa1