From 98be0838c4d38aba09c711cd76cf6ef672d50615 Mon Sep 17 00:00:00 2001 From: Kirk Date: Thu, 15 Oct 2026 10:01:17 +0800 Subject: [PATCH] fix: a failed cloud save recovery no longer ends the app (#846) The cloud-recovery thread started at every app start had nothing to catch an error, so one went to CrashHandler, which halts the process with status 2. In unit tests the thread can outlive the test that started it, fail on the torn-down preferences, and take the whole test run down. The error is now logged and the thread ends. Co-authored-by: Kirk <12691869+KirkPatrickJunsay@users.noreply.github.com> --- .../droiddeck/launcher/stores/StoresState.kt | 5 ++- .../stores/CloudRecoveryThreadTest.kt | 40 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 app/src/test/java/com/droiddeck/launcher/stores/CloudRecoveryThreadTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt index 74af04fb80..d353b2545e 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt @@ -233,7 +233,10 @@ object StoresState { val overlay = java.io.File(com.droiddeck.launcher.runtime.LinuxRuntime.rootDir(context), "root/.local/share/droiddeck/epic-overlay") if (overlay.exists()) Thread({ overlay.deleteRecursively(); Log.i(TAG, "removed the old EOS overlay download") }, "epic-overlay-cleanup").start() val app = context.applicationContext - Thread({ CloudSaves.uploadAllDirty(app, "recovery") }, "cloud-recovery").start() + Thread({ + runCatching { CloudSaves.uploadAllDirty(app, "recovery") } + .onFailure { Log.w(TAG, "cloud save recovery failed", it) } + }, "cloud-recovery").start() } internal fun post(block: () -> Unit) = main.post(block) diff --git a/app/src/test/java/com/droiddeck/launcher/stores/CloudRecoveryThreadTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/CloudRecoveryThreadTest.kt new file mode 100644 index 0000000000..63beea565b --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/stores/CloudRecoveryThreadTest.kt @@ -0,0 +1,40 @@ +package com.droiddeck.launcher.stores + +import android.content.Context +import android.content.ContextWrapper +import android.content.SharedPreferences +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [33], manifest = Config.NONE) +class CloudRecoveryThreadTest { + private val uncaught = mutableListOf() + private var previous: Thread.UncaughtExceptionHandler? = null + + @Before fun setUp() { + previous = Thread.getDefaultUncaughtExceptionHandler() + Thread.setDefaultUncaughtExceptionHandler { _, error -> synchronized(uncaught) { uncaught += error } } + } + + @After fun tearDown() { + Thread.setDefaultUncaughtExceptionHandler(previous) + } + + @Test fun aFailingRecoveryUploadDoesNotReachTheCrashHandler() { + val context = object : ContextWrapper(RuntimeEnvironment.getApplication()) { + override fun getApplicationContext(): Context = this + override fun getSharedPreferences(name: String, mode: Int): SharedPreferences = + if (name == "stores.cloud") throw IllegalStateException("preferences are gone") else super.getSharedPreferences(name, mode) + } + StoresState.init(context) + Thread.getAllStackTraces().keys.filter { it.name == "cloud-recovery" }.forEach { it.join(5000) } + assertEquals(emptyList(), synchronized(uncaught) { uncaught.toList() }) + } +}