A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.
fix(avf): flush guest filesystems before stopping the VM
ExTv2c7d4c9d6795
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 bd15397..6cae1ba 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@@ -58,6 +58,10 @@ class AvfEngine @Inject constructor(// doesn't strand the engine in Starting forever — EngineHolder.trySwap// waits on a terminal state, so a stuck Starting blocks backend switch.private const val BOOT_TIMEOUT_MS = 60_000L+ // Upper bound on the best-effort guest flush before stop. Bounded so a+ // hung guest can never block the stop path; the caller proceeds to kill+ // the VM if no SYNCED ack arrives.+ private const val SYNC_TIMEOUT_MS = 8_000L}private val _state = MutableStateFlow<VmState>(VmState.Idle)@@ -432,18 +436,19 @@ class AvfEngine @Inject constructor(// is mapped to Stopped (never downgraded to Error) by the funnel.stopRequested = trueval generation = vmGeneration- // Surface (don't swallow) a framework stop() failure: if the request was- // rejected the VM is still alive, and we must NOT null the handle (cleanup- // nulls it) or we'd leak an unkillable VM. Retain the handle so the- // watchdog below can retry / so a later stop() can act on it.- val requested = runCatching { AvfReflect.stop(vm) }- .onFailure { Log.w(TAG, "AVF stop() request failed; VM may still be running", it) }- .isSuccess- // Drive the final Stopped + cleanup from the onStopped/onDied callback so- // we don't tear down a still-live VM. If the framework never delivers a- // terminal callback (or stop() was rejected), a bounded watchdog forces- // the transition so the engine never strands in Running/Starting.scope.launch {+ // Best-effort guest flush so unwritten ext4 data hits storage before+ // we kill the VM. Bounded so a hung guest can't block the stop.+ runCatching { control?.syncAndWait(SYNC_TIMEOUT_MS) }+ if (generation != vmGeneration || cleanedUp.get()) return@launch+ // Surface (don't swallow) a framework stop() failure: if rejected the+ // VM is still alive, and we must NOT null the handle.+ val requested = runCatching { AvfReflect.stop(vm) }+ .onFailure { Log.w(TAG, "AVF stop() request failed; VM may still be running", it) }+ .isSuccess+ // Drive the final Stopped + cleanup from onStopped/onDied. If no+ // terminal callback arrives (or stop() was rejected), a bounded+ // watchdog forces the transition so we never strand in Running.kotlinx.coroutines.delay(if (requested) 5_000L else 1_000L)if (generation == vmGeneration && !cleanedUp.get()) {Log.w(TAG, "AVF stop: no terminal callback within timeout — forcing Stopped")diff --git a/app/src/main/java/com/excp/podroid/engine/avf/VsockControlChannel.kt b/app/src/main/java/com/excp/podroid/engine/avf/VsockControlChannel.ktindex db090d0..147af53 100644--- a/app/src/main/java/com/excp/podroid/engine/avf/VsockControlChannel.kt+++ b/app/src/main/java/com/excp/podroid/engine/avf/VsockControlChannel.kt@@ -12,6 +12,7 @@* ADD <vport> tcp <host> <gport>* REMOVE <vport>* PING → agent replies "PONG\n" (we ignore the reply)+ * SYNC → agent runs sync(2), replies "SYNCED\n"*/package com.excp.podroid.engine.avf@@ -24,6 +25,7 @@ import kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.Jobimport kotlinx.coroutines.SupervisorJobimport kotlinx.coroutines.launch+import kotlinx.coroutines.withContextimport java.io.PrintWriter@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)@@ -171,6 +173,37 @@ class VsockControlChannel(fun addForward(vport: Int, host: String, gport: Int) = sendOrQueue("ADD $vport tcp $host $gport")fun removeForward(vport: Int) = sendOrQueue("REMOVE $vport")+ /**+ * Deterministic guest flush before stop: tells the agent to sync(2) and waits+ * up to timeoutMs for the "SYNCED" ack. Best-effort - returns true only on a+ * SYNCED ack within the timeout; false if the channel is down, the write+ * fails, or no ack arrives in time. The caller then just proceeds to stop.+ */+ suspend fun syncAndWait(timeoutMs: Long): Boolean {+ val pfdLocal: ParcelFileDescriptor+ synchronized(this) {+ val w = writer+ val p = pfd+ if (closed || w == null || p == null) return false+ if (!writeLine(w, "SYNC")) return false+ pfdLocal = p+ }+ // Read the ack OUTSIDE the monitor so a slow guest can't hold the lock.+ return withContext(Dispatchers.IO) {+ kotlinx.coroutines.withTimeoutOrNull(timeoutMs) {+ val reader = java.io.BufferedReader(java.io.InputStreamReader(+ java.io.FileInputStream(pfdLocal.fileDescriptor)))+ // Do NOT close `reader`: it wraps the same fd the writer/pfd own;+ // close() owns the pfd. A read still blocked at timeout unblocks+ // when close()/cleanup shuts the fd moments later, which is fine on+ // the stop path.+ var line = reader.readLine()+ while (line != null && line != "SYNCED") line = reader.readLine()+ line == "SYNCED"+ } ?: false+ }+ }+@Synchronized fun close() {if (closed) returnclosed = truediff --git a/build-rootfs/vsock-agent/podroid-vsock-agent.c b/build-rootfs/vsock-agent/podroid-vsock-agent.cindex 31e58ec..ef1034d 100644--- a/build-rootfs/vsock-agent/podroid-vsock-agent.c+++ b/build-rootfs/vsock-agent/podroid-vsock-agent.c@@ -513,6 +513,10 @@ static void ctl_loop(int fd) {} else if (strcmp(line, "PING") == 0) {fprintf(out, "PONG\n");fflush(out);+ } else if (strcmp(line, "SYNC") == 0) {+ sync(); /* flush all guest filesystem buffers to virtio-blk */+ fprintf(out, "SYNCED\n");+ fflush(out);} else if (line[0] != '\0') {LOG_W("unknown command: '%s'", line);}