Skip to content

A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.

fix(engine): backend selection, async init, and live rule diffing

ExTv748737714c2f
diff --git a/app/src/main/java/com/excp/podroid/engine/EngineHolder.kt b/app/src/main/java/com/excp/podroid/engine/EngineHolder.kt
index 2a5e6e0..72bfbea 100644
--- a/app/src/main/java/com/excp/podroid/engine/EngineHolder.kt
+++ b/app/src/main/java/com/excp/podroid/engine/EngineHolder.kt
@@ -25,9 +25,11 @@ import com.termux.terminal.TerminalSession
import com.termux.terminal.TerminalSessionClient
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.SupervisorJob
+import kotlinx.coroutines.async
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
@@ -39,7 +41,6 @@ import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
-import kotlinx.coroutines.runBlocking
import javax.inject.Inject
import javax.inject.Provider
import javax.inject.Singleton
@@ -54,11 +55,30 @@ class EngineHolder @Inject constructor(
private val avfProvider: Provider<AvfEngine>,
) : VmEngine {
- private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
-
- private val _currentFlow: MutableStateFlow<VmEngine> = MutableStateFlow(
- pick(runBlocking { settings.getEngineSelectionSnapshot() })
+ // Single-threaded dispatcher: confines every appliedRules read-modify-write
+ // (swap-reset + diff loop) to one thread so the @Volatile field can't be
+ // torn by a swap landing between the diff loop's read and write.
+ private val scope = CoroutineScope(
+ SupervisorJob() + Dispatchers.IO.limitedParallelism(1)
)
+
+ // First pick is resolved OFF the main thread: getEngineSelectionSnapshot()
+ // is a DataStore disk read and pick() does a binder-IPC AVF probe — running
+ // either in the field initializer (on whatever thread first injects this
+ // @Singleton, i.e. the main thread) is an ANR risk. We seed _currentFlow
+ // with a side-effect-free QEMU singleton (its ctor starts no VM) and replace
+ // it with the real pick once firstPick resolves. start() awaits firstPick
+ // before delegating, so the first Start can NEVER run the seed by mistake.
+ private val firstPick: Deferred<VmEngine> =
+ scope.async { pick(settings.getEngineSelectionSnapshot()) }
+
+ // start() runs on the Service's IO thread, not the holder scope, so the
+ // first-pick publish can be raced by the init coroutine. CAS makes it land
+ // exactly once; both callers pass the same resolved firstPick value anyway.
+ private val firstPickPublished = java.util.concurrent.atomic.AtomicBoolean(false)
+
+ private val _currentFlow: MutableStateFlow<VmEngine> =
+ MutableStateFlow(qemuProvider.get())
val currentFlow: StateFlow<VmEngine> = _currentFlow.asStateFlow()
private val current: VmEngine get() = _currentFlow.value
@@ -66,6 +86,15 @@ class EngineHolder @Inject constructor(
@Volatile private var appliedRules: Set<PortForwardRule> = emptySet()
init {
+ // 0. Publish the real first pick as soon as it resolves off-main, so the
+ // delegate flows (state/bootStage/consoleText via flatMapLatest) and
+ // the cosmetic backendId reads converge onto the correct engine on
+ // cold start without anyone blocking the main thread. start() also
+ // awaits firstPick directly, so an early Start beats no race here.
+ // runCatching: start() consumes firstPick's result, so an await() throw here
+ // would otherwise be an uncaught exception on the scope thread.
+ scope.launch { runCatching { publishFirstPick(firstPick.await()) } }
+
// 1. Backend swap observer — drops the first emit so we don't re-pick
// on cold start. Waits for Stopped/Idle/Error so we never kill a
// running VM (the Settings UI also disables the chips, but defend
@@ -92,29 +121,63 @@ class EngineHolder @Inject constructor(
}
val added = rules - appliedRules
val removed = appliedRules - rules
- for (r in added) eng.addPortForward(r)
- for (r in removed) eng.removePortForward(r)
- appliedRules = rules
+ // Track what is actually live so a transient add/remove failure
+ // doesn't permanently desync appliedRules from the engine: a
+ // failed add isn't recorded as applied (retried next diff), a
+ // failed remove stays recorded (retried next diff). Removes go
+ // first so a same-port churn frees the host port before re-add.
+ val live = appliedRules.toMutableSet()
+ for (r in removed) {
+ runCatching { eng.removePortForward(r) }
+ .onSuccess { live.remove(r) }
+ .onFailure { android.util.Log.w(TAG, "removePortForward failed for $r", it) }
+ }
+ for (r in added) {
+ runCatching { eng.addPortForward(r) }
+ .onSuccess { live.add(r) }
+ .onFailure { android.util.Log.w(TAG, "addPortForward failed for $r", it) }
+ }
+ appliedRules = live
}
}
}
private fun pick(sel: EngineSelection): VmEngine {
val probe = AvfDiagnostics.probe(context)
+ val capsChoice = AvfCapabilities.choose(probe.capabilitiesRaw)
+ // avfUsable = AVF can actually start here. serviceReachable is the new
+ // gate: capabilitiesRaw is 0 (→ Unknown, NOT Unsupported) whenever the
+ // system service is unreachable, so without this an unreachable AVF
+ // passed the "!is Unsupported" check and AvfEngine.start() errored out
+ // instead of falling back to QEMU. On a working AVF device all four
+ // conjuncts are true (feature+perms+reachable, caps=NonProtected), so
+ // this is unchanged for the happy path.
val avfUsable = probe.featureSupported &&
probe.managePermissionGranted &&
probe.customPermissionGranted &&
- AvfCapabilities.choose(probe.capabilitiesRaw) !is
- AvfCapabilities.ProtectedVmChoice.Unsupported
+ probe.serviceReachable &&
+ capsChoice !is AvfCapabilities.ProtectedVmChoice.Unsupported
return when {
sel == EngineSelection.QEMU -> qemuProvider.get()
- sel == EngineSelection.AVF && AvfCapabilities.choose(probe.capabilitiesRaw) is
- AvfCapabilities.ProtectedVmChoice.Unsupported -> {
- android.util.Log.w(
- TAG,
- "AVF forced but device is protected-only; falling back to QEMU. " +
- "caps=${probe.capabilitiesRaw}(${probe.capabilitiesDecoded})"
- )
+ // Forced AVF, but AVF can't run here → transparent QEMU fallback
+ // instead of an Error state. Protected-only keeps its dedicated log.
+ sel == EngineSelection.AVF && !avfUsable -> {
+ if (capsChoice is AvfCapabilities.ProtectedVmChoice.Unsupported) {
+ android.util.Log.w(
+ TAG,
+ "AVF forced but device is protected-only; falling back to QEMU. " +
+ "caps=${probe.capabilitiesRaw}(${probe.capabilitiesDecoded})"
+ )
+ } else {
+ android.util.Log.w(
+ TAG,
+ "AVF forced but unavailable; falling back to QEMU. " +
+ "feature=${probe.featureSupported} " +
+ "perms=${probe.managePermissionGranted}/${probe.customPermissionGranted} " +
+ "reachable=${probe.serviceReachable} " +
+ "caps=${probe.capabilitiesRaw}(${probe.capabilitiesDecoded})"
+ )
+ }
qemuProvider.get()
}
sel == EngineSelection.AVF -> avfProvider.get()
@@ -130,13 +193,45 @@ class EngineHolder @Inject constructor(
}
}
+ /**
+ * Publish the first picked engine into _currentFlow exactly once. Idempotent
+ * and safe to call from both the init coroutine and the first start(): the
+ * loser is a no-op. We only replace the seed (never an engine a swap already
+ * installed) by gating on firstPickPublished and keeping the swap observer
+ * the sole writer thereafter — both run on the single-thread scope, so the
+ * flag check + write don't interleave.
+ */
+ private fun publishFirstPick(first: VmEngine) {
+ if (!firstPickPublished.compareAndSet(false, true)) return
+ if (first !== _currentFlow.value) {
+ android.util.Log.i(TAG, "first pick: ${_currentFlow.value.backendId} → ${first.backendId}")
+ _currentFlow.value = first
+ }
+ }
+
private suspend fun trySwap(newSel: EngineSelection) {
- // Defensive: wait for swappable state even though Settings UI gates chips.
+ // The swap observer drops emit #0, so it never fires before the first
+ // pick is published; firstPick is already resolved by the time a user
+ // changes the backend chip. Defensive: also wait for a swappable state
+ // even though Settings UI gates chips.
currentFlow.value.state.first {
it is VmState.Stopped || it is VmState.Idle || it is VmState.Error
}
+ // A swap is the authoritative selection from here on. Mark first-pick
+ // published so a late init/start publish (firstPick that hadn't resolved
+ // when this swap fired — only possible if the user changed the backend
+ // within ~ms of cold launch) can never clobber the swapped engine.
+ firstPickPublished.set(true)
val next = pick(newSel)
if (next === currentFlow.value) return
+ // NOTE: AvfEngine.stop() flips state to Stopped before cleanup() finishes
+ // (socket delete + coroutine cancel), so publishing `next` here can
+ // briefly overlap the old engine's teardown. This is the lower-risk
+ // choice: the @Singleton engines mean `next` is never a fresh instance,
+ // the UI gates the chips while Running/Starting, and no two VMs run at
+ // once (state is already terminal). Forcing stop()+cleanup ordering would
+ // require touching the engine classes (out of scope) and risks the
+ // happy-path swap. The residual window is teardown-only, not dual-run.
android.util.Log.i(TAG, "swap: ${currentFlow.value.backendId} → ${next.backendId}")
appliedRules = emptySet()
_currentFlow.value = next
@@ -163,13 +258,21 @@ class EngineHolder @Inject constructor(
get() = current.sessionClientDelegate
set(v) { current.sessionClientDelegate = v }
- override suspend fun start(portForwards: List<PortForwardRule>, config: VmConfig) =
+ override suspend fun start(portForwards: List<PortForwardRule>, config: VmConfig) {
+ // Guarantee the first Start runs on the correctly-picked engine even on a
+ // fast cold launch where Start beats the init publish coroutine. start()
+ // is always called off-main (PodroidService.launchPodroid → withContext
+ // (Dispatchers.IO)), so awaiting the off-main first pick here is safe and
+ // closes the seed→AVF race: the seed (QEMU) can never run by mistake.
+ publishFirstPick(firstPick.await())
current.start(portForwards, config)
+ }
override fun stop() = current.stop()
override fun createTerminalSession(client: TerminalSessionClient) =
current.createTerminalSession(client)
override suspend fun addPortForward(rule: PortForwardRule) = current.addPortForward(rule)
override suspend fun removePortForward(rule: PortForwardRule) = current.removePortForward(rule)
+ override fun diagnosticsReport(): String = current.diagnosticsReport()
companion object {
private const val TAG = "EngineHolder"
diff --git a/app/src/main/java/com/excp/podroid/engine/VmEngine.kt b/app/src/main/java/com/excp/podroid/engine/VmEngine.kt
index 9ffb002..74a4696 100644
--- a/app/src/main/java/com/excp/podroid/engine/VmEngine.kt
+++ b/app/src/main/java/com/excp/podroid/engine/VmEngine.kt
@@ -52,6 +52,13 @@ interface VmEngine {
*/
suspend fun addPortForward(rule: PortForwardRule)
suspend fun removePortForward(rule: PortForwardRule)
+
+ /**
+ * Backend-specific diagnostics for the export log: AVF stop/crash reason +
+ * launch config, or QEMU exit code + stderr tail. Empty when there's nothing
+ * backend-specific to add. Observational; never mutates VM state.
+ */
+ fun diagnosticsReport(): String = ""
}
/**
diff --git a/app/src/main/java/com/excp/podroid/util/NetworkUtils.kt b/app/src/main/java/com/excp/podroid/util/NetworkUtils.kt
index 8dd1881..0c100cf 100644
--- a/app/src/main/java/com/excp/podroid/util/NetworkUtils.kt
+++ b/app/src/main/java/com/excp/podroid/util/NetworkUtils.kt
@@ -36,6 +36,24 @@ object NetworkUtils {
)
private fun firstIpv4ByTransportPreference(cm: ConnectivityManager): String? {
+ // Prefer the active (default-route) network first — it's the address
+ // that traffic actually leaves through, not just any connected interface.
+ cm.activeNetwork?.let { active ->
+ val caps = cm.getNetworkCapabilities(active)
+ if (caps != null && !caps.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
+ val link = cm.getLinkProperties(active)
+ if (link != null) {
+ for (la in link.linkAddresses) {
+ val addr = la.address
+ if (addr is Inet4Address && !addr.isLoopbackAddress) {
+ return addr.hostAddress
+ }
+ }
+ }
+ }
+ }
+ // Fall back to transport-preference scan for edge cases (e.g. active network
+ // has only IPv6, but a secondary WiFi interface has an IPv4 address).
for (preferred in TRANSPORT_PREFERENCE) {
for (net in cm.allNetworks) {
val caps = cm.getNetworkCapabilities(net) ?: continue