A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.
fix(service): off-main asset extraction and start de-duplication
ExTv7189db7e4c2b
diff --git a/app/src/main/java/com/excp/podroid/PodroidApplication.kt b/app/src/main/java/com/excp/podroid/PodroidApplication.ktindex a24722d..c285f41 100644--- a/app/src/main/java/com/excp/podroid/PodroidApplication.kt+++ b/app/src/main/java/com/excp/podroid/PodroidApplication.kt@@ -11,6 +11,11 @@ import android.app.Applicationimport android.os.Buildimport android.util.Logimport dagger.hilt.android.HiltAndroidApp+import kotlinx.coroutines.CompletableDeferred+import kotlinx.coroutines.CoroutineScope+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.SupervisorJob+import kotlinx.coroutines.launchimport org.lsposed.hiddenapibypass.HiddenApiBypassimport java.io.Fileimport java.util.concurrent.Executors@@ -19,12 +24,31 @@ import java.util.concurrent.TimeUnit@HiltAndroidAppclass PodroidApplication : Application() {+ private val appScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)++ // Completion signal for asset extraction. The VM launch path+ // (PodroidService.launchPodroid) reads the extracted files synchronously,+ // so it MUST await this before starting the engine — see awaitAssetsReady.+ // Completed (never failed) in extractAssets' finally so a waiter can never+ // hang even if extraction throws; intactness is enforced by the size-check+ // in QemuEngine/AvfEngine's own asset reads, not by this signal.+ private val assetsReady = CompletableDeferred<Unit>()+override fun onCreate() {super.onCreate()exemptHiddenApi()- extractAssets()+ // Extract off the main thread: the squashfs alone is ~225 MB and+ // blocking onCreate on first install/upgrade would ANR the cold start.+ appScope.launch { extractAssets() }}+ /**+ * Suspends until the bundled assets (qemu/, kernel, initrd, squashfs) have+ * finished extracting to [filesDir]. The foreground service awaits this+ * before launching the VM so QEMU/AVF never read a partial or missing file.+ */+ suspend fun awaitAssetsReady() = assetsReady.await()+// Android 14+ hides @SystemApi reflection lookups (returning NoSuchMethod// even via getDeclared*). Two prefixes need exempting:// - Landroid/system/virtualmachine/ — AVF framework (AvfDiagnostics + AvfEngine)@@ -44,60 +68,82 @@ class PodroidApplication : Application() {}private fun extractAssets() {- // Asset extraction has a self-healing version stamp: on every install- // or upgrade `packageInfo.lastUpdateTime` changes, so we record it in- // `.assets_stamp` and force a re-copy on mismatch. Pure size checks- // are deceiving because `mksquashfs -all-root -noappend` is- // deterministic — changing service scripts inside the rootfs can- // produce a byte-identical-size file with different content, which- // older extraction logic silently kept stale.- val stampFile = File(filesDir, ".assets_stamp")- val currentStamp = runCatching {- packageManager.getPackageInfo(packageName, 0).lastUpdateTime- }.getOrDefault(0L).toString()- val previousStamp = runCatching { stampFile.readText() }.getOrDefault("")- val forceCopy = previousStamp != currentStamp- if (forceCopy) {- Log.i(TAG, "asset stamp drift ($previousStamp → $currentStamp) — forcing re-extract")- }-- // Fan out the four top-level extractions across a small thread pool.- // Disk-write throughput is the bottleneck for the squashfs (~225 MB),- // but decompression, asset-FD lookup, and skip-when-size-matches all- // overlap usefully across threads. Must complete before onCreate- // returns — the QEMU launch path reads these files synchronously.- val tasks: List<() -> Unit> = listOf(- { copyAssetDir("qemu", filesDir, forceCopy) },- { copyAssetIfNeeded("vmlinuz-virt", filesDir, forceCopy) },- { copyAssetIfNeeded("initrd.img", filesDir, forceCopy) },- { copyAssetIfNeeded("alpine-rootfs.squashfs", filesDir, forceCopy) },- )- val pool = Executors.newFixedThreadPool(tasks.size.coerceAtMost(4))try {- // invokeAll blocks until every Callable finishes (or times out).- // Each Callable wraps the task so a thrown exception is captured- // in the returned Future rather than killing the worker silently.- val futures = pool.invokeAll(tasks.map { task ->- java.util.concurrent.Callable<Unit> { task() }- })- for (f in futures) {- try { f.get() } catch (e: Exception) {- // copyAssetIfNeeded / copyAssetFileIfNeeded already log- // their own failures; this catches anything that escaped.- Log.w(TAG, "Asset extraction task failed", e)+ // Asset extraction has a self-healing version stamp: on every install+ // or upgrade `packageInfo.lastUpdateTime` changes, so we record it in+ // `.assets_stamp` and force a re-copy on mismatch. Pure size checks+ // are deceiving because `mksquashfs -all-root -noappend` is+ // deterministic — changing service scripts inside the rootfs can+ // produce a byte-identical-size file with different content, which+ // older extraction logic silently kept stale.+ val stampFile = File(filesDir, ".assets_stamp")+ val currentStamp = runCatching {+ packageManager.getPackageInfo(packageName, 0).lastUpdateTime+ }.getOrDefault(0L).toString()+ val previousStamp = runCatching { stampFile.readText() }.getOrDefault("")+ val forceCopy = previousStamp != currentStamp+ if (forceCopy) {+ Log.i(TAG, "asset stamp drift ($previousStamp → $currentStamp) — forcing re-extract")+ }++ // Drop any .tmp files left by a process killed mid-copy so they+ // can't accumulate or shadow a fresh atomic write.+ deleteStaleTmpFiles(filesDir)++ // Fan out the four top-level extractions across a small thread pool.+ // Disk-write throughput is the bottleneck for the squashfs (~225 MB),+ // but decompression, asset-FD lookup, and skip-when-size-matches all+ // overlap usefully across threads. Runs on a background coroutine+ // (not the main thread); the VM launch path awaits awaitAssetsReady.+ val tasks: List<() -> Unit> = listOf(+ { copyAssetDir("qemu", filesDir, forceCopy) },+ { copyAssetIfNeeded("vmlinuz-virt", filesDir, forceCopy) },+ { copyAssetIfNeeded("initrd.img", filesDir, forceCopy) },+ { copyAssetIfNeeded("alpine-rootfs.squashfs", filesDir, forceCopy) },+ )+ val pool = Executors.newFixedThreadPool(tasks.size.coerceAtMost(4))+ try {+ // invokeAll blocks until every Callable finishes (or times out).+ // Each Callable wraps the task so a thrown exception is captured+ // in the returned Future rather than killing the worker silently.+ val futures = pool.invokeAll(tasks.map { task ->+ java.util.concurrent.Callable<Unit> { task() }+ })+ for (f in futures) {+ try { f.get() } catch (e: Exception) {+ // copyAssetIfNeeded / copyAssetFileIfNeeded already log+ // their own failures; this catches anything that escaped.+ Log.w(TAG, "Asset extraction task failed", e)+ }+ }+ } finally {+ pool.shutdown()+ if (!pool.awaitTermination(30, TimeUnit.SECONDS)) {+ pool.shutdownNow()}}++ // Commit the new stamp only after extraction so a half-finished copy+ // (process killed mid-extract) doesn't leave us thinking we're done.+ runCatching { stampFile.writeText(currentStamp) }+ .onFailure { Log.w(TAG, "Failed to write assets stamp", it) }} finally {- pool.shutdown()- if (!pool.awaitTermination(5, TimeUnit.SECONDS)) {- pool.shutdownNow()- }+ // Always release waiters — a failed/partial extract is detected by+ // the per-file size-check on the next read, not by hanging here.+ assetsReady.complete(Unit)}+ }- // Commit the new stamp only after extraction so a half-finished copy- // (process killed mid-extract) doesn't leave us thinking we're done.- runCatching { stampFile.writeText(currentStamp) }- .onFailure { Log.w(TAG, "Failed to write assets stamp", it) }+ /** Recursively removes leftover `<name>.tmp` files under [dir]. */+ private fun deleteStaleTmpFiles(dir: File) {+ val children = dir.listFiles() ?: return+ for (child in children) {+ if (child.isDirectory) {+ deleteStaleTmpFiles(child)+ } else if (child.name.endsWith(TMP_SUFFIX)) {+ runCatching { child.delete() }+ }+ }}/**@@ -113,10 +159,8 @@ class PodroidApplication : Application() {val assetSize = try { assets.openFd(assetName).use { it.length } } catch (_: Exception) { -1L }if (!forceCopy && assetSize >= 0 && destFile.exists() && destFile.length() == assetSize) return- assets.open(assetName).use { input ->- destFile.parentFile?.mkdirs()- destFile.outputStream().use { output -> input.copyTo(output) }- }+ destFile.parentFile?.mkdirs()+ copyAssetAtomically(assetName, destFile)} catch (e: Exception) {Log.w(TAG, "Failed to extract $assetName", e)}@@ -147,15 +191,40 @@ class PodroidApplication : Application() {val assetSize = try { assets.openFd(assetPath).use { it.length } } catch (_: Exception) { -1L }if (!forceCopy && assetSize >= 0 && destFile.exists() && destFile.length() == assetSize) return+ copyAssetAtomically(assetPath, destFile)+ } catch (e: Exception) {+ Log.w(TAG, "Failed to extract $assetPath", e)+ }+ }++ /**+ * Streams [assetPath] to `<destFile>.tmp`, fsyncs the data to disk, then+ * atomically renames it onto [destFile]. The final canonical path therefore+ * only ever holds a fully-written file — an async reader (the VM launch)+ * never sees a half-written squashfs/kernel. Throws on any failure so the+ * caller logs it and the stale/missing file is caught by the next size-check.+ */+ private fun copyAssetAtomically(assetPath: String, destFile: File) {+ val tmpFile = File(destFile.parentFile, destFile.name + TMP_SUFFIX)+ try {assets.open(assetPath).use { input ->- destFile.outputStream().use { output -> input.copyTo(output) }+ java.io.FileOutputStream(tmpFile).use { output ->+ input.copyTo(output)+ output.flush()+ output.fd.sync()+ }+ }+ if (!tmpFile.renameTo(destFile)) {+ throw java.io.IOException("atomic rename ${tmpFile.name} -> ${destFile.name} failed")}} catch (e: Exception) {- Log.w(TAG, "Failed to extract $assetPath", e)+ runCatching { tmpFile.delete() }+ throw e}}companion object {private const val TAG = "PodroidApp"+ private const val TMP_SUFFIX = ".tmp"}}diff --git a/app/src/main/java/com/excp/podroid/service/PodroidService.kt b/app/src/main/java/com/excp/podroid/service/PodroidService.ktindex 930dcbd..94f779d 100644--- a/app/src/main/java/com/excp/podroid/service/PodroidService.kt+++ b/app/src/main/java/com/excp/podroid/service/PodroidService.kt@@ -23,6 +23,7 @@ import android.util.Logimport androidx.core.app.NotificationCompatimport androidx.core.app.ServiceCompatimport com.excp.podroid.MainActivity+import com.excp.podroid.PodroidApplicationimport com.excp.podroid.Rimport com.excp.podroid.data.repository.PortForwardRepositoryimport com.excp.podroid.data.repository.SettingsRepository@@ -69,17 +70,42 @@ class PodroidService : Service() {} else {0}+ // Always (re-)assert foreground within the start window — required+ // even on a redundant ACTION_START so the system doesn't fault us+ // for not calling startForeground.ServiceCompat.startForeground(this,NOTIFICATION_ID,buildNotification("Starting VM..."),fgType,)- acquireWakeLock()- launchPodroid()+ // De-dup: if the VM is already active, do NOT re-acquire/relaunch.+ // launchPodroid relaunches the observers (resetting their+ // seenActive latch); doing that on a double-tap could strand the+ // teardown path. The engine's own start() is also a no-op here.+ val alreadyActive = engine.state.value is VmState.Starting ||+ engine.state.value is VmState.Running+ if (!alreadyActive) {+ acquireWakeLock()+ launchPodroid()+ }}ACTION_STOP -> {+ // engine.stop() is a no-op if nothing is running. Either way the+ // shutdown observer only fires after an active state, so on the+ // "stop while idle" path (e.g. PodroidService.stop() spinning up a+ // fresh service) we must release + stop here so this doesn't+ // linger as a started-but-never-foregrounded orphan.engine.stop()+ releaseWakeLock()+ stopForegroundCompat()+ stopSelf()+ }+ else -> {+ // Null/unrecognized action (e.g. a system redelivery): we never+ // called startForeground for this start, so just stop to avoid a+ // started-but-not-foregrounded service.+ stopSelf()}}return START_NOT_STICKY@@ -94,11 +120,14 @@ class PodroidService : Service() {override fun onTaskRemoved(rootIntent: Intent?) {super.onTaskRemoved(rootIntent)- // App swiped from recents — stop the VM gracefully- if (engine.state.value is VmState.Running ||- engine.state.value is VmState.Starting) {- engine.stop()- }+ // App swiped from recents — stop the VM gracefully and tear the service+ // down unconditionally. engine.stop() is a no-op if idle; doing this even+ // in the Idle-before-Starting window prevents a swipe during early start+ // from leaving a VM that starts with no UI, holding the WakeLock.+ engine.stop()+ releaseWakeLock()+ stopForegroundCompat()+ stopSelf()}private fun acquireWakeLock() {@@ -147,33 +176,56 @@ class PodroidService : Service() {/*** Releases the wakelock and stops the service when the VM reaches a- * terminal state. The seenActive guard avoids tearing down on the initial- * Idle replay before QEMU has even been launched.+ * terminal state.+ *+ * `Error`/`Stopped` are treated as always-actionable: an engine that goes+ * straight to `Error` without ever emitting `Starting` (e.g. a missing QEMU+ * binary), or a conflated `Idle→Starting→Error` that the Main collector only+ * observes as `Error`, must still release the no-timeout WakeLock and stop+ * the foreground service. The `seenActive` latch exists only to skip the+ * initial `Idle` replay before the VM has been launched, so the guard now+ * gates `Idle` alone — terminal failures are never silently swallowed.*/private suspend fun observeStateForShutdown() {var seenActive = falseengine.state.collect { state ->when (state) {is VmState.Starting, is VmState.Running -> seenActive = true- is VmState.Stopped, is VmState.Idle, is VmState.Error -> {+ is VmState.Idle -> {if (!seenActive) return@collect- releaseWakeLock()- if (Build.VERSION.SDK_INT >= 33) {- stopForeground(STOP_FOREGROUND_REMOVE)- } else {- @Suppress("DEPRECATION") stopForeground(true)- }- stopSelf()+ teardown()}+ is VmState.Stopped, is VmState.Error -> teardown()}}}+ /** Release the WakeLock, drop the foreground notification, and stop. */+ private fun teardown() {+ releaseWakeLock()+ stopForegroundCompat()+ stopSelf()+ }++ private fun stopForegroundCompat() {+ if (Build.VERSION.SDK_INT >= 33) {+ stopForeground(STOP_FOREGROUND_REMOVE)+ } else {+ @Suppress("DEPRECATION") stopForeground(true)+ }+ }+private fun launchPodroid() {serviceScope.launch {startNotificationUpdates()withContext(Dispatchers.IO) {try {+ // Asset extraction now runs off the main thread; the engine+ // reads vmlinuz/initrd/squashfs synchronously in start(), so+ // we MUST block here until extraction has fully completed or+ // the VM could launch against a partial/missing file.+ (application as? PodroidApplication)?.awaitAssetsReady()+val rules = portForwardRepository.getRulesSnapshot().toMutableList()val sshEnabled = settingsRepository.getSshEnabledSnapshot()