From f6d44376ed7c11b52f52ab5c17053f3532a5c199 Mon Sep 17 00:00:00 2001 From: ovurrsl Date: Sun, 18 Oct 2026 03:40:51 +0300 Subject: [PATCH] fix: cancelling the first-run desktop or Proton install stops its download Cancel closed the session screen, but the desktop or Proton download kept running with no UI, and a relaunch saw the stale stop flag and closed itself. Downloads now take a cancel check, DesktopCatalog.install stops before changing anything on disk, the install state is process-wide, and a new session is refused while a cancelled install is still winding down. --- .../com/droiddeck/launcher/MainActivity.kt | 4 +++ .../com/droiddeck/launcher/SessionActivity.kt | 15 +++++--- .../droiddeck/launcher/core/Downloader.java | 36 +++++++++++++++---- .../launcher/runtime/DesktopCatalog.kt | 9 +++-- .../launcher/core/DownloaderResumeTest.kt | 20 +++++++++++ 5 files changed, 70 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt index ec5ff76c43..ff22490c29 100644 --- a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt @@ -433,6 +433,10 @@ class MainActivity : ComponentActivity() { android.widget.Toast.makeText(this, R.string.main_wait_runtime, android.widget.Toast.LENGTH_SHORT).show() return } + SessionState.installing != null && !SessionState.running -> { + android.widget.Toast.makeText(this, R.string.main_wait_desktop_install, android.widget.Toast.LENGTH_SHORT).show() + return + } protons.protonBusyId != null || ProtonExtras.installInProgress -> { android.widget.Toast.makeText(this, R.string.main_wait_proton_install, android.widget.Toast.LENGTH_SHORT).show() return diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 9f145ab572..cd1ed6367f 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -402,6 +402,13 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { finish() return } + // A cancelled install keeps its thread until the download stops; a new launch would reuse its stop flag and close itself. + if (savedInstanceState == null && SessionState.installing != null && !SessionState.running) { + runtimeRemovalBlocked = true + android.widget.Toast.makeText(this, R.string.main_wait_desktop_install, android.widget.Toast.LENGTH_SHORT).show() + finish() + return + } if (!SessionState.running && SessionState.phase in setOf(SessionPhase.IDLE, SessionPhase.FAILED)) { val mode = intent.getStringExtra(SessionService.EXTRA_MODE) ?: SessionService.MODE_STEAM SessionState.programArgs = intent.getStringArrayExtra(SessionService.EXTRA_PROGRAM_ARGS)?.toList().orEmpty() @@ -1319,11 +1326,10 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } - @Volatile private var installingRuntime = false + private val installingRuntime get() = SessionState.installing != null private fun installThenStart(runtime: Boolean, desktop: Boolean, proton: Boolean = false) { if (SessionState.stopRequested) return - installingRuntime = true SessionState.installing = if (runtime) "runtime" else if (desktop) "desktop" else "proton" SessionEvents.transition( SessionPhase.INSTALLING_RUNTIME, @@ -1354,7 +1360,6 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { else SessionEvents.record("proton.skipped", mapOf("reason" to protonProblem)) } uiHandler.post { - installingRuntime = false SessionState.installing = null if (SessionState.stopRequested) { SessionState.stopRequested = false @@ -1423,7 +1428,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private fun installDesktop(): String? { uiHandler.post { loading.percent = -1; loading.say(getString(DESKTOP_LINES.downloading), DESKTOP_LINES.topic, readable = true) } val entry = com.droiddeck.launcher.runtime.DesktopCatalog.desktopEntry(this) - val problem = com.droiddeck.launcher.runtime.DesktopCatalog.install(this, entry, + val problem = com.droiddeck.launcher.runtime.DesktopCatalog.install(this, entry, { SessionState.stopRequested }, progressFor(DESKTOP_LINES, entry.size / 1_000_000)) return problem?.let { getString(R.string.session_desktop_install_failed, it) } } @@ -1433,7 +1438,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { val catalog = com.droiddeck.launcher.runtime.DesktopCatalog val entry = catalog.fetch(catalog.STEAM_SEED_URL)?.firstOrNull { it.id == catalog.PROTON_SEED_ID } ?: return "catalog unreachable" - return catalog.install(this, entry, progressFor(PROTON_LINES, entry.size / 1_000_000)) + return catalog.install(this, entry, { SessionState.stopRequested }, progressFor(PROTON_LINES, entry.size / 1_000_000)) } override fun surfaceCreated(holder: SurfaceHolder) { diff --git a/app/src/main/java/com/droiddeck/launcher/core/Downloader.java b/app/src/main/java/com/droiddeck/launcher/core/Downloader.java index 2527c57e82..acad82b853 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/Downloader.java +++ b/app/src/main/java/com/droiddeck/launcher/core/Downloader.java @@ -52,19 +52,35 @@ public final class Downloader { /** Verify each candidate before accepting it, so a bad mirror can fall back. */ public static boolean downloadFile(String url, File destination, boolean resume, String expectedSha256, java.util.function.Consumer progress) { + return downloadFile(url, destination, resume, expectedSha256, null, progress); + } + + /** {@code cancelled} is polled while bytes arrive; a cancel stops at once and leaves the partial file for the caller. */ + public static boolean downloadFile(String url, File destination, boolean resume, String expectedSha256, + java.util.function.BooleanSupplier cancelled, + java.util.function.Consumer progress) { if (expectedSha256 != null && !expectedSha256.matches("(?i)[0-9a-f]{64}")) throw new IllegalArgumentException("Expected SHA-256 must be 64 hex characters"); - return downloadCandidates(url, DownloadSource.INSTANCE.candidates(url), destination, resume, expectedSha256, progress); + return downloadCandidates(url, DownloadSource.INSTANCE.candidates(url), destination, resume, expectedSha256, cancelled, progress); } // Package-private for the local multi-source HTTP test. static boolean downloadCandidates(String original, List sources, File destination, boolean resume, String expectedSha256, java.util.function.Consumer progress) { + return downloadCandidates(original, sources, destination, resume, expectedSha256, null, progress); + } + + static boolean downloadCandidates(String original, List sources, File destination, boolean resume, + String expectedSha256, java.util.function.BooleanSupplier cancelled, + java.util.function.Consumer progress) { Identity identity = loadIdentity(destination, resume, expectedSha256); for (int i = 0; i < sources.size(); i++) { String source = sources.get(i); + if (isCancelled(cancelled)) return false; if (i > 0) Log.i(TAG, "retrying " + original + " via " + source); - Outcome outcome = attempt(source, destination, resume, identity, expectedSha256, progress); + Outcome outcome = attempt(source, destination, resume, identity, expectedSha256, cancelled, progress); + // A cancel is not a source failure: no other mirror, and no DownloadSource.failed(). + if (outcome == Outcome.CANCELLED) return false; if (outcome == Outcome.DONE) { if (expectedSha256 != null && !expectedSha256.equalsIgnoreCase(Hashes.sha256(destination))) { Log.w(TAG, "checksum mismatch from " + source + "; trying another source"); @@ -82,7 +98,11 @@ public final class Downloader { return false; } - private enum Outcome { DONE, RETRY, FATAL } + private enum Outcome { DONE, RETRY, FATAL, CANCELLED } + + private static boolean isCancelled(java.util.function.BooleanSupplier cancelled) { + return cancelled != null && cancelled.getAsBoolean(); + } private static final class Identity { private String source; @@ -150,7 +170,8 @@ public final class Downloader { } private static Outcome attempt(String url, File destination, boolean resume, Identity identity, - String expectedSha256, java.util.function.Consumer progress) { + String expectedSha256, java.util.function.BooleanSupplier cancelled, + java.util.function.Consumer progress) { HttpURLConnection connection = null; long have = resume && destination.isFile() ? destination.length() : 0; try { @@ -168,7 +189,7 @@ public final class Downloader { if (have > 0 && code == HTTP_RANGE_NOT_SATISFIABLE) { connection.disconnect(); if (!discardPartial(destination, identity)) return Outcome.RETRY; - return attempt(url, destination, false, identity, expectedSha256, progress); + return attempt(url, destination, false, identity, expectedSha256, cancelled, progress); } if (code / 100 != 2) { Log.w(TAG, url + " -> HTTP " + code); @@ -179,13 +200,13 @@ public final class Downloader { if (appending && !rangeStartsAt(connection.getHeaderField("Content-Range"), have)) { connection.disconnect(); if (!discardPartial(destination, identity)) return Outcome.RETRY; - return attempt(url, destination, false, identity, expectedSha256, progress); + return attempt(url, destination, false, identity, expectedSha256, cancelled, progress); } String etag = connection.getHeaderField("ETag"); if (have > 0 && appending && (!strongETag(etag) || !etag.equals(identity.etag))) { connection.disconnect(); if (!discardPartial(destination, identity)) return Outcome.RETRY; - return attempt(url, destination, false, identity, expectedSha256, progress); + return attempt(url, destination, false, identity, expectedSha256, cancelled, progress); } if (!appending && have > 0) { // If-Range may legitimately return full 200 when representation changed. @@ -209,6 +230,7 @@ public final class Downloader { for (int read = in.read(buffer); read > 0; read = in.read(buffer)) { out.write(buffer, 0, read); written += read; + if (isCancelled(cancelled)) return Outcome.CANCELLED; if (progress != null && written - lastReport > (1 << 20)) { lastReport = written; progress.accept(total > 0 ? written / (float) total : -1f); diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/DesktopCatalog.kt b/app/src/main/java/com/droiddeck/launcher/runtime/DesktopCatalog.kt index 3fad171f23..43f3f991c0 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/DesktopCatalog.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/DesktopCatalog.kt @@ -82,7 +82,11 @@ object DesktopCatalog { fun removeDesktop(context: Context, listener: LinuxRuntimeInstaller.ProgressListener): String? = DesktopRemoval.remove(context, listener) - fun install(context: Context, entry: Entry, listener: LinuxRuntimeInstaller.ProgressListener?): String? { + fun install(context: Context, entry: Entry, listener: LinuxRuntimeInstaller.ProgressListener?): String? = + install(context, entry, { false }, listener) + + /** A cancel stops the download and is checked once more before anything on disk changes; extraction itself runs to the end. */ + fun install(context: Context, entry: Entry, cancelled: () -> Boolean, listener: LinuxRuntimeInstaller.ProgressListener?): String? { val root = LinuxRuntime.rootDir(context) if (!root.isDirectory) return context.getString(R.string.deskpkg_runtime_missing) if (entry.sha256.isEmpty()) return context.getString(R.string.deskpkg_no_checksum, entry.name) @@ -90,13 +94,14 @@ object DesktopCatalog { try { val downloading = context.getString(R.string.user_apps_downloading, entry.name) listener?.onProgress(LinuxRuntimeInstaller.Step.DOWNLOADING, downloading, 0) - val ok = Downloader.downloadFile(entry.url, download, true, entry.sha256) { f -> + val ok = Downloader.downloadFile(entry.url, download, true, entry.sha256, { cancelled() }) { f -> listener?.onProgress(LinuxRuntimeInstaller.Step.DOWNLOADING, downloading, if (f < 0) -1 else Math.round(f * 100f)) } if (!ok) return context.getString(R.string.user_apps_download_failed) listener?.onProgress(LinuxRuntimeInstaller.Step.VERIFYING, context.getString(R.string.rtinst_verifying), -1) val actual = Hashes.sha256(download) if (!entry.sha256.equals(actual, ignoreCase = true)) return context.getString(R.string.deskpkg_checksum_mismatch) + if (cancelled()) return context.getString(R.string.deskpkg_install_failed) listener?.onProgress(context.getString(R.string.deskpkg_installing, entry.name), -1) when (entry.kind) { "appimage" -> { diff --git a/app/src/test/java/com/droiddeck/launcher/core/DownloaderResumeTest.kt b/app/src/test/java/com/droiddeck/launcher/core/DownloaderResumeTest.kt index a3faef0701..d286d5aa03 100644 --- a/app/src/test/java/com/droiddeck/launcher/core/DownloaderResumeTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/core/DownloaderResumeTest.kt @@ -167,4 +167,24 @@ class DownloaderResumeTest { assertEquals(listOf(catalog), DownloadSource.candidates(catalog)) } + + @Test fun cancelStopsTheTransferWithoutTryingAnotherSource() { + val first = "http://127.0.0.1:${server.localPort}/first" + val second = "http://127.0.0.1:${server.localPort}/second" + val target = File(dir, "runtime.tar.zst") + misbehave = true + val polls = java.util.concurrent.atomic.AtomicInteger() + // the first poll is before the request; every later one is after a chunk was written + val cancelled = java.util.function.BooleanSupplier { polls.incrementAndGet() > 1 } + assertFalse(Downloader.downloadCandidates(first, listOf(first, second), target, true, null, cancelled, null)) + assertEquals("a cancel must not fall back to the next source", listOf(null), requests.toList()) + assertTrue("the transfer stopped early (kept=${target.length()})", target.length() in 1 until data.size) + } + + @Test fun aCancelBeforeTheFirstRequestSendsNothing() { + val url = "http://127.0.0.1:${server.localPort}/runtime.tar.zst" + val target = File(dir, "runtime.tar.zst") + assertFalse(Downloader.downloadFile(url, target, true, null, { true }, null)) + assertEquals(emptyList(), requests.toList()) + } }