updater: a duplicate install start no longer kills the in-flight update
- Commit
da307938686504b8627d173f1da1e3fc4b70a398- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 21:42 UTC (5 days ago)
- Parents
48888b6
Two overlapping checks (immediate + periodic UpdateCheckWorker racing) each called UpdateInstallService.start(); the duplicate path then called stopSelf(newestStartId) — precisely the call that stops the service immediately, cancelling the update engine mid-install. The cancelled coroutine posted no failure, so the update silently waited up to 6 hours for the next periodic check. The already-in-progress check now runs before intent validation and foreground entry, and any start arriving while a job is active returns START_NOT_STICKY without stopSelf. The job's completion finally calls unconditional stopSelf() so an ignored duplicate's newer start id can't strand the foreground service. Regression test fails pre-fix (service stopped under an active job) and passes after. Version comparison, signature ordering, download resume and channel handling were audited and cleared. Verified: :updater:testDevUnitTest 153 tests pass; lint errors are pre-existing on untouched Route.kt.
Diff
updater/src/main/java/com/vayunmathur/updater/service/UpdateInstallService.kt
diff --git a/updater/src/main/java/com/vayunmathur/updater/service/UpdateInstallService.kt b/updater/src/main/java/com/vayunmathur/updater/service/UpdateInstallService.ktindex 2456cc5a3a..52ee1b6e46 100644--- a/updater/src/main/java/com/vayunmathur/updater/service/UpdateInstallService.kt+++ b/updater/src/main/java/com/vayunmathur/updater/service/UpdateInstallService.kt@@ -61,22 +61,24 @@ class UpdateInstallService : Service() { } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {- val build = intent?.getStringExtra(EXTRA_BUILD)- val buildDate = intent?.getLongExtra(EXTRA_BUILD_DATE, 0L) ?: 0L- if (build.isNullOrEmpty() || buildDate <= 0L) {- Log.status(TAG, "started without a target build")- stopSelf(startId)- return START_NOT_STICKY- }- // Checked before entering the foreground so a duplicate start does not re-post the // progress notification for work it is about to discard. Applying two payloads at once // is rejected by update_engine anyway, and a second download would fight the first over // the same `.part` file.+ //+ // A duplicate must NOT stopSelf here: the startId it arrived with is by definition the+ // most recent one, and stopSelf with the most recent startId stops the service even+ // though the original start's job is still running — onDestroy then cancels that job+ // mid-download/apply. Just return; the in-flight job stops the service when it ends. if (job?.isActive == true) { Log.status(TAG, "an update is already in progress")- // stopSelf takes the LATEST startId, so without this the in-flight job's own- // stopSelf(startId) becomes a no-op and the service outlives its work.+ return START_NOT_STICKY+ }++ val build = intent?.getStringExtra(EXTRA_BUILD)+ val buildDate = intent?.getLongExtra(EXTRA_BUILD_DATE, 0L) ?: 0L+ if (build.isNullOrEmpty() || buildDate <= 0L) {+ Log.status(TAG, "started without a target build") stopSelf(startId) return START_NOT_STICKY }@@ -95,7 +97,10 @@ class UpdateInstallService : Service() { // terminal one, and leaving the ongoing copy behind would show an update // permanently stuck at whatever percent it reached. ServiceCompat.stopForeground(this@UpdateInstallService, ServiceCompat.STOP_FOREGROUND_REMOVE)- stopSelf(startId)+ // Unconditional, not stopSelf(startId): a duplicate start ignored above carries+ // a newer startId, so this start's id is no longer the most recent and+ // stopSelf(startId) would be a no-op, stranding a foreground service.+ stopSelf() } } return START_NOT_STICKY
updater/src/test/java/com/vayunmathur/updater/service/UpdateInstallServiceTest.kt
diff --git a/updater/src/test/java/com/vayunmathur/updater/service/UpdateInstallServiceTest.kt b/updater/src/test/java/com/vayunmathur/updater/service/UpdateInstallServiceTest.ktindex 509cdc5ee9..2b7ff49284 100644--- a/updater/src/test/java/com/vayunmathur/updater/service/UpdateInstallServiceTest.kt+++ b/updater/src/test/java/com/vayunmathur/updater/service/UpdateInstallServiceTest.kt@@ -21,6 +21,7 @@ import kotlin.test.AfterTest import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertEquals+import kotlin.test.assertFalse import kotlin.test.assertNotNull import kotlin.test.assertNull import kotlin.test.assertTrue@@ -38,8 +39,15 @@ class UpdateInstallServiceTest { private val store get() = DataStoreUtils.getInstance(ctx) // The engine must complete through the callback registered at bind time; capture it there.- private class CapturingEngine(var completionCode: Int = 0) : OtaInstaller.PayloadEngine {+ private class CapturingEngine(+ var completionCode: Int = 0,+ var neverCompletes: Boolean = false,+ ) : OtaInstaller.PayloadEngine { private var complete: ((Int) -> Unit)? = null++ @Volatile var applyCalled: Boolean = false+ private set+ override fun bind( onStatusUpdate: (Int, Float) -> Unit, onPayloadApplicationComplete: (Int) -> Unit,@@ -49,7 +57,12 @@ class UpdateInstallServiceTest { } override fun applyPayload(url: String, offset: Long, headers: Array<String>) {- complete?.invoke(completionCode)+ applyCalled = true+ if (!neverCompletes) complete?.invoke(completionCode)+ }++ fun completeManually(code: Int) {+ complete?.invoke(code) } override fun cancel() = Unit@@ -141,6 +154,31 @@ class UpdateInstallServiceTest { assertNull(service.onBind(null)) } + @Test+ fun `a duplicate start does not stop the in-flight update`() {+ // Two overlapping checks (e.g. the immediate + periodic workers racing) both call+ // start(). The first start's job is mid-apply when the second arrives. That second+ // start carries the most recent startId, so stopSelf(thatId) would stop the service+ // and onDestroy would cancel the running job mid-apply.+ responses.add(FakeHttpsConnection(URL("https://ota.example/inc"), 200, zipBytes()))+ val engine = CapturingEngine(completionCode = 0, neverCompletes = true)+ OtaInstaller.payloadEngineOverride = { engine }+ val service = startService(startIntent())+ await("payload apply started") { engine.applyCalled }++ assertEquals(Service.START_NOT_STICKY, service.onStartCommand(startIntent(), 0, 2))+ assertFalse(+ shadowOf(service).isStoppedBySelf,+ "duplicate start must not stop the service running the update",+ )++ // The original run is still alive and can finish normally.+ engine.completeManually(0)+ await("pending reboot after duplicate start") {+ store.getStringAwait(UpdaterPreferences.PENDING_REBOOT_BUILD) == "2026090700"+ }+ }+ @Test fun `an unreadable running build fails with the unknown-build message`() { TestSysProps.set("ro.build.version.incremental", "")