Modern Apps git

← Modern-Apps.git

appstore: restart Play downloads when Range is ignored

Commit
989fc9dbe4261ec2bfef78caced35a96f6c4f2d9
Author
Vayun Mathur <[email protected]>
When
2026-11-07 19:38 UTC (28 days ago)
Parents
737f507
Symptom: a resumed Play split download could corrupt its APK.
downloadSingleFile sent a Range header whenever a .tmp partial
existed, then appended the response body unconditionally. A CDN
edge that ignores Range answers 200 with the whole file, so the
finished file was partial-prefix + whole-file: oversized, hash-
mismatched, and rejected by InstallVerifier minutes later with
no hint the resume was at fault. Retrying hit the same .tmp and
failed the same way until the cache was swept.

Root cause: streamToTmp keyed append mode (and the progress
base) off the pre-existing .tmp length alone, never off the
response code. 206 Partial Content is the only response whose
body is actually the missing suffix.

Fix: append only when the response is 206 and the .tmp is
non-empty (PlayDownloader.resumesPartial); a 200 restarts the
file from byte zero.

Verification: new PlayResumeTest pins 206/200/empty cases;
:appstore:testDevUnitTest and :appstore:compileDevKotlin pass.

Diff

appstore/src/main/java/com/vayunmathur/appstore/data/installer/PlayDownloader.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/data/installer/PlayDownloader.kt b/appstore/src/main/java/com/vayunmathur/appstore/data/installer/PlayDownloader.ktindex 788a84cfa9..01897c6d88 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/data/installer/PlayDownloader.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/data/installer/PlayDownloader.kt@@ -31,6 +31,10 @@ class PlayDownloader(         private const val HTTP_PARTIAL = 206         private const val HTTP_FORBIDDEN = 403         private const val HTTP_GONE = 410++        /** True only when a Range resume is safe: partial body for a non-empty .tmp. */+        internal fun resumesPartial(responseCode: Int, existingBytes: Long): Boolean =+            existingBytes > 0 && responseCode == HTTP_PARTIAL     }      class ExpiredUrlException(message: String) : java.io.IOException(message)@@ -138,8 +142,13 @@ class PlayDownloader(          val input = openInput(conn)             ?: return Result.failure(java.io.IOException("Empty body"))+        // Resume only on a real partial response. A server (or CDN) that+        // ignores Range answers 200 with the whole body; appending that to+        // the partial .tmp corrupts the file, and the install then fails+        // verification minutes later instead of simply starting over.+        val append = resumesPartial(code, existing)         return try {-            streamToTmp(input, tmpFile, existing, progressCallback)+            streamToTmp(input, tmpFile, if (append) existing else 0L, progressCallback)             promoteTmp(tmpFile, destFile)             Result.success(destFile)         } catch (expected: java.io.IOException) {
appstore/src/test/java/com/vayunmathur/appstore/data/PlayResumeTest.kt
diff --git a/appstore/src/test/java/com/vayunmathur/appstore/data/PlayResumeTest.kt b/appstore/src/test/java/com/vayunmathur/appstore/data/PlayResumeTest.ktnew file mode 100644index 0000000000..dbbf27c67f--- /dev/null+++ b/appstore/src/test/java/com/vayunmathur/appstore/data/PlayResumeTest.kt@@ -0,0 +1,30 @@+package com.vayunmathur.appstore.data++import com.vayunmathur.appstore.data.installer.PlayDownloader+import kotlin.test.Test+import kotlin.test.assertFalse+import kotlin.test.assertTrue++/**+ * Regression: a server that ignores the Range header answers 200 with the+ * whole body. Appending that to the partial .tmp corrupted the APK, so a+ * resumed Play download failed verification instead of restarting clean.+ */+class PlayResumeTest {++    @Test+    fun `206 with a partial file resumes`() {+        assertTrue(PlayDownloader.resumesPartial(206, existingBytes = 512))+    }++    @Test+    fun `200 restarts instead of appending`() {+        assertFalse(PlayDownloader.resumesPartial(200, existingBytes = 512))+    }++    @Test+    fun `nothing to resume from`() {+        assertFalse(PlayDownloader.resumesPartial(206, existingBytes = 0))+        assertFalse(PlayDownloader.resumesPartial(200, existingBytes = 0))+    }+}