Skip to content

fix: [SDK-4995] match SyncJobService stop and completion by job ID - #2765

Open
fadi-george wants to merge 1 commit into
mainfrom
fadi/sdk-4995-syncjob-jobid
Open

fadi-george wants to merge 1 commit into
mainfrom
fadi/sdk-4995-syncjob-jobid

Conversation

@fadi-george

Copy link
Copy Markdown
Contributor

Description

One Line Summary

Make SyncJobService start, stop, and completion race-safe by tracking the active run and matching onStopJob by job ID.

Details

Motivation

Split out of #2712. onStopJob previously reached into IBackgroundManager via getService, which could fail if init had not finished, and jobFinished could still be called after the system had already stopped the job.

Scope

  • Each onStartJob creates a JobRun that owns its coroutine and state (RUNNING, STOPPED, FINISHED).
  • onStopJob matches by jobId (the system may pass a different JobParameters instance), cancels the owned coroutine, and returns whether it stopped a running job.
  • Exactly one of jobFinished or a successful onStopJob wins, via compare-and-set.
  • Failures and cancellations request a reschedule.
  • Launches through OneSignalDispatchers.launchOnIO directly so stop cancellations are not logged as errors.

Testing

Unit testing

Updated SyncJobServiceTests for distinct JobParameters instances with matching and different job IDs, no active run, and stop after completion.

Manual testing

Forced JobScheduler job 2071862118 on an emulator with queued offline work. The sync ran on OneSignal-IO-1, completed through jobFinished, with no ANR or crash.

Affected code checklist

  • Notifications
    • Display
    • Open
    • Push Processing
    • Confirm Deliveries
  • Outcomes
  • Sessions
  • In-App Messaging
  • REST API requests
  • Public API changes

Checklist

Overview

  • I have filled out all REQUIRED sections above
  • PR does one thing
  • 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
  • I have personally tested this on my device, or explained why that is not possible

Final pass

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

Co-authored-by: Cursor <cursoragent@cursor.com>
@fadi-george
fadi-george requested a review from a team as a code owner September 23, 2026 21:17
@github-actions

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

  • SyncJobService.kt: 33/42 touched executable lines (78.6%) (60 touched lines in diff)
    • 9 uncovered touched lines in this file

Overall (aggregate gate)

33/42 touched executable lines covered (78.6% — requires ≥ 80%)

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

  • SyncJobService.kt: 78.6% (9 uncovered touched lines)

❌ Coverage Check Failed

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

📥 View workflow run

@abdulraqeeb33 abdulraqeeb33 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved.

  1. The two stop tests put the inline launchOnIO runner back only after the assertions. A failed assert leaves the non-running stub for later tests. Restore it in afterAny.
  2. No test that a thrown runBackgroundServices calls jobFinished(parameters, true), or that a winning onStopJob does not also jobFinished.

@abdulraqeeb33 abdulraqeeb33 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same nits, on the lines.

result shouldBe true
verify { job.cancel() }
verify(exactly = 0) { OneSignal.getService<IBackgroundManager>() }
every { OneSignalDispatchers.launchOnIO(any<suspend () -> Unit>()) } answers {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This puts the inline launchOnIO runner back only after the assertions. The other stop test does the same at line 197. A failed assert leaves the non-running stub for later tests. Restore it in afterAny.


// When
val result = mocks.syncJobService.onStopJob(mocks.jobParameters)
test("onStopJob does not reschedule a run that already completed") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still missing: a thrown runBackgroundServices calls jobFinished(parameters, true), and a winning onStopJob does not also jobFinished.

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.

2 participants