Skip to content

Sync health: 429 retry storm, WAL crashes, v2 token expiry, v1 audio latency #7587

Closed as not planned

Description

@beastoin

Sync Health Issues — Root Cause Analysis

Cross-referenced runtime signals (BQ, Crashlytics, Cloud Logging) with code tracing on latest main. Six confirmed issues across backend and mobile client.


Issue 1: 429 Retry Storm on Sync Endpoints

Root cause: Fair-use budget gate returns 429 when minutes exhausted. Client (app/lib/backend/http/api/conversations.dart:410) throws immediately on 429 with zero backoff — no exponential retry, no Retry-After header parsing.

Runtime evidence:

  • 3K–13K 429s/day on sync endpoints (last 7 days)
  • May 30 burst: 707 429s in a single hour (07:00 UTC)
  • Top IP 188.***45: 1,899 429s in 7-hour window (May 30 04:24–11:22 UTC), zero successful requests. Single user who exhausted budget and client retried for 7 hours straight.
  • All 429s from backend-sync service (serverIp 34.***00), not upstream proxy.

Code path: app/lib/backend/http/api/conversations.dart:410-411 — throw Exception('Rate limited or budget exhausted') with no retry logic.

Impact: Wasted server resources, degraded UX for rate-limited users.


Issue 2: WAL _getMissingWals Null Race Condition (66 crashes, 29 users)

Root cause: FlashPageWalSyncImpl._getMissingWals reads oldest_flash_page/newest_flash_page indices from device, but indices go stale if device clears pages between read and use.

Crashlytics stack trace: Null check operator used on a null value at flash_page_wal_sync.dart:128. Call path: setDevice → _getMissingWals.

Code path: app/lib/services/wals/flash_page_wal_sync.dart:111-128 — reads page indices, then uses them later without refreshing. Race between setDevice and page access.

Impact: High — 29 users affected, potential data loss from failed sync.


Issue 3: WAL _flush OOM (16 crashes, 4 users)

Root cause: LocalWalSyncImpl._flush accumulates all WAL frames via unbounded List.addAll before writing to disk. On large WALs, this exhausts memory.

Crashlytics stack trace: Out of Memory at List.addAll → _flush.

Code path: app/lib/services/wals/local_wal_sync.dart:240-284 — no chunked writing, entire frame buffer loaded into memory. Secondary issue: writeAsBytes (line 268) has no try-catch.

Impact: Medium — 4 users, crashes on devices with large accumulated WALs.


Issue 4: WAL _chunk Unmodifiable List Bug (12 crashes, 6 users)

Root cause: LocalWalSyncImpl._chunk calls addAll on an unmodifiable list at line 228.

Crashlytics stack trace: Cannot add to an unmodifiable list at local_wal_sync.dart:228. Call path: start → _chunk.

Code path: app/lib/services/wals/local_wal_sync.dart:228 — Dart immutability bug, list returned from const or List.unmodifiable is mutated.

Impact: Medium — 6 users affected.


Issue 5: v2 Sync Token Expiry Mid-Upload (401s)

Root cause: syncLocalFilesV2 multipart uploads can run 30+ minutes. Firebase token is checked before upload starts (app/lib/backend/http/shared.dart:25-49) but NOT refreshed during transfer. Token expires mid-upload → 401 → retry re-uploads from scratch.

Runtime evidence:

  • 200–1,300 401s/day on sync endpoints
  • 97% on v2 (733 v2 vs 24 v1 in 3 days)
  • Top 8 IPs account for 88% of 401s — power users with large sync queues
  • Time pattern: spikes at 02:00 (130), 06:00 (106), 07:00 (236 — peak) UTC — overnight sync batches when devices accumulate audio files
  • Top IP: 218 401s in a single day

Code path: app/lib/backend/http/shared.dart:240-286 — multipart upload checks token before request, no mid-upload refresh.

Impact: Medium — 8 power users affected, wasted bandwidth from repeated full re-uploads.


Issue 6: v1 Audio Sync Extreme Latency (200–400s)

Root cause: /v1/sync/audio/{id}/urls handler synchronously calls _precache_audio_file() for first uncached audio file. This blocks the HTTP request while performing: GCS chunk listing → parallel chunk download → AES-256-GCM decryption → Opus decode → PCM merge → WAV conversion → GCS cache upload.

Runtime evidence:

  • May 27–28: individual requests took 200–400s (max 400.5s at 03:00 UTC May 27)
  • All returned 200 OK — requests complete, just extremely slowly
  • 85% of slow requests from 2 IPs on same /24 subnet (136.***69, 136.***73) — power users bulk-syncing
    • IP1: 266 requests >60s, avg 180s, max 393s, 5+ distinct audio IDs
    • IP2: 118 requests >60s, avg 216s, max 401s, 5+ distinct audio IDs
  • Remaining ~13 IPs at 2–8 slow requests each

Code path: backend/routers/sync.py:260 — synchronous _precache_audio_file() call. v1 /sync-local-files deprecated (PR #7125) but audio URLs endpoints still active without deprecation headers.

Impact: Low — 2 users, deprecated path. Not system-wide.


Latency Profile (last 3 days, BQ)

Version Status Count p50 p95 p99 Max
v2 200 168K 11ms 36ms 289ms 13.6s
v2 202 117K 112ms 298ms 536ms 6.5s
v2 429 3.8K 39ms 59ms 120ms 0.5s
v1 200 7K 55ms 887ms 3.1s 17.3s

Priority

# Issue Severity Users
1 WAL _getMissingWals null race High 29
2 Client 429 backoff missing Medium at scale
3 WAL _flush OOM Medium 4
4 WAL _chunk immutable list Medium 6
5 v2 token expiry mid-upload Medium 8
6 v1 audio precache latency Low 2

Investigation by @mon (runtime signals) and @taro (code tracing) for @beastoin

Activity

  1. beastoin commented on Jun 2, 2026

    @beastoin
    CollaboratorAuthor

    Affected Users (Redacted)

    Issue 1: 429 Retry Storm

    No Firebase UID available — 429s are returned by fair-use budget gate before auth handler logs UID.

    • IP: 188.***45 — 1,899 429s, May 30 04:24–11:22 UTC, zero successful requests
    • Budget gate–adjacent UIDs (May 30, approximate — cannot confirm IP mapping): 04N***kp1, aa3***wA3, tif***iD3, ewE***Z33, cOK***RU2

    Issue 2: WAL _getMissingWals Null Race (top 5 of 29 users)

    Crashlytics installation UUIDs (device IDs, not Firebase UIDs):

    Installation UUID Crashes
    A81***EE3 5
    362***1F3 4
    B41***338 4
    F40***5B5 4
    631***090 4

    Issue 3: WAL _flush OOM (all 4 users)

    Installation UUID Crashes Platform
    57E***78B 7 iOS
    390***12B 5 iOS
    A4E***857 3 iOS
    920***E2C 1 iOS

    Issue 4: WAL _chunk Immutable List (top 3 of 6 users)

    Installation UUID Crashes Platform
    B8A***19C 3 Android
    00F***265 3 Android
    A98***756 2 iOS

    Issue 5: v2 Token Expiry 401s

    No Firebase UID — 401s rejected at auth middleware before UID extraction.

    IP 401 Count Period
    221.***62 218 1 day
    114.***68 181 1 day
    36.***243 67 1 day

    Issue 6: v1 Audio Latency

    No Firebase UID — sync audio handler doesn't log UIDs.

    IP Slow Requests Avg Latency Max Latency
    136.***69 266 180s 393s
    136.***73 118 216s 401s

    Same /24 subnet — likely same household.


    Instrumentation Gap

    Firebase UIDs are not available for issues 1, 5, and 6 because:

    • 429s/401s: rejected before auth handler extracts UID from token
    • Sync audio: handler doesn't log UID in stderr
    • Crashlytics: stores installation_uuid (device fingerprint), not Firebase UID

    Recommended instrumentation fixes:

    1. Log UID extraction attempt before budget gate check (captures UID even on 429)
    2. Add Firebase UID as Crashlytics custom key (maps device crashes → user account)
    3. Add UID to sync audio handler log context

    by @taro for @beastoin

  2. added
    p1
    bug
    capture
    on Jun 9, 2026
  3. beastoin commented on Jun 10, 2026

    @beastoin
    CollaboratorAuthor

    Closing as not planned per CTO directive (2026-06-10).


    by AI for @beastoin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcapturep1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions