Skip to content

fix: [SDK-5832] hold OpRepo HTTP under SyncJob when backgrounded - #2862

Draft
abdulraqeeb33 wants to merge 2 commits into
mainfrom
fix/SDK-5832-patch-user-disconnect
Draft

abdulraqeeb33 wants to merge 2 commits into
mainfrom
fix/SDK-5832-patch-user-disconnect

Conversation

@abdulraqeeb33

Copy link
Copy Markdown
Contributor

Description

One Line Summary

Run due OpRepo HTTP under a held SyncJob when the app is backgrounded, so property user PATCHes are not started on an unprotected daemon thread.

Details

Motivation

SDK-5832: Turbine sees Android PATCH /apps/:id/users/by/:alias/:id hang up during GetUser within ~1–3ms (~650k/day). Server cancel is a client TCP close, not an SDK HTTP timeout (timeouts are 120s+). p50 ≈ 1.6ms. Android is ~100–200× iOS.

The queue is not cleared in background. Ops stay on disk until SUCCESS. What fails is the in-flight TCP: OpRepo still starts HTTP after opRepoExecutionInterval (~5s) on a daemon thread with no process hold, Android kills the socket, Turbine logs a cancel, and the SDK retries (status 0 → FAIL_RETRY).

Property-only PATCHes (refresh_device_metadata=false) cancel ~46× more often than session-start refreshes. Session start uses enqueue(..., flush=true) while typically foregrounded. Property ops from model listeners use default flush=false, so the 5s batching window often ends after backgrounding.

When this started

Not a single-line regression. The unprotected OpRepo path existed in mid-5.1; later releases increased how often HTTP starts while killable:

Band Errors / 10k What changed
5.0–5.1 3.8 Baseline. OpRepo already had ~5s batching/buckets; login/refresh stopped using flush=true; Sync path is JobScheduler-only and does not drain OpRepo HTTP.
5.6–5.8 18.3 Session timezone writes on start/active enqueue property ops with flush=false, raising unprotected PATCH volume.
5.9 21.1 Background-threading feature flag rolling out (HTTP/lifecycle on SDK dispatcher pools).
5.10–5.11 32.9 Background-threading FF removed; async init default. SyncJob finishes after runBackgroundServices() without waiting on OpRepo HTTP. Peak cancel share ~0.3% of 202s.

Scope

OperationRepo is now an IBackgroundService (same bridge SessionService already uses):

  1. scheduleBackgroundRunIn: 0 when due work is queued (BackgroundManager clamps to ≥5s), or the retry delay when all ops are backed off.
  2. When the batch delay ends while backgrounded, the forever loop does not start HTTP on the naked OpRepo daemon thread.
  3. backgroundRun(): arms a hold, force-wakes the loop, awaits drain of executable ops, then returns under the held SyncJobService.
  4. OperationRetryTrigger binds live isInForeground into OpRepo so focus can resume execution without SyncJob.
  5. Foreground behavior stays as today. Queue persistence / FAIL_RETRY unchanged.
JobScheduler
  └── SyncJobService          ← OS process hold
        └── BackgroundManager
              ├── SessionService.backgroundRun()
              └── OperationRepo.backgroundRun()   ← this PR

Punted from this PR (separate follow-ups if still wanted):

  • Blank language hygiene (volume reduction only; not the disconnect cause)
  • HttpClient withContext(IO) + request-body stream close (structure only)

Testing

Unit testing

  • scheduleBackgroundRunIn null when empty / 0 when due work is queued
  • Backgrounded repo does not execute until backgroundRun holds the process
  • Foregrounding after a background wait allows execution without backgroundRun
  • Existing OperationRetryTriggerTests updated for concrete OperationRepo bind

Manual testing

Not yet.

Affected code checklist

  • Notifications
  • Outcomes
  • Sessions
  • REST API requests
  • Public API changes

Checklist

Overview

  • I have filled out all REQUIRED sections above
  • PR does one thing
    • OpRepo background process hold only; language / HttpClient punted
  • Any Public API changes are explained in the PR details and conform to existing APIs

Testing

  • I have included test coverage for these changes, or explained why they are not needed
  • All automated tests pass, or I explained why that is not possible
    • New OpRepo background-hold tests pass. Pre-existing flake: ensure each time enqueue is called it restarts the delay time also fails on main.
  • I have personally tested this on my device, or explained why that is not possible
    • Not yet

Final pass

  • Code is as readable as possible.
  • I have reviewed this PR myself, ensuring it meets each checklist item

Made with Cursor

Property PATCHes were starting on the OpRepo daemon thread after the
batching delay while the process was unprotected. Make OperationRepo an
IBackgroundService so due work runs inside SyncJob's process hold, and
gate the forever loop so background HTTP cannot start without that hold.

Co-authored-by: Cursor <cursoragent@cursor.com>
@abdulraqeeb33
abdulraqeeb33 requested a review from a team December 1, 2026 20:13
…y test

Co-authored-by: Cursor <cursoragent@cursor.com>
@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

📊 Diff Coverage Report

Diff Coverage Report (Changed Lines Only)

Gate: aggregate coverage on changed executable lines must be ≥ 80% (JaCoCo line data for lines touched in the diff).

Changed Files Coverage

  • AnrCheckEvaluator.kt: 2/2 touched executable lines (100.0%) (4 touched lines in diff)
  • AndroidLogAnrDetector.kt: 0/5 touched executable lines (0.0%) (16 touched lines in diff)
    • 5 uncovered touched lines in this file
  • AndroidLogCrashHandler.kt: 16/16 touched executable lines (100.0%) (37 touched lines in diff)

Overall (aggregate gate)

18/23 touched executable lines covered (78.3% — requires ≥ 80%)

Per-file detail (informational; gate is aggregate above):

  • AndroidLogAnrDetector.kt: 0.0% (5 uncovered touched lines)

❌ Coverage Check Failed

Aggregate coverage on touched lines is 78.3% (minimum 80%).

📥 View workflow run

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant