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:
MaxsTechReview authored and GitHub committed 2026-11-14 14:51:29 -05:00
1 parent 2cb51103ac
commit aa3a599ec6
8 files changed
+538 -53

No files matched your search

@@ -74,6 +74,13 @@ class AddonPackTest {
assertEquals("a".repeat(64), pack.dir(context).name)
}
@Test fun aBuildWithoutThePackSaysSoWhenAwaited() {
val pack = AddonPack("sample", "", 0L, listOf("tool"), reuseDeviceCopy = false)
assertFalse(pack.await(context))
assertEquals("this build has no pack", pack.failure)
}
@Test fun localBuildInstallsTheBundledPackWithoutDownloading() {
val archive = tarZst("tool" to "x")
val pack = AddonPack("sample", Hashes.sha256(archive), archive.length(), listOf("tool"), bundle = { archive.inputStream() })
@@ -0,0 +1,85 @@
package com.droiddeck.launcher.core
import java.io.File
import java.net.ServerSocket
import java.nio.file.Files
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
class DownloaderFailureTest {
private lateinit var dir: File
@Before fun setUp() {
dir = Files.createTempDirectory("downloader-failure").toFile()
}
@After fun tearDown() {
dir.deleteRecursively()
}
private fun serve(status: String, body: ByteArray = ByteArray(0)): ServerSocket {
val server = ServerSocket(0)
Thread {
runCatching {
server.accept().use { socket ->
val input = socket.getInputStream().bufferedReader(Charsets.ISO_8859_1)
while (true) {
val line = input.readLine() ?: break
if (line.isEmpty()) break
}
socket.getOutputStream().apply {
write("HTTP/1.1 $status\r\nContent-Length: ${body.size}\r\nConnection: close\r\n\r\n".toByteArray())
write(body)
flush()
}
}
}
}.apply { isDaemon = true; start() }
return server
}
private fun download(url: String): Boolean =
Downloader.downloadCandidates(url, listOf(url), File(dir, "file"), false, null, null)
@Test fun anHttpRefusalIsReportedWithTheHostAndCode() {
serve("403 Forbidden").use { server ->
assertFalse(download("http://127.0.0.1:${server.localPort}/pack.tar.zst"))
}
assertEquals("127.0.0.1: HTTP 403", Downloader.lastFailure())
}
@Test fun aRefusedConnectionIsReportedWithItsException() {
val port = ServerSocket(0).use { it.localPort }
assertFalse(download("http://127.0.0.1:$port/pack.tar.zst"))
val failure = Downloader.lastFailure().orEmpty()
assertTrue(failure, failure.startsWith("127.0.0.1: ConnectException"))
}
@Test fun aSuccessfulDownloadClearsTheEarlierFailure() {
serve("500 Internal Server Error").use { server ->
assertFalse(download("http://127.0.0.1:${server.localPort}/pack.tar.zst"))
}
serve("200 OK", "data".toByteArray()).use { server ->
assertTrue(download("http://127.0.0.1:${server.localPort}/pack.tar.zst"))
}
assertNull(Downloader.lastFailure())
}
@Test fun everyFailedSourceIsListedInOrder() {
val first = serve("403 Forbidden")
val second = serve("503 Service Unavailable")
val urls = listOf(first, second).map { "http://127.0.0.1:${it.localPort}/pack.tar.zst" }
try {
assertFalse(Downloader.downloadCandidates(urls[0], urls, File(dir, "file"), false, null, null))
} finally {
first.close()
second.close()
}
assertEquals("127.0.0.1: HTTP 403; 127.0.0.1: HTTP 503", Downloader.lastFailure())
}
}
@@ -0,0 +1,72 @@
package com.droiddeck.launcher.runtime
import android.app.Application
import java.net.ServerSocket
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], application = Application::class)
class RuntimeCatalogFailureTest {
private fun serve(body: String): ServerSocket {
val server = ServerSocket(0)
val bytes = body.toByteArray()
Thread {
runCatching {
server.accept().use { socket ->
val input = socket.getInputStream().bufferedReader(Charsets.ISO_8859_1)
while (true) {
val line = input.readLine() ?: break
if (line.isEmpty()) break
}
socket.getOutputStream().apply {
write("HTTP/1.1 200 OK\r\nContent-Length: ${bytes.size}\r\nConnection: close\r\n\r\n".toByteArray())
write(bytes)
flush()
}
}
}
}.apply { isDaemon = true; start() }
return server
}
private fun fetch(body: String): LinuxRuntimeInstaller.Release? =
serve(body).use { LinuxRuntimeInstaller.fetchRelease("http://127.0.0.1:${it.localPort}/linuxfs.json") }
@Test fun anEmptyCatalogIsReported() {
assertNull(fetch(""))
assertEquals("empty catalog", LinuxRuntimeInstaller.catalogError())
}
@Test fun aMalformedCatalogIsReported() {
assertNull(fetch("<html>captive portal</html>"))
val error = LinuxRuntimeInstaller.catalogError().orEmpty()
assertTrue(error, error.startsWith("invalid catalog: "))
}
@Test fun aCatalogMissingFieldsIsReported() {
assertNull(fetch("""{"version":"1","url":"http://127.0.0.1/rootfs.tar.zst"}"""))
assertEquals("catalog lacks version, url or sha256", LinuxRuntimeInstaller.catalogError())
}
@Test fun anUnreachableCatalogKeepsTheDownloadReason() {
val port = ServerSocket(0).use { it.localPort }
assertNull(LinuxRuntimeInstaller.fetchRelease("http://127.0.0.1:$port/linuxfs.json"))
val error = LinuxRuntimeInstaller.catalogError().orEmpty()
assertTrue(error, error.startsWith("127.0.0.1: ConnectException"))
}
@Test fun aValidCatalogClearsTheEarlierError() {
assertNull(fetch(""))
val release = fetch("""{"version":"2","url":"http://127.0.0.1/rootfs.tar.zst","sha256":"${"a".repeat(64)}","size":5}""")
assertNotNull(release)
assertEquals("2", release!!.version)
assertNull(LinuxRuntimeInstaller.catalogError())
}
}
@@ -40,7 +40,7 @@ class RuntimeInstallRollbackTest {
private data class Attempt(val root: File, val staging: File, val old: File, val result: Boolean)
private fun attempt(failRollback: Boolean): Attempt {
private fun packedContext(): Context {
val files = tmp.newFolder("files")
val cache = tmp.newFolder("cache")
val context = object : ContextWrapper(RuntimeEnvironment.getApplication()) {
@@ -58,6 +58,29 @@ class RuntimeInstallRollbackTest {
required.forEach { put(directory, it, "test pack") }
put(directory, AddonPack.COMPLETE, version)
}
return context
}
private fun serveOnce(server: ServerSocket, head: String, body: ByteArray = ByteArray(0)) = Thread {
runCatching {
server.accept().use { socket ->
socket.soTimeout = 5000
val input = socket.getInputStream().bufferedReader(Charsets.ISO_8859_1)
while (true) {
val line = input.readLine() ?: break
if (line.isEmpty()) break
}
val out = socket.getOutputStream()
out.write(("$head\r\nContent-Length: ${body.size}\r\nConnection: close\r\n\r\n").toByteArray())
out.write(body)
out.flush()
}
}
}.apply { isDaemon = true; start() }
private fun attempt(failRollback: Boolean): Attempt {
val context = packedContext()
val files = context.filesDir
val root = LinuxRuntime.rootDir(context)
val staging = File(files, LinuxRuntime.DIR + ".new")
val old = File(files, LinuxRuntime.DIR + ".old")
@@ -87,22 +110,7 @@ class RuntimeInstallRollbackTest {
} else failRollback && path == File(old, "root/rollback-block").toPath()
}
val server = ServerSocket(0)
val thread = Thread {
runCatching {
server.accept().use { socket ->
socket.soTimeout = 5000
val input = socket.getInputStream().bufferedReader(Charsets.ISO_8859_1)
while (true) {
val line = input.readLine() ?: break
if (line.isEmpty()) break
}
val out = socket.getOutputStream()
out.write(("HTTP/1.1 200 OK\r\nContent-Length: ${payload.size}\r\nConnection: close\r\n\r\n").toByteArray())
out.write(payload)
out.flush()
}
}
}.apply { isDaemon = true; start() }
val thread = serveOnce(server, "HTTP/1.1 200 OK", payload)
try {
val release = LinuxRuntimeInstaller.Release("synthetic", "http://127.0.0.1:${server.localPort}/runtime.tar.zst", digest, payload.size.toLong())
val result = LinuxRuntimeInstaller.install(context, release, null)
@@ -137,4 +145,212 @@ class RuntimeInstallRollbackTest {
assertEquals("original app", File(attempt.root, "opt/appimages/user/game/app.AppImage").readText())
assertFalse(attempt.staging.exists())
}
private fun failedInstall(context: Context, head: String): String? {
val server = ServerSocket(0)
val thread = serveOnce(server, head)
try {
val release = LinuxRuntimeInstaller.Release("synthetic", "http://127.0.0.1:${server.localPort}/runtime.tar.zst", "a".repeat(64), 1L)
assertFalse(LinuxRuntimeInstaller.install(context, release, null))
return LinuxRuntimeInstaller.installError()
} finally {
server.close()
thread.join(1000)
}
}
@Test fun aRefusedRuntimeDownloadNamesTheStepAndTheServerAnswer() {
val context = packedContext()
assertEquals("Cannot download the runtime: 127.0.0.1: HTTP 403", failedInstall(context, "HTTP/1.1 403 Forbidden"))
}
@Test fun eachInstallReportsItsOwnFailureOnly() {
val context = packedContext()
failedInstall(context, "HTTP/1.1 403 Forbidden")
assertEquals("Cannot download the runtime: 127.0.0.1: HTTP 404", failedInstall(context, "HTTP/1.1 404 Not Found"))
}
@Test fun anInstallInFlightKeepsTheLastFailureReadable() {
val context = packedContext()
val first = failedInstall(context, "HTTP/1.1 403 Forbidden")
val server = ServerSocket(0)
val accepted = java.util.concurrent.CountDownLatch(1)
val release = java.util.concurrent.CountDownLatch(1)
val stall = Thread {
runCatching {
server.accept().use { socket ->
accepted.countDown()
release.await()
socket.getOutputStream().write("HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".toByteArray())
}
}
}.apply { isDaemon = true; start() }
var secondReason: String? = null
val second = Thread {
LinuxRuntimeInstaller.install(context,
LinuxRuntimeInstaller.Release("synthetic", "http://127.0.0.1:${server.localPort}/runtime.tar.zst", "a".repeat(64), 1L), null)
secondReason = LinuxRuntimeInstaller.installError()
}.apply { isDaemon = true; start() }
try {
assertTrue(accepted.await(10, java.util.concurrent.TimeUnit.SECONDS))
assertTrue(LinuxRuntimeInstaller.isInstalling())
assertEquals(first, LinuxRuntimeInstaller.installError())
} finally {
release.countDown()
second.join(10000)
server.close()
stall.join(1000)
}
assertEquals("Cannot download the runtime: 127.0.0.1: HTTP 404", secondReason)
assertEquals(first, LinuxRuntimeInstaller.installError())
}
@Test fun anInstallRejectedDuringMaintenanceReportsItsOwnReason() {
val context = packedContext()
failedInstall(context, "HTTP/1.1 403 Forbidden")
val maintenance = LinuxRuntimeInstaller.beginMaintenance(context, "maintenance")!!
try {
assertFalse(LinuxRuntimeInstaller.install(context,
LinuxRuntimeInstaller.Release("synthetic", "http://127.0.0.1:1/runtime.tar.zst", "a".repeat(64), 1L), null))
assertEquals("Runtime busy: maintenance in progress", LinuxRuntimeInstaller.installError())
} finally {
assertTrue(maintenance.run(null) { null })
}
assertEquals("Cannot download the runtime: 127.0.0.1: HTTP 404", failedInstall(context, "HTTP/1.1 404 Not Found"))
}
@Test fun aRejectedInstallKeepsItsBusyReasonAfterALaterInstallCompletes() {
val context = packedContext()
val maintenance = LinuxRuntimeInstaller.beginMaintenance(context, "maintenance")!!
try {
assertFalse(LinuxRuntimeInstaller.install(context,
LinuxRuntimeInstaller.Release("synthetic", "http://127.0.0.1:1/runtime.tar.zst", "a".repeat(64), 1L), null))
} finally {
assertTrue(maintenance.run(null) { null })
}
var later: String? = null
Thread { later = failedInstall(context, "HTTP/1.1 404 Not Found") }.apply { start(); join(10000) }
assertEquals("Cannot download the runtime: 127.0.0.1: HTTP 404", later)
assertEquals("Runtime busy: maintenance in progress", LinuxRuntimeInstaller.installError())
}
@Test fun aJoinedInstallReportsTheReasonOfTheInstallItJoined() {
val context = packedContext()
val server = ServerSocket(0)
val accepted = java.util.concurrent.CountDownLatch(1)
val answer = java.util.concurrent.CountDownLatch(1)
val attached = java.util.concurrent.CountDownLatch(1)
val joinedDone = java.util.concurrent.CountDownLatch(1)
val readNow = java.util.concurrent.CountDownLatch(1)
val stall = Thread {
runCatching {
server.accept().use { socket ->
accepted.countDown()
answer.await()
socket.getOutputStream().write("HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".toByteArray())
}
}
}.apply { isDaemon = true; start() }
val release = LinuxRuntimeInstaller.Release("synthetic", "http://127.0.0.1:${server.localPort}/runtime.tar.zst", "a".repeat(64), 1L)
val owner = Thread { LinuxRuntimeInstaller.install(context, release, null) }.apply { isDaemon = true; start() }
var joinedResult = true
var joinedReason: String? = null
val joiner = Thread {
joinedResult = LinuxRuntimeInstaller.install(context, release) { _, _ -> attached.countDown() }
joinedDone.countDown()
readNow.await()
joinedReason = LinuxRuntimeInstaller.installError()
}
try {
assertTrue(accepted.await(10, java.util.concurrent.TimeUnit.SECONDS))
joiner.apply { isDaemon = true; start() }
assertTrue(attached.await(10, java.util.concurrent.TimeUnit.SECONDS))
answer.countDown()
assertTrue(joinedDone.await(10, java.util.concurrent.TimeUnit.SECONDS))
owner.join(10000)
assertEquals("Cannot download the runtime: 127.0.0.1: HTTP 403", failedInstall(context, "HTTP/1.1 403 Forbidden"))
readNow.countDown()
joiner.join(10000)
} finally {
answer.countDown()
readNow.countDown()
server.close()
stall.join(1000)
}
assertFalse(joinedResult)
assertEquals("Cannot download the runtime: 127.0.0.1: HTTP 404", joinedReason)
}
@Test fun anInterruptedJoinKeepsItsInterruptedReasonWhenTheOwnerFinishesFirst() {
val context = packedContext()
val server = ServerSocket(0)
val accepted = java.util.concurrent.CountDownLatch(1)
val answer = java.util.concurrent.CountDownLatch(1)
val stall = Thread {
runCatching {
server.accept().use { socket ->
accepted.countDown()
answer.await()
socket.getOutputStream().write("HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".toByteArray())
}
}
}.apply { isDaemon = true; start() }
val release = LinuxRuntimeInstaller.Release("synthetic", "http://127.0.0.1:${server.localPort}/runtime.tar.zst", "a".repeat(64), 1L)
val ownerReason = arrayOfNulls<String>(1)
val owner = Thread {
LinuxRuntimeInstaller.install(context, release, null)
ownerReason[0] = LinuxRuntimeInstaller.installError()
}.apply { isDaemon = true; start() }
var joinedResult = true
var joinedReason: String? = null
val ownerFinished = AtomicBoolean()
lateinit var joiner: Thread
joiner = Thread {
joinedResult = LinuxRuntimeInstaller.install(context, release) { _, _ ->
if (Thread.currentThread() === joiner && ownerFinished.compareAndSet(false, true)) {
answer.countDown()
owner.join(10000)
Thread.currentThread().interrupt()
}
}
joinedReason = LinuxRuntimeInstaller.installError()
Thread.interrupted()
}
try {
assertTrue(accepted.await(10, java.util.concurrent.TimeUnit.SECONDS))
joiner.apply { isDaemon = true; start() }
joiner.join(10000)
} finally {
answer.countDown()
owner.join(10000)
server.close()
stall.join(1000)
}
assertTrue(ownerFinished.get())
assertFalse(owner.isAlive)
assertEquals("Cannot download the runtime: 127.0.0.1: HTTP 404", ownerReason[0])
assertFalse(joinedResult)
assertEquals("Runtime install interrupted while waiting for the running install", joinedReason)
}
@Test fun aFinishedInstallKeepsNoReferenceToItsListener() {
val context = packedContext()
val server = ServerSocket(0)
val thread = serveOnce(server, "HTTP/1.1 403 Forbidden")
try {
val release = LinuxRuntimeInstaller.Release("synthetic", "http://127.0.0.1:${server.localPort}/runtime.tar.zst", "a".repeat(64), 1L)
assertFalse(LinuxRuntimeInstaller.install(context, release) { _, _ -> })
} finally {
server.close()
thread.join(1000)
}
val job = (LinuxRuntimeInstaller::class.java.getDeclaredField("LAST_INSTALL").apply { isAccessible = true }.get(null) as ThreadLocal<*>).get()!!
val listeners = job.javaClass.getDeclaredField("listeners").apply { isAccessible = true }.get(job) as Collection<*>
assertTrue(listeners.isEmpty())
}
@Test fun aFailedSwapNamesTheStep() {
assertFalse(attempt(failRollback = false).result)
assertTrue(LinuxRuntimeInstaller.installError().orEmpty().startsWith("Cannot carry user data into the new runtime"))
}
}