mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
fix(runtime): record why a runtime install failed in the session events (#2591)
* fix(runtime): record why a runtime install failed in the session events * fix(runtime): report empty or invalid runtime catalogs in the failure event * fix(runtime): keep each install failure reason on its own job * fix(runtime): name the busy reason for rejected installs and drop the owner's listener * fix(runtime): keep each install call's failure reason on its calling thread * fix(runtime): keep the interrupted reason when a joined install finishes during the wait
This commit is contained in:
1 parent
2cb51103ac
commit
aa3a599ec6
8 files changed
+538
-53
No files matched your search
@@ -2235,11 +2235,16 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
|
||||
|
||||
private fun installRuntime(): String? {
|
||||
val release = com.droiddeck.launcher.runtime.LinuxRuntimeInstaller.fetchRelease()
|
||||
?: return getString(R.string.session_runtime_catalog_unreachable)
|
||||
if (release == null) {
|
||||
SessionEvents.record("runtime.failed", mapOf("step" to "catalog", "reason" to com.droiddeck.launcher.runtime.LinuxRuntimeInstaller.catalogError()))
|
||||
return getString(R.string.session_runtime_catalog_unreachable)
|
||||
}
|
||||
runCatching { com.droiddeck.launcher.runtime.RuntimeInstallService.start(this, release) }
|
||||
val ok = com.droiddeck.launcher.runtime.LinuxRuntimeInstaller.install(this, release,
|
||||
progressFor(RUNTIME_LINES, release.size / 1_000_000))
|
||||
return if (ok) null else getString(R.string.session_runtime_install_failed)
|
||||
if (ok) return null
|
||||
SessionEvents.record("runtime.failed", mapOf("step" to "install", "reason" to com.droiddeck.launcher.runtime.LinuxRuntimeInstaller.installError()))
|
||||
return getString(R.string.session_runtime_install_failed)
|
||||
}
|
||||
|
||||
private fun installDesktop(): String? {
|
||||
|
||||
@@ -31,6 +31,9 @@ internal class AddonPack(
|
||||
val state: StateFlow<State> = mutableState.asStateFlow()
|
||||
private val lock = Any()
|
||||
|
||||
@Volatile var failure: String? = null
|
||||
private set
|
||||
|
||||
val available: Boolean get() = sha256.isNotEmpty()
|
||||
private val tag get() = "pack-$name-${sha256.take(16)}"
|
||||
private val urls get() = listOfNotNull(
|
||||
@@ -67,9 +70,13 @@ internal class AddonPack(
|
||||
if (installed(app)) { mutableState.value = State.Ready; return }
|
||||
if (mutableState.value is State.Downloading) return
|
||||
mutableState.value = State.Downloading(0f)
|
||||
failure = null
|
||||
}
|
||||
scope.launch {
|
||||
val ok = runCatching { install(app) }.onFailure { Log.w(TAG, "$name: install failed", it) }.getOrDefault(false)
|
||||
val ok = runCatching { install(app) }.onFailure {
|
||||
Log.w(TAG, "$name: install failed", it)
|
||||
failure = it.toString()
|
||||
}.getOrDefault(false)
|
||||
mutableState.value = if (ok) State.Ready else State.Failed
|
||||
if (ok) onReady(app)
|
||||
}
|
||||
@@ -93,20 +100,28 @@ internal class AddonPack(
|
||||
|
||||
private fun install(context: Context): Boolean {
|
||||
if (!available) {
|
||||
Log.w(TAG, if (reuseDeviceCopy) "$name: this build has no pack and none is installed" else "$name: this build has no pack")
|
||||
failure = if (reuseDeviceCopy) "this build has no pack and none is installed" else "this build has no pack"
|
||||
Log.w(TAG, "$name: $failure")
|
||||
return false
|
||||
}
|
||||
val root = root(context).apply { mkdirs() }
|
||||
val archive = File(root, "$sha256.tar.zst")
|
||||
val problems = mutableListOf<String>()
|
||||
val downloaded = bundled(context, archive) || urls.any { url ->
|
||||
Downloader.downloadFile(url, archive, true, sha256) { p -> mutableState.value = State.Downloading(p.coerceIn(0f, 1f)) }
|
||||
.also { if (!it) archive.delete() }
|
||||
.also { if (!it) { archive.delete(); Downloader.lastFailure()?.let(problems::add) } }
|
||||
}
|
||||
if (!downloaded) {
|
||||
failure = problems.joinToString("; ").ifEmpty { "download failed" }
|
||||
return false
|
||||
}
|
||||
if (!downloaded) return false
|
||||
val staging = File(root, "$sha256.partial")
|
||||
staging.deleteRecursively()
|
||||
try {
|
||||
if (!FileInputStream(archive).use { TarZst.extract(it, staging) }) return false
|
||||
if (!FileInputStream(archive).use { TarZst.extract(it, staging) }) {
|
||||
failure = "could not unpack the pack"
|
||||
return false
|
||||
}
|
||||
check(required.all { File(staging, it).isFile }) { "$name pack is incomplete" }
|
||||
File(staging, COMPLETE).writeText(sha256)
|
||||
val target = dir(context)
|
||||
|
||||
@@ -31,8 +31,26 @@ public final class Downloader {
|
||||
return thread;
|
||||
});
|
||||
|
||||
private static final ThreadLocal<String> LAST_FAILURE = new ThreadLocal<>();
|
||||
|
||||
private Downloader() {}
|
||||
|
||||
public static String lastFailure() {
|
||||
return LAST_FAILURE.get();
|
||||
}
|
||||
|
||||
private static void failed(String url, String reason) {
|
||||
String host;
|
||||
try { host = new java.net.URI(url).getHost(); } catch (java.net.URISyntaxException e) { host = null; }
|
||||
String entry = (host == null ? url : host) + ": " + reason;
|
||||
String earlier = LAST_FAILURE.get();
|
||||
LAST_FAILURE.set(earlier == null ? entry : earlier + "; " + entry);
|
||||
}
|
||||
|
||||
private static String describe(Exception e) {
|
||||
return e.getMessage() == null ? e.getClass().getSimpleName() : e.getClass().getSimpleName() + ": " + e.getMessage();
|
||||
}
|
||||
|
||||
public static final class Cancellation implements java.util.function.BooleanSupplier {
|
||||
private final AtomicBoolean cancelled = new AtomicBoolean();
|
||||
private final AtomicReference<HttpURLConnection> connection = new AtomicReference<>();
|
||||
@@ -87,12 +105,14 @@ public final class Downloader {
|
||||
|
||||
public static String downloadString(String url, java.util.function.BooleanSupplier cancelled) {
|
||||
List<String> sources = DownloadSource.INSTANCE.candidates(url);
|
||||
LAST_FAILURE.remove();
|
||||
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), cancelled);
|
||||
if (isCancelled(cancelled)) return null;
|
||||
if (fetch.body != null) {
|
||||
LAST_FAILURE.remove();
|
||||
DownloadSource.INSTANCE.succeeded(sources.get(i));
|
||||
return fetch.body;
|
||||
}
|
||||
@@ -103,6 +123,7 @@ public final class Downloader {
|
||||
}
|
||||
|
||||
public static String downloadDirect(String url) {
|
||||
LAST_FAILURE.remove();
|
||||
return fetch(url).body;
|
||||
}
|
||||
|
||||
@@ -138,6 +159,7 @@ public final class Downloader {
|
||||
String expectedSha256, java.util.function.BooleanSupplier cancelled,
|
||||
java.util.function.Consumer<Float> progress) {
|
||||
Identity identity = loadIdentity(destination, resume, expectedSha256);
|
||||
LAST_FAILURE.remove();
|
||||
for (int i = 0; i < sources.size(); i++) {
|
||||
String source = sources.get(i);
|
||||
if (isCancelled(cancelled)) return false;
|
||||
@@ -150,12 +172,14 @@ public final class Downloader {
|
||||
if (isCancelled(cancelled)) return false;
|
||||
if (!expectedSha256.equalsIgnoreCase(actualSha256)) {
|
||||
Log.w(TAG, "checksum mismatch from " + source + "; trying another source");
|
||||
failed(source, "checksum mismatch");
|
||||
if (!discardPartial(destination, identity)) return false;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (isCancelled(cancelled)) return false;
|
||||
resumeFile(destination).delete();
|
||||
LAST_FAILURE.remove();
|
||||
DownloadSource.INSTANCE.succeeded(source);
|
||||
if (progress != null) progress.accept(1f);
|
||||
return true;
|
||||
@@ -260,6 +284,7 @@ public final class Downloader {
|
||||
}
|
||||
if (code / 100 != 2) {
|
||||
Log.w(TAG, url + " -> HTTP " + code);
|
||||
failed(url, "HTTP " + code);
|
||||
return code == HttpURLConnection.HTTP_NOT_FOUND || code == HttpURLConnection.HTTP_GONE
|
||||
? Outcome.FATAL : Outcome.RETRY;
|
||||
}
|
||||
@@ -308,11 +333,15 @@ public final class Downloader {
|
||||
if (cancelled instanceof Cancellation) ((Cancellation) cancelled).detach(input);
|
||||
}
|
||||
if (isCancelled(cancelled)) return Outcome.CANCELLED;
|
||||
if (total > 0 && written < total) return Outcome.RETRY;
|
||||
if (total > 0 && written < total) {
|
||||
failed(url, "connection closed after " + written + " of " + total + " bytes");
|
||||
return Outcome.RETRY;
|
||||
}
|
||||
return Outcome.DONE;
|
||||
} catch (Exception e) {
|
||||
if (isCancelled(cancelled)) return Outcome.CANCELLED;
|
||||
Log.w(TAG, "download " + url, e);
|
||||
failed(url, describe(e));
|
||||
return Outcome.RETRY;
|
||||
} finally {
|
||||
if (connection != null) {
|
||||
@@ -357,6 +386,7 @@ public final class Downloader {
|
||||
int code = connection.getResponseCode();
|
||||
if (code / 100 != 2) {
|
||||
Log.w(TAG, url + " -> HTTP " + code);
|
||||
failed(url, "HTTP " + code);
|
||||
return new Fetch(null, code == HttpURLConnection.HTTP_NOT_FOUND || code == HttpURLConnection.HTTP_GONE);
|
||||
}
|
||||
try (InputStream in = connection.getInputStream();
|
||||
@@ -367,6 +397,7 @@ public final class Downloader {
|
||||
} catch (Exception e) {
|
||||
if (isCancelled(cancelled)) return new Fetch(null, false);
|
||||
Log.w(TAG, "GET " + url, e);
|
||||
failed(url, describe(e));
|
||||
return new Fetch(null, false);
|
||||
} finally {
|
||||
if (connection != null) {
|
||||
|
||||
@@ -70,6 +70,7 @@ public final class LinuxRuntimeInstaller {
|
||||
volatile Step step = Step.OTHER;
|
||||
volatile int percent = -1;
|
||||
volatile boolean ok;
|
||||
volatile String failure;
|
||||
final boolean removal;
|
||||
boolean maintenance;
|
||||
Job(boolean removal, String stage) { this.removal = removal; this.stage = stage; }
|
||||
@@ -78,6 +79,9 @@ public final class LinuxRuntimeInstaller {
|
||||
private static final Object JOB_LOCK = new Object();
|
||||
private static Job running;
|
||||
private static volatile String removalError;
|
||||
private static final ThreadLocal<Job> LAST_INSTALL = new ThreadLocal<>();
|
||||
private static final ThreadLocal<Job> CURRENT_INSTALL = new ThreadLocal<>();
|
||||
private static final ThreadLocal<String> CATALOG_ERROR = new ThreadLocal<>();
|
||||
|
||||
private LinuxRuntimeInstaller() {}
|
||||
|
||||
@@ -105,34 +109,66 @@ public final class LinuxRuntimeInstaller {
|
||||
}
|
||||
|
||||
public static Release fetchRelease() {
|
||||
String body = Downloader.downloadString(catalogUrl());
|
||||
if (body == null || body.isEmpty()) return null;
|
||||
return fetchRelease(catalogUrl());
|
||||
}
|
||||
|
||||
public static String catalogError() { return CATALOG_ERROR.get(); }
|
||||
|
||||
static Release fetchRelease(String catalogUrl) {
|
||||
CATALOG_ERROR.remove();
|
||||
String body = Downloader.downloadString(catalogUrl);
|
||||
if (body == null) return catalogFailed(Downloader.lastFailure());
|
||||
if (body.isEmpty()) return catalogFailed("empty catalog");
|
||||
try {
|
||||
JSONObject json = new JSONObject(body);
|
||||
String version = json.optString("version", "");
|
||||
String url = json.optString("url", "");
|
||||
String sha256 = json.optString("sha256", "");
|
||||
if (version.isEmpty() || url.isEmpty() || sha256.isEmpty()) return null;
|
||||
if (version.isEmpty() || url.isEmpty() || sha256.isEmpty()) {
|
||||
return catalogFailed("catalog lacks version, url or sha256");
|
||||
}
|
||||
return new Release(version, url, sha256, json.optLong("size", 0L));
|
||||
} catch (Exception e) {
|
||||
Log.w(TAG, "catalog: " + e);
|
||||
return null;
|
||||
return catalogFailed("invalid catalog: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
private static Release catalogFailed(String reason) {
|
||||
CATALOG_ERROR.set(reason);
|
||||
Log.w(TAG, "catalog: " + reason);
|
||||
return null;
|
||||
}
|
||||
|
||||
private static final String USER_DATA = "root";
|
||||
|
||||
public static boolean install(Context context, Release release, ProgressListener listener) {
|
||||
Job job;
|
||||
boolean owner;
|
||||
synchronized (JOB_LOCK) {
|
||||
if (running != null && (running.removal || running.maintenance)) return false;
|
||||
if (running != null && (running.removal || running.maintenance)) {
|
||||
Job rejected = new Job(false, null);
|
||||
rejected.failure = running.removal ? "Runtime busy: removal in progress" : "Runtime busy: maintenance in progress";
|
||||
Log.w(TAG, rejected.failure);
|
||||
LAST_INSTALL.set(rejected);
|
||||
return false;
|
||||
}
|
||||
owner = running == null;
|
||||
if (owner) { running = new Job(false, context.getString(R.string.user_apps_starting)); removalError = null; }
|
||||
job = running;
|
||||
if (listener != null) job.listeners.add(listener);
|
||||
}
|
||||
if (!owner) return join(job, listener);
|
||||
if (!owner) {
|
||||
Boolean ok = await(job, listener);
|
||||
if (ok != null) {
|
||||
LAST_INSTALL.set(job);
|
||||
return ok;
|
||||
}
|
||||
Job interrupted = new Job(false, null);
|
||||
interrupted.failure = "Runtime install interrupted while waiting for the running install";
|
||||
LAST_INSTALL.set(interrupted);
|
||||
return false;
|
||||
}
|
||||
CURRENT_INSTALL.set(job);
|
||||
try {
|
||||
Context app = context.getApplicationContext();
|
||||
ProgressListener progress = new ProgressListener() {
|
||||
@@ -156,9 +192,10 @@ public final class LinuxRuntimeInstaller {
|
||||
if (job.ok && desktop) keepDesktop(app, progress);
|
||||
return job.ok;
|
||||
} finally {
|
||||
synchronized (JOB_LOCK) {
|
||||
running = null;
|
||||
}
|
||||
CURRENT_INSTALL.remove();
|
||||
LAST_INSTALL.set(job);
|
||||
synchronized (JOB_LOCK) { running = null; }
|
||||
if (listener != null) job.listeners.remove(listener);
|
||||
job.done.countDown();
|
||||
}
|
||||
}
|
||||
@@ -247,6 +284,21 @@ public final class LinuxRuntimeInstaller {
|
||||
|
||||
public static String removalError() { return removalError; }
|
||||
|
||||
public static String installError() {
|
||||
Job job = LAST_INSTALL.get();
|
||||
return job == null ? null : job.failure;
|
||||
}
|
||||
|
||||
private static boolean fail(String step, String detail) {
|
||||
String reason = detail == null || detail.isEmpty() ? step : step + ": " + detail;
|
||||
Job job = CURRENT_INSTALL.get();
|
||||
if (job != null) job.failure = reason;
|
||||
Log.w(TAG, reason);
|
||||
return false;
|
||||
}
|
||||
|
||||
private static final String SWAP_FAILED = "Cannot replace the previous runtime";
|
||||
|
||||
public static boolean hasRemovalPending(Context context) {
|
||||
return removalDirectory(LinuxRuntime.rootDir(context)).exists();
|
||||
}
|
||||
@@ -266,13 +318,17 @@ public final class LinuxRuntimeInstaller {
|
||||
}
|
||||
|
||||
private static boolean join(Job job, ProgressListener listener) {
|
||||
return Boolean.TRUE.equals(await(job, listener));
|
||||
}
|
||||
|
||||
private static Boolean await(Job job, ProgressListener listener) {
|
||||
try {
|
||||
if (listener != null) listener.onProgress(job.step, job.stage, job.percent);
|
||||
job.done.await();
|
||||
return job.ok;
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
return false;
|
||||
return null;
|
||||
} finally {
|
||||
if (listener != null) job.listeners.remove(listener);
|
||||
}
|
||||
@@ -329,8 +385,9 @@ public final class LinuxRuntimeInstaller {
|
||||
if (listener != null) listener.onProgress(Step.DOWNLOADING, downloading, 0);
|
||||
if (!LinuxRuntime.patchedProot(context) && !AddonPack.PROOT.await(context, () -> true, packProgress(listener, downloading))
|
||||
&& !SessionPrefs.allowStockProot(context))
|
||||
throw new IOException("Cannot download proot");
|
||||
if (!AddonPack.DIRECTAUDIO.await(context, () -> true, packProgress(listener, downloading))) throw new IOException("Cannot download DirectAudio");
|
||||
return fail("Cannot download proot", AddonPack.PROOT.getFailure());
|
||||
if (!AddonPack.DIRECTAUDIO.await(context, () -> true, packProgress(listener, downloading)))
|
||||
return fail("Cannot download DirectAudio", AddonPack.DIRECTAUDIO.getFailure());
|
||||
boolean ok = Downloader.downloadFile(release.url, archive, true, release.sha256, (fraction) -> {
|
||||
if (listener != null) {
|
||||
listener.onProgress(Step.DOWNLOADING, downloading,
|
||||
@@ -338,39 +395,36 @@ public final class LinuxRuntimeInstaller {
|
||||
}
|
||||
});
|
||||
if (!ok) {
|
||||
Log.w(TAG, "download failed");
|
||||
keepPartial = true;
|
||||
return false;
|
||||
return fail("Cannot download the runtime", Downloader.lastFailure());
|
||||
}
|
||||
|
||||
if (listener != null) listener.onProgress(Step.VERIFYING, context.getString(R.string.rtinst_verifying), -1);
|
||||
String actual = Hashes.sha256(archive);
|
||||
if (!release.sha256.equalsIgnoreCase(actual)) {
|
||||
Log.w(TAG, "checksum mismatch: wanted " + release.sha256 + ", got " + actual);
|
||||
return false;
|
||||
}
|
||||
if (!release.sha256.equalsIgnoreCase(actual))
|
||||
return fail("Runtime checksum mismatch", "wanted " + release.sha256 + ", got " + actual);
|
||||
|
||||
File root = LinuxRuntime.rootDir(context);
|
||||
File staging = new File(root.getParentFile(), LinuxRuntime.DIR + ".new");
|
||||
File old = new File(root.getParentFile(), LinuxRuntime.DIR + ".old");
|
||||
recoverInterruptedSwap(root, staging, old);
|
||||
if (!discard(staging, null) || !staging.mkdirs()) return false;
|
||||
if (!discard(staging, null) || !staging.mkdirs()) return fail("Cannot prepare " + staging.getName(), null);
|
||||
if (listener != null) listener.onProgress(context.getString(R.string.rtinst_extracting), -1);
|
||||
if (!extract(context, archive, staging, root, listener)) {
|
||||
RuntimeFileTree.delete(staging, null);
|
||||
return false;
|
||||
return fail("Cannot unpack the runtime", null);
|
||||
}
|
||||
FileUtils.writeString(new File(staging, VERSION_FILE), release.version);
|
||||
|
||||
if (!discard(old, null)) {
|
||||
discard(staging, null);
|
||||
return false;
|
||||
return fail(SWAP_FAILED, null);
|
||||
}
|
||||
java.util.List<String> carryPaths = root.isDirectory()
|
||||
? RuntimeFileTree.existingUserPaths(root, staging) : java.util.Collections.emptyList();
|
||||
if (root.isDirectory() && !root.renameTo(old)) {
|
||||
RuntimeFileTree.delete(staging, null);
|
||||
return false;
|
||||
return fail(SWAP_FAILED, null);
|
||||
}
|
||||
File carryMarker = new File(staging, CARRY_MARKER);
|
||||
java.util.List<String> encodedPaths = new java.util.ArrayList<>();
|
||||
@@ -379,7 +433,7 @@ public final class LinuxRuntimeInstaller {
|
||||
if (old.isDirectory() && !old.renameTo(root))
|
||||
Log.w(TAG, "could not restore the previous runtime; retained at " + old);
|
||||
discard(staging, null);
|
||||
return false;
|
||||
return fail(SWAP_FAILED, null);
|
||||
}
|
||||
|
||||
java.util.List<String> carried;
|
||||
@@ -396,7 +450,7 @@ public final class LinuxRuntimeInstaller {
|
||||
} else {
|
||||
Log.w(TAG, "incomplete rollback; retaining user data in " + staging);
|
||||
}
|
||||
return false;
|
||||
return fail("Cannot carry user data into the new runtime", e.getMessage());
|
||||
}
|
||||
|
||||
if (!staging.renameTo(root)) {
|
||||
@@ -407,7 +461,7 @@ public final class LinuxRuntimeInstaller {
|
||||
} catch (IOException e) { Log.w(TAG, "could not move all user data back after a failed swap", e); }
|
||||
old.renameTo(root);
|
||||
}
|
||||
return false;
|
||||
return fail(SWAP_FAILED, null);
|
||||
}
|
||||
new File(root, CARRY_MARKER).delete();
|
||||
try { RuntimeFileTree.dropStaleState(root); }
|
||||
@@ -415,10 +469,10 @@ public final class LinuxRuntimeInstaller {
|
||||
discard(old, null);
|
||||
try { com.droiddeck.launcher.session.SessionFiles.INSTANCE.stage(context, root, false); }
|
||||
catch (Exception e) { Log.w(TAG, "could not stage the session files into the new runtime", e); }
|
||||
return LinuxRuntime.isInstalled(context);
|
||||
return LinuxRuntime.isInstalled(context) || fail("The runtime is incomplete after unpacking", null);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "install", e);
|
||||
return false;
|
||||
return fail("Install stopped", e.toString());
|
||||
} finally {
|
||||
if (!keepPartial) archive.delete();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user