A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.
fix(engine): per-launch boot detector and bounded storage/stdout
ExTvd2dfb558c339
diff --git a/app/src/main/java/com/excp/podroid/engine/BootStageDetector.kt b/app/src/main/java/com/excp/podroid/engine/BootStageDetector.ktindex cfa1dd2..e6eb2f3 100644--- a/app/src/main/java/com/excp/podroid/engine/BootStageDetector.kt+++ b/app/src/main/java/com/excp/podroid/engine/BootStageDetector.kt@@ -35,18 +35,6 @@ class BootStageDetector(private var scannedLen = 0private var ready = false- /**- * Reset the one-shot guard + buffer. Called by engines at the start of- * each VM run so a Stop → Start cycle's second boot can re-fire onReady.- * Without this, the detector silently ignores the new boot stream because- * `ready=true` from the previous run, and state stays Starting forever.- */- fun reset() {- ready = false- buf.clear()- scannedLen = 0- }-fun feed(bytes: ByteArray, len: Int) {if (ready) return// Latin-1 decode is byte-safe (1 byte → 1 char) and the ASCII subsetdiff --git a/app/src/main/java/com/excp/podroid/engine/QemuEngine.kt b/app/src/main/java/com/excp/podroid/engine/QemuEngine.ktindex 9db2423..93ca7d2 100644--- a/app/src/main/java/com/excp/podroid/engine/QemuEngine.kt+++ b/app/src/main/java/com/excp/podroid/engine/QemuEngine.kt@@ -120,7 +120,14 @@ class QemuEngine @Inject constructor(/** Per-run serial monitor; created in start(), released in cleanup(). */private var bootMonitor: QemuBootMonitor? = null- private val bootStageDetector = BootStageDetector(_bootStage, _state) {+ /**+ * Fresh detector per run (see start()) rather than one reused instance.+ * The previous run's boot monitor may still be draining when start() runs+ * (cleanup cancels but does not join it); a shared instance let that stale+ * feed race the new run's scan offsets / one-shot guard. A new instance per+ * run isolates each boot. Mirrors the AVF backend, which already does this.+ */+ private fun newBootStageDetector() = BootStageDetector(_bootStage, _state) {// BootStageDetector has already flipped _state to Running by the time// this onReady fires; stamp the uptime origin to match._runningSinceMs = System.currentTimeMillis()@@ -273,14 +280,22 @@ class QemuEngine @Inject constructor(return}- ensureStorageImage(config.storageSizeGb)+ try {+ ensureStorageImage(config.storageSizeGb)+ } catch (e: java.io.IOException) {+ // Restore the "cleanedUp=false ⟺ VM lifetime in progress" invariant+ // (same as the qemuExecutable() path); no process/scope exists yet.+ cleanedUp.set(true)+ bootStartTime = 0L+ _state.value = VmState.Error(e.message ?: "Could not prepare the VM disk image.")+ return+ }_consoleText.value = ""_bootStage.value = "Starting QEMU..."- // Re-arm the one-shot detector so a Stop → Start cycle's second boot- // can fire onReady again. Without this the new boot's "Ready!" marker- // is silently ignored and state stays Starting forever.- bootStageDetector.reset()+ // Fresh per-run detector so a previous run's still-draining monitor can't+ // feed (or race the one-shot guard / scan offsets of) this run's detector.+ val detector = newBootStageDetector()// Clean up stale sockets from a previous run. qmp.sock must be// included — a leftover file from a crashed QEMU prevents the new@@ -298,6 +313,12 @@ class QemuEngine @Inject constructor(val nativeDir = context.applicationInfo.nativeLibraryDirval pb = ProcessBuilder(cmd).directory(context.filesDir)pb.environment()["LD_LIBRARY_PATH"] = "$nativeDir:${context.filesDir.absolutePath}"+ // Discard QEMU's stdout. Nothing routes there today (serial/QMP use+ // sockets), but user extra args like `-monitor stdio` could, and an+ // unread OS pipe would fill its buffer and deadlock the VM. Redirect+ // to /dev/null rather than merging into stderr, which feeds error+ // formatting (recordStderr below). Redirect.DISCARD would need API 33.+ pb.redirectOutput(File("/dev/null"))// Fork QEMU on a private, long-lived thread (see qemuDispatcher).// PR_SET_PDEATHSIG (set by libpodroid-launcher) is thread-scoped, so@@ -335,7 +356,7 @@ class QemuEngine @Inject constructor(// streams the guest console into console.log + the boot-stage detector.val monitor = QemuBootMonitor(serialSockPath, File(context.filesDir, "console.log"),- bootStageDetector, _consoleText, SOCKET_READY_TIMEOUT_MS,+ detector, _consoleText, SOCKET_READY_TIMEOUT_MS,)bootMonitor = monitormonitor.connectAndRun(scope) { proc.isAlive }@@ -679,7 +700,12 @@ class QemuEngine @Inject constructor(java.io.RandomAccessFile(storageFile, "rw").use { it.setLength(desiredBytes) }Log.d(TAG, "Created storage.img (${storageSizeGb}GB)")} catch (e: Exception) {+ // Don't swallow: a 0-byte / missing storage.img would otherwise boot+ // into an opaque early-boot stop. Surface it so start() can report a+ // clear storage error (mirrors the AVF disk-create path).Log.e(TAG, "Failed to create storage.img", e)+ runCatching { storageFile.delete() }+ throw java.io.IOException("Couldn't create the ${storageSizeGb} GB VM disk image: ${e.message}", e)}}diff --git a/app/src/main/java/com/excp/podroid/engine/avf/AvfEngine.kt b/app/src/main/java/com/excp/podroid/engine/avf/AvfEngine.ktindex 0362817..26ecc47 100644--- a/app/src/main/java/com/excp/podroid/engine/avf/AvfEngine.kt+++ b/app/src/main/java/com/excp/podroid/engine/avf/AvfEngine.kt@@ -848,7 +848,11 @@ class AvfEngine @Inject constructor(java.io.RandomAccessFile(storageFile, "rw").use { it.setLength(desiredBytes) }Log.d(TAG, "Created storage.img (${storageSizeGb}GB)")} catch (e: Exception) {+ // Don't swallow + boot a 0-byte image into an opaque early-boot stop.+ // Rethrow so start()'s catch surfaces a clear storage error.Log.e(TAG, "Failed to create storage.img", e)+ runCatching { storageFile.delete() }+ throw java.io.IOException("Couldn't create the ${storageSizeGb} GB VM disk image: ${e.message}", e)}return storageFile}diff --git a/app/src/test/java/com/excp/podroid/engine/BootStageDetectorTest.kt b/app/src/test/java/com/excp/podroid/engine/BootStageDetectorTest.ktindex 60c8d15..c7ed9a5 100644--- a/app/src/test/java/com/excp/podroid/engine/BootStageDetectorTest.kt+++ b/app/src/test/java/com/excp/podroid/engine/BootStageDetectorTest.kt@@ -84,17 +84,17 @@ class BootStageDetectorTest {}@Test- fun `reset re-arms the one-shot so a second boot fires Running again`() {- val (detector, stage, state) = newDetector()- detector.feed("Ready!\n")- assertTrue(state.value is VmState.Running)-- detector.reset()- state.value = VmState.Starting- // A fresh boot stream after reset must be detected again.- detector.feed("Booting kernel...\n")- assertEquals("Booting kernel...", stage.value)- detector.feed("Ready!\n")- assertTrue(state.value is VmState.Running)+ fun `a fresh detector instance re-detects a new boot (per-run allocation)`() {+ // QEMU and AVF both allocate a new detector per run rather than resetting+ // one in place; a brand-new instance must detect a boot stream from clean.+ val (first, _, firstState) = newDetector()+ first.feed("Ready!\n")+ assertTrue(firstState.value is VmState.Running)++ val (second, secondStage, secondState) = newDetector()+ second.feed("Booting kernel...\n")+ assertEquals("Booting kernel...", secondStage.value)+ second.feed("Ready!\n")+ assertTrue(secondState.value is VmState.Running)}}