From f6d44376ed7c11b52f52ab5c17053f3532a5c199 Mon Sep 17 00:00:00 2001 From: ovurrsl Date: Sun, 18 Oct 2026 03:40:51 +0300 Subject: [PATCH 1/9] 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()) + } } From b3483349e37e0e4b7a9df9aa9666fa80a7058718 Mon Sep 17 00:00:00 2001 From: ovurrsl Date: Mon, 19 Oct 2026 16:18:04 +0300 Subject: [PATCH 2/9] fix(install): check cancellation around retries/completion and keep install worker ownership --- .../com/droiddeck/launcher/MainActivity.kt | 4 ++++ .../com/droiddeck/launcher/SessionActivity.kt | 19 ++++++++++++++----- .../droiddeck/launcher/core/Downloader.java | 2 ++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt index ff22490c29..5d43f9dd23 100644 --- a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt @@ -1997,6 +1997,10 @@ class MainActivity : ComponentActivity() { private fun startSession(intent: Intent, steamSession: Boolean = false): Boolean { if (busy || LinuxRuntimeInstaller.isBusy()) return false + if (SessionState.installing != null && !SessionState.running) { + android.widget.Toast.makeText(this, R.string.main_wait_desktop_install, android.widget.Toast.LENGTH_SHORT).show() + return false + } if (com.droiddeck.launcher.storage.StorageState.cleaning) { android.widget.Toast.makeText(this, R.string.storage_cleanup_busy, android.widget.Toast.LENGTH_SHORT).show() return false diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index cd1ed6367f..97ef4d4041 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -1326,10 +1326,14 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } - private val installingRuntime get() = SessionState.installing != null + private var localInstalling = false + private val installingRuntime get() = localInstalling || SessionState.installing != null private fun installThenStart(runtime: Boolean, desktop: Boolean, proton: Boolean = false) { if (SessionState.stopRequested) return + val worker = Any() + activeInstallWorker = worker + localInstalling = true SessionState.installing = if (runtime) "runtime" else if (desktop) "desktop" else "proton" SessionEvents.transition( SessionPhase.INSTALLING_RUNTIME, @@ -1347,22 +1351,26 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (problem == null) SessionEvents.record("runtime.ready") else failedComponent = "runtime" } if (problem == null && desktop && !SessionState.stopRequested) { - SessionState.installing = "desktop" + if (activeInstallWorker === worker) SessionState.installing = "desktop" SessionEvents.transition(SessionPhase.INSTALLING_RUNTIME, "desktop.installing", mapOf("component" to "desktop")) problem = installDesktop() if (problem == null) SessionEvents.record("desktop.ready") else failedComponent = "desktop" } if (problem == null && proton && !SessionState.stopRequested) { - SessionState.installing = "proton" + if (activeInstallWorker === worker) SessionState.installing = "proton" SessionEvents.transition(SessionPhase.INSTALLING_RUNTIME, "proton.installing", mapOf("component" to "proton")) val protonProblem = installProton() if (protonProblem == null) SessionEvents.record("proton.ready") else SessionEvents.record("proton.skipped", mapOf("reason" to protonProblem)) } uiHandler.post { - SessionState.installing = null + localInstalling = false + if (activeInstallWorker === worker) { + activeInstallWorker = null + SessionState.installing = null + } if (SessionState.stopRequested) { - SessionState.stopRequested = false + if (activeInstallWorker == null) SessionState.stopRequested = false SessionEvents.transition(SessionPhase.IDLE, "session.cancelled") collectStartArtifacts("session start cancelled") finish() @@ -3195,6 +3203,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private const val LEAVE_TIMEOUT_MS = 1_500L private const val TAG = "SessionActivity" private var startedScreens = 0 + @Volatile private var activeInstallWorker: Any? = null private const val FEX_MISSING = "droiddeck-fex: x86 Linux programs need" private const val UNBUFFERED_SOURCES = InputDevice.SOURCE_CLASS_JOYSTICK or InputDevice.SOURCE_CLASS_TRACKBALL or InputDevice.SOURCE_CLASS_POSITION private const val CURSOR_PAD_HOLD_MS = 1200L 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 acad82b853..a924667383 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/Downloader.java +++ b/app/src/main/java/com/droiddeck/launcher/core/Downloader.java @@ -172,6 +172,7 @@ public final class Downloader { private static Outcome attempt(String url, File destination, boolean resume, Identity identity, String expectedSha256, java.util.function.BooleanSupplier cancelled, java.util.function.Consumer progress) { + if (isCancelled(cancelled)) return Outcome.CANCELLED; HttpURLConnection connection = null; long have = resume && destination.isFile() ? destination.length() : 0; try { @@ -237,6 +238,7 @@ public final class Downloader { } } } + if (isCancelled(cancelled)) return Outcome.CANCELLED; if (total > 0 && written < total) return Outcome.RETRY; return Outcome.DONE; } catch (Exception e) { From 4e4a7312210a281a970e56833c776707ee873603 Mon Sep 17 00:00:00 2001 From: xXJsonDeruloXx Date: Thu, 22 Oct 2026 09:52:32 -0400 Subject: [PATCH 3/9] test: cancel downloads after the request starts --- .../java/com/droiddeck/launcher/core/DownloaderResumeTest.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 d286d5aa03..a505ee1730 100644 --- a/app/src/test/java/com/droiddeck/launcher/core/DownloaderResumeTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/core/DownloaderResumeTest.kt @@ -174,8 +174,8 @@ class DownloaderResumeTest { 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 } + // candidate selection and attempt each check once before the request; later checks follow chunks + val cancelled = java.util.function.BooleanSupplier { polls.incrementAndGet() > 2 } 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) From c02832ac59becfe901847715f5043fcaa0b7aa7d Mon Sep 17 00:00:00 2001 From: xXJsonDeruloXx Date: Thu, 22 Oct 2026 09:56:05 -0400 Subject: [PATCH 4/9] fix: disconnect desktop download on session cancel --- .../com/droiddeck/launcher/SessionActivity.kt | 12 +++++-- .../droiddeck/launcher/core/Downloader.java | 34 +++++++++++++++++- .../launcher/runtime/DesktopCatalog.kt | 5 ++- .../launcher/session/SessionService.kt | 1 + .../launcher/session/SessionState.kt | 3 ++ .../launcher/core/DownloaderResumeTest.kt | 36 +++++++++++++++++-- 6 files changed, 84 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 97ef4d4041..7f1ac67165 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -1436,9 +1436,15 @@ 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, { SessionState.stopRequested }, - progressFor(DESKTOP_LINES, entry.size / 1_000_000)) - return problem?.let { getString(R.string.session_desktop_install_failed, it) } + val cancellation = com.droiddeck.launcher.core.Downloader.Cancellation { SessionState.stopRequested } + SessionState.installCancellation = cancellation + return try { + val problem = com.droiddeck.launcher.runtime.DesktopCatalog.install(this, entry, cancellation, + progressFor(DESKTOP_LINES, entry.size / 1_000_000)) + problem?.let { getString(R.string.session_desktop_install_failed, it) } + } finally { + if (SessionState.installCancellation === cancellation) SessionState.installCancellation = null + } } private fun installProton(): String? { 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 a924667383..dcf3f0d950 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/Downloader.java +++ b/app/src/main/java/com/droiddeck/launcher/core/Downloader.java @@ -15,6 +15,8 @@ import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Properties; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; public final class Downloader { private static final String TAG = "Downloader"; @@ -24,6 +26,32 @@ public final class Downloader { private Downloader() {} + public static final class Cancellation implements java.util.function.BooleanSupplier { + private final AtomicBoolean cancelled = new AtomicBoolean(); + private final AtomicReference connection = new AtomicReference<>(); + private final java.util.function.BooleanSupplier external; + + public Cancellation() { this(null); } + public Cancellation(java.util.function.BooleanSupplier external) { this.external = external; } + + public void cancel() { + cancelled.set(true); + HttpURLConnection active = connection.get(); + if (active != null) active.disconnect(); + } + + @Override public boolean getAsBoolean() { + return cancelled.get() || (external != null && external.getAsBoolean()); + } + + private void attach(HttpURLConnection active) { + connection.set(active); + if (getAsBoolean()) active.disconnect(); + } + + private void detach(HttpURLConnection active) { connection.compareAndSet(active, null); } + } + public static String downloadString(String url) { List sources = DownloadSource.INSTANCE.candidates(url); for (int i = 0; i < sources.size(); i++) { @@ -182,6 +210,7 @@ public final class Downloader { have = 0; } connection = open(url, STALL_TIMEOUT_MS); + if (cancelled instanceof Cancellation) ((Cancellation) cancelled).attach(connection); if (have > 0) { connection.setRequestProperty("Range", "bytes=" + have + "-"); connection.setRequestProperty("If-Range", identity.etag); @@ -245,7 +274,10 @@ public final class Downloader { Log.w(TAG, "download " + url, e); return Outcome.RETRY; } finally { - if (connection != null) connection.disconnect(); + if (connection != null) { + if (cancelled instanceof Cancellation) ((Cancellation) cancelled).detach(connection); + connection.disconnect(); + } } } 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 43f3f991c0..b276b5b456 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/DesktopCatalog.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/DesktopCatalog.kt @@ -86,7 +86,10 @@ object DesktopCatalog { 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? { + fun install(context: Context, entry: Entry, cancelled: () -> Boolean, listener: LinuxRuntimeInstaller.ProgressListener?): String? = + install(context, entry, Downloader.Cancellation(cancelled), listener) + + fun install(context: Context, entry: Entry, cancelled: Downloader.Cancellation, 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) diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt index e9f7fb3900..f98aa7469d 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -1733,6 +1733,7 @@ class SessionService : Service() { fun stop(context: Context) { if (!SessionState.running) { SessionState.stopRequested = true + SessionState.installCancellation?.cancel() return } context.startService(Intent(context, SessionService::class.java).setAction(ACTION_STOP)) diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionState.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionState.kt index ab32b90591..395d839b40 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionState.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionState.kt @@ -36,6 +36,9 @@ object SessionState { @Volatile var installing: String? = null + @Volatile + var installCancellation: com.droiddeck.launcher.core.Downloader.Cancellation? = null + @Volatile var stopRequested = false 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 a505ee1730..da6fec27b9 100644 --- a/app/src/test/java/com/droiddeck/launcher/core/DownloaderResumeTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/core/DownloaderResumeTest.kt @@ -6,6 +6,9 @@ import java.net.Socket import java.nio.file.Files import java.security.MessageDigest import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.CountDownLatch +import java.util.concurrent.Executors +import java.util.concurrent.TimeUnit import org.junit.After import org.junit.Assert.assertArrayEquals import org.junit.Assert.assertEquals @@ -29,6 +32,9 @@ class DownloaderResumeTest { @Volatile private var misbehave = false @Volatile private var etag: String? = "\"v1\"" @Volatile private var corrupt = false + @Volatile private var holdAfterFirstChunk = false + private var bodyStarted = CountDownLatch(1) + private var continueBody = CountDownLatch(1) @Before fun setUp() { dir = Files.createTempDirectory("downloader-resume").toFile() @@ -70,8 +76,15 @@ class DownloaderResumeTest { val start = if (from == null) "200 OK" else "206 Partial Content" val extra = if (from == null) "" else "Content-Range: bytes 0-${data.size - 1}/${data.size}\r\n" out.write("HTTP/1.1 $start\r\n${extra}${validator}Content-Length: ${data.size}\r\nConnection: close\r\n\r\n".toByteArray()) - out.write(content) - out.flush() + if (holdAfterFirstChunk) { + out.write(content, 0, 1024) + out.flush() + bodyStarted.countDown() + continueBody.await(5, TimeUnit.SECONDS) + } else { + out.write(content) + out.flush() + } } else if (from == null) { out.write("HTTP/1.1 200 OK\r\n${validator}Content-Length: ${data.size}\r\nConnection: close\r\n\r\n".toByteArray()) out.write(content, 0, cutAt) @@ -181,6 +194,25 @@ class DownloaderResumeTest { assertTrue("the transfer stopped early (kept=${target.length()})", target.length() in 1 until data.size) } + @Test fun cancellationDisconnectsAStalledTransfer() { + val url = "http://127.0.0.1:${server.localPort}/first" + val target = File(dir, "runtime.tar.zst") + misbehave = true + holdAfterFirstChunk = true + val cancellation = Downloader.Cancellation() + val executor = Executors.newSingleThreadExecutor() + try { + val result = executor.submit { Downloader.downloadFile(url, target, true, null, cancellation, null) } + assertTrue("server sent the first chunk", bodyStarted.await(2, TimeUnit.SECONDS)) + cancellation.cancel() + assertFalse("cancelled download", result.get(2, TimeUnit.SECONDS)) + assertEquals(listOf(null), requests.toList()) + } finally { + continueBody.countDown() + executor.shutdownNow() + } + } + @Test fun aCancelBeforeTheFirstRequestSendsNothing() { val url = "http://127.0.0.1:${server.localPort}/runtime.tar.zst" val target = File(dir, "runtime.tar.zst") From 0ab2172857a2c4ea89998c77a02bd6700fd6227f Mon Sep 17 00:00:00 2001 From: xXJsonDeruloXx Date: Thu, 22 Oct 2026 09:57:45 -0400 Subject: [PATCH 5/9] fix: cancel Proton seed downloads promptly --- .../com/droiddeck/launcher/SessionActivity.kt | 8 +++++++- .../com/droiddeck/launcher/core/Downloader.java | 16 ++++++++++++++-- .../launcher/core/DownloaderResumeTest.kt | 5 ++++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 7f1ac67165..ae90ed4d8a 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -1452,7 +1452,13 @@ 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, { SessionState.stopRequested }, progressFor(PROTON_LINES, entry.size / 1_000_000)) + val cancellation = com.droiddeck.launcher.core.Downloader.Cancellation { SessionState.stopRequested } + SessionState.installCancellation = cancellation + return try { + catalog.install(this, entry, cancellation, progressFor(PROTON_LINES, entry.size / 1_000_000)) + } finally { + if (SessionState.installCancellation === cancellation) SessionState.installCancellation = null + } } 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 dcf3f0d950..44072d2de3 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/Downloader.java +++ b/app/src/main/java/com/droiddeck/launcher/core/Downloader.java @@ -23,6 +23,12 @@ public final class Downloader { private static final int CONNECT_TIMEOUT_MS = 10_000; private static final int STALL_TIMEOUT_MS = 15_000; private static final int HTTP_RANGE_NOT_SATISFIABLE = 416; + private static final java.util.concurrent.ExecutorService CANCEL_EXECUTOR = + java.util.concurrent.Executors.newCachedThreadPool(r -> { + Thread thread = new Thread(r, "download-cancel"); + thread.setDaemon(true); + return thread; + }); private Downloader() {} @@ -37,7 +43,13 @@ public final class Downloader { public void cancel() { cancelled.set(true); HttpURLConnection active = connection.get(); - if (active != null) active.disconnect(); + if (active != null) disconnectAsync(active); + } + + private static void disconnectAsync(HttpURLConnection active) { + CANCEL_EXECUTOR.execute(() -> { + try { active.disconnect(); } catch (RuntimeException ignored) { } + }); } @Override public boolean getAsBoolean() { @@ -46,7 +58,7 @@ public final class Downloader { private void attach(HttpURLConnection active) { connection.set(active); - if (getAsBoolean()) active.disconnect(); + if (getAsBoolean()) disconnectAsync(active); } private void detach(HttpURLConnection active) { connection.compareAndSet(active, null); } 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 da6fec27b9..e973504c34 100644 --- a/app/src/test/java/com/droiddeck/launcher/core/DownloaderResumeTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/core/DownloaderResumeTest.kt @@ -201,15 +201,18 @@ class DownloaderResumeTest { holdAfterFirstChunk = true val cancellation = Downloader.Cancellation() val executor = Executors.newSingleThreadExecutor() + val cancelExecutor = Executors.newSingleThreadExecutor() try { val result = executor.submit { Downloader.downloadFile(url, target, true, null, cancellation, null) } assertTrue("server sent the first chunk", bodyStarted.await(2, TimeUnit.SECONDS)) - cancellation.cancel() + val cancelled = cancelExecutor.submit { cancellation.cancel() } + cancelled.get(2, TimeUnit.SECONDS) assertFalse("cancelled download", result.get(2, TimeUnit.SECONDS)) assertEquals(listOf(null), requests.toList()) } finally { continueBody.countDown() executor.shutdownNow() + cancelExecutor.shutdownNow() } } From 62d218fe3751b66665bab602449cf6bbaa88b54a Mon Sep 17 00:00:00 2001 From: ovurrsl Date: Thu, 22 Oct 2026 19:36:10 +0300 Subject: [PATCH 6/9] fix: cancelling the first-run Proton install also stops its catalog download installProton fetched the catalog before its cancellation existed, so a stop had to wait for the catalog request. The cancellation is now registered first and passed through DesktopCatalog.fetch to a cancellable downloadString, which disconnects the request and does not count a cancelled fetch as a source failure. --- .../com/droiddeck/launcher/SessionActivity.kt | 4 ++-- .../droiddeck/launcher/core/Downloader.java | 21 +++++++++++++++++-- .../launcher/runtime/DesktopCatalog.kt | 4 ++-- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 48ca0d4de7..fb3edbb4ac 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -1635,11 +1635,11 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private fun installProton(): String? { uiHandler.post { loading.percent = -1; loading.say(getString(PROTON_LINES.downloading), PROTON_LINES.topic, readable = true) } 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" val cancellation = com.droiddeck.launcher.core.Downloader.Cancellation { SessionState.stopRequested } SessionState.installCancellation = cancellation return try { + val entry = catalog.fetch(catalog.STEAM_SEED_URL, cancellation)?.firstOrNull { it.id == catalog.PROTON_SEED_ID } + ?: return if (cancellation.asBoolean) "cancelled" else "catalog unreachable" catalog.install(this, entry, cancellation, progressFor(PROTON_LINES, entry.size / 1_000_000)) } finally { if (SessionState.installCancellation === cancellation) SessionState.installCancellation = null 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 8a56876714..1b8f61c3fb 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/Downloader.java +++ b/app/src/main/java/com/droiddeck/launcher/core/Downloader.java @@ -65,10 +65,17 @@ public final class Downloader { } public static String downloadString(String url) { + return downloadString(url, null); + } + + public static String downloadString(String url, java.util.function.BooleanSupplier cancelled) { List sources = DownloadSource.INSTANCE.candidates(url); for (int i = 0; i < sources.size(); i++) { + if (isCancelled(cancelled)) return null; if (i > 0) Log.i(TAG, "retrying " + url + " via " + sources.get(i)); - Fetch fetch = fetch(sources.get(i)); + Fetch fetch = fetch(sources.get(i), cancelled); + // A cancelled fetch says nothing about the source, so it must not count as a failure. + if (isCancelled(cancelled)) return null; if (fetch.body != null) { DownloadSource.INSTANCE.succeeded(sources.get(i)); return fetch.body; @@ -325,9 +332,15 @@ public final class Downloader { } private static Fetch fetch(String url) { + return fetch(url, null); + } + + private static Fetch fetch(String url, java.util.function.BooleanSupplier cancelled) { HttpURLConnection connection = null; + Cancellation token = cancelled instanceof Cancellation ? (Cancellation) cancelled : null; try { connection = open(url, STALL_TIMEOUT_MS); + if (token != null) token.attach(connection); int code = connection.getResponseCode(); if (code / 100 != 2) { Log.w(TAG, url + " -> HTTP " + code); @@ -339,10 +352,14 @@ public final class Downloader { return new Fetch(new String(out.toByteArray(), StandardCharsets.UTF_8), false); } } catch (Exception e) { + if (isCancelled(cancelled)) return new Fetch(null, false); Log.w(TAG, "GET " + url, e); return new Fetch(null, false); } finally { - if (connection != null) connection.disconnect(); + if (connection != null) { + if (token != null) token.detach(connection); + connection.disconnect(); + } } } 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 30d33050d0..b7977a909d 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/DesktopCatalog.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/DesktopCatalog.kt @@ -22,8 +22,8 @@ object DesktopCatalog { val icon: String, val category: String, ) - fun fetch(url: String = CATALOG_URL): List? { - val body = Downloader.downloadString(url) ?: return null + fun fetch(url: String = CATALOG_URL, cancelled: java.util.function.BooleanSupplier? = null): List? { + val body = Downloader.downloadString(url, cancelled) ?: return null return try { val json = JSONObject(body) val arr = json.getJSONArray("packages") From b2185be039f9fb23c44030de4e84b1fc3dfc1c6a Mon Sep 17 00:00:00 2001 From: XxJSONDeruloxX Date: Fri, 23 Oct 2026 11:26:41 -0400 Subject: [PATCH 7/9] fix: close stalled download streams on cancel --- .../droiddeck/launcher/core/Downloader.java | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) 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 1b8f61c3fb..7a88f6beb4 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/Downloader.java +++ b/app/src/main/java/com/droiddeck/launcher/core/Downloader.java @@ -35,6 +35,7 @@ public final class Downloader { public static final class Cancellation implements java.util.function.BooleanSupplier { private final AtomicBoolean cancelled = new AtomicBoolean(); private final AtomicReference connection = new AtomicReference<>(); + private final AtomicReference input = new AtomicReference<>(); private final java.util.function.BooleanSupplier external; public Cancellation() { this(null); } @@ -44,6 +45,8 @@ public final class Downloader { cancelled.set(true); HttpURLConnection active = connection.get(); if (active != null) disconnectAsync(active); + InputStream activeInput = input.get(); + if (activeInput != null) closeAsync(activeInput); } private static void disconnectAsync(HttpURLConnection active) { @@ -52,6 +55,12 @@ public final class Downloader { }); } + private static void closeAsync(InputStream active) { + CANCEL_EXECUTOR.execute(() -> { + try { active.close(); } catch (IOException ignored) { } + }); + } + @Override public boolean getAsBoolean() { return cancelled.get() || (external != null && external.getAsBoolean()); } @@ -62,6 +71,13 @@ public final class Downloader { } private void detach(HttpURLConnection active) { connection.compareAndSet(active, null); } + + private void attach(InputStream active) { + input.set(active); + if (getAsBoolean()) closeAsync(active); + } + + private void detach(InputStream active) { input.compareAndSet(active, null); } } public static String downloadString(String url) { @@ -278,7 +294,9 @@ public final class Downloader { identity.etag = strongETag(etag) ? etag : null; saveIdentity(destination, identity, expectedSha256); long written = have; - try (InputStream in = new BufferedInputStream(connection.getInputStream(), 1 << 16); + InputStream input = connection.getInputStream(); + if (cancelled instanceof Cancellation) ((Cancellation) cancelled).attach(input); + try (InputStream in = new BufferedInputStream(input, 1 << 16); OutputStream out = new FileOutputStream(destination, appending)) { byte[] buffer = new byte[1 << 16]; long lastReport = 0; @@ -291,6 +309,8 @@ public final class Downloader { progress.accept(total > 0 ? written / (float) total : -1f); } } + } finally { + if (cancelled instanceof Cancellation) ((Cancellation) cancelled).detach(input); } if (isCancelled(cancelled)) return Outcome.CANCELLED; if (total > 0 && written < total) return Outcome.RETRY; From 9119ae96a54749950391ef46036880e74f016f60 Mon Sep 17 00:00:00 2001 From: ovurrsl Date: Sun, 25 Oct 2026 16:29:57 +0300 Subject: [PATCH 8/9] fix: the stalled-download cancel test gives a loaded CI runner time to tear the transfer down The cancel and the download's return each had 2 s, which a busy release-test runner missed while the test passes locally every time. The held server body still guarantees the download only returns because of the cancel. --- .../java/com/droiddeck/launcher/core/DownloaderResumeTest.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 8461334b3d..f0c1002eb0 100644 --- a/app/src/test/java/com/droiddeck/launcher/core/DownloaderResumeTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/core/DownloaderResumeTest.kt @@ -206,8 +206,8 @@ class DownloaderResumeTest { val result = executor.submit { Downloader.downloadFile(url, target, true, null, cancellation, null) } assertTrue("server sent the first chunk", bodyStarted.await(2, TimeUnit.SECONDS)) val cancelled = cancelExecutor.submit { cancellation.cancel() } - cancelled.get(2, TimeUnit.SECONDS) - assertFalse("cancelled download", result.get(2, TimeUnit.SECONDS)) + cancelled.get(10, TimeUnit.SECONDS) + assertFalse("cancelled download", result.get(10, TimeUnit.SECONDS)) assertEquals(listOf(null), requests.toList()) } finally { continueBody.countDown() From 42b799c1949ce82f72c46c99ee227f072070c594 Mon Sep 17 00:00:00 2001 From: ovurrsl Date: Wed, 28 Oct 2026 00:05:04 +0300 Subject: [PATCH 9/9] runtime: finalize package markers once extraction completes --- .../main/java/com/droiddeck/launcher/runtime/DesktopCatalog.kt | 1 - 1 file changed, 1 deletion(-) 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 f2e4a357c4..094564f5c7 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/DesktopCatalog.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/DesktopCatalog.kt @@ -220,7 +220,6 @@ object DesktopCatalog { if (!LinuxRuntimeInstaller.extract(context, download, root, report)) return context.getString(R.string.deskpkg_extract_failed) } } - if (cancelled?.getAsBoolean() == true) return context.getString(R.string.user_apps_download_failed) if (entry.id == DESKTOP_ID) { cleanLegacy(context, report) val pending = File(root, DesktopRemoval.PENDING)