Skip to content

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

fix(hostbridge): log a vanished guest as one line, not a stack trace

ExTva4553d2de1d5
diff --git a/app/src/main/java/com/excp/podroid/engine/hostbridge/HostRequestServer.kt b/app/src/main/java/com/excp/podroid/engine/hostbridge/HostRequestServer.kt
index 1446001..9fc42a2 100644
--- a/app/src/main/java/com/excp/podroid/engine/hostbridge/HostRequestServer.kt
+++ b/app/src/main/java/com/excp/podroid/engine/hostbridge/HostRequestServer.kt
@@ -20,6 +20,7 @@ import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
+import java.io.IOException
import java.util.concurrent.atomic.AtomicBoolean
/**
@@ -153,6 +154,15 @@ class HostRequestServer(
}
} catch (c: CancellationException) {
throw c // don't log a stop/swap's cancellation as a loop error
+ } catch (e: HostProtocolViolationException) {
+ // A malformed request line (too long, bad UTF-8): a real bug
+ // from a hostile or buggy guest, not a peer disconnect. Keep
+ // the throwable, unlike the generic IOException case below.
+ log("host bridge loop error: ${e.message}", e)
+ } catch (e: IOException) {
+ // The peer went away: expected at every VM stop (also a VM
+ // crash). No throwable/stack trace for the routine case.
+ log("host bridge peer closed: ${e.message}", null)
} catch (e: Exception) {
log("host bridge loop error: ${e.message}", e)
} finally {
diff --git a/app/src/main/java/com/excp/podroid/engine/hostbridge/HostTransport.kt b/app/src/main/java/com/excp/podroid/engine/hostbridge/HostTransport.kt
index 5c0a8d6..26a3607 100644
--- a/app/src/main/java/com/excp/podroid/engine/hostbridge/HostTransport.kt
+++ b/app/src/main/java/com/excp/podroid/engine/hostbridge/HostTransport.kt
@@ -35,7 +35,15 @@ import java.nio.charset.CodingErrorAction
*/
internal const val MAX_REQUEST_LINE_BYTES = 8191
-internal class RequestLineTooLongException : IOException("host request line exceeds $MAX_REQUEST_LINE_BYTES bytes")
+/**
+ * A malformed request from the guest daemon (too long, or not valid UTF-8):
+ * a protocol violation from a hostile or buggy guest, not a peer disconnect.
+ * Callers should log this with its stack trace like any other real bug.
+ */
+internal open class HostProtocolViolationException(message: String, cause: Throwable? = null) : IOException(message, cause)
+
+internal class RequestLineTooLongException :
+ HostProtocolViolationException("host request line exceeds $MAX_REQUEST_LINE_BYTES bytes")
/** Reads exactly one complete UTF-8 request line, or null for EOF. */
internal object HostRequestLineReader {
@@ -58,7 +66,7 @@ internal object HostRequestLineReader {
.decode(ByteBuffer.wrap(bytes, 0, contentSize))
.toString()
} catch (e: CharacterCodingException) {
- throw IOException("invalid UTF-8 request", e)
+ throw HostProtocolViolationException("invalid UTF-8 request", e)
}
}
if (size == bytes.size) throw RequestLineTooLongException()
diff --git a/app/src/test/java/com/excp/podroid/engine/hostbridge/HostRequestServerTest.kt b/app/src/test/java/com/excp/podroid/engine/hostbridge/HostRequestServerTest.kt
index 48043fa..d24016f 100644
--- a/app/src/test/java/com/excp/podroid/engine/hostbridge/HostRequestServerTest.kt
+++ b/app/src/test/java/com/excp/podroid/engine/hostbridge/HostRequestServerTest.kt
@@ -2,6 +2,7 @@ package com.excp.podroid.engine.hostbridge
import com.excp.podroid.data.repository.AddRuleResult
import com.excp.podroid.data.repository.PortForwardRule
+import java.io.IOException
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
@@ -134,6 +135,95 @@ class HostRequestServerTest {
}
}
+ @Test(timeout = 5000) fun ioExceptionFromReadLogsWithoutAThrowable() {
+ val scope = testScope()
+ try {
+ val logged = CountDownLatch(1)
+ var loggedMessage: String? = null
+ var loggedError: Throwable? = null
+ val transport = ThrowingTransport(IOException("read failed: ECONNRESET"))
+ val server = HostRequestServer(
+ openTransport = { transport },
+ dispatcher = dispatcher(),
+ scope = scope,
+ log = { message, error ->
+ if (message.startsWith("host bridge peer closed")) {
+ loggedMessage = message
+ loggedError = error
+ logged.countDown()
+ }
+ },
+ )
+ server.start()
+
+ assertTrue(logged.await(1, TimeUnit.SECONDS))
+ assertEquals("host bridge peer closed: read failed: ECONNRESET", loggedMessage)
+ assertEquals(null, loggedError)
+ } finally {
+ scope.cancel()
+ }
+ }
+
+ @Test(timeout = 5000) fun nonIoExceptionFromReadLogsWithItsThrowable() {
+ val scope = testScope()
+ try {
+ val logged = CountDownLatch(1)
+ var loggedMessage: String? = null
+ var loggedError: Throwable? = null
+ val failure = IllegalStateException("boom")
+ val transport = ThrowingTransport(failure)
+ val server = HostRequestServer(
+ openTransport = { transport },
+ dispatcher = dispatcher(),
+ scope = scope,
+ log = { message, error ->
+ if (message.startsWith("host bridge loop error")) {
+ loggedMessage = message
+ loggedError = error
+ logged.countDown()
+ }
+ },
+ )
+ server.start()
+
+ assertTrue(logged.await(1, TimeUnit.SECONDS))
+ assertEquals("host bridge loop error: boom", loggedMessage)
+ assertEquals(failure, loggedError)
+ } finally {
+ scope.cancel()
+ }
+ }
+
+ @Test(timeout = 5000) fun protocolViolationFromReadLogsWithItsThrowable() {
+ val scope = testScope()
+ try {
+ val logged = CountDownLatch(1)
+ var loggedMessage: String? = null
+ var loggedError: Throwable? = null
+ val failure = RequestLineTooLongException()
+ val transport = ThrowingTransport(failure)
+ val server = HostRequestServer(
+ openTransport = { transport },
+ dispatcher = dispatcher(),
+ scope = scope,
+ log = { message, error ->
+ if (message.startsWith("host bridge loop error")) {
+ loggedMessage = message
+ loggedError = error
+ logged.countDown()
+ }
+ },
+ )
+ server.start()
+
+ assertTrue(logged.await(1, TimeUnit.SECONDS))
+ assertEquals("host bridge loop error: ${failure.message}", loggedMessage)
+ assertEquals(failure, loggedError)
+ } finally {
+ scope.cancel()
+ }
+ }
+
private fun awaitChildren(scope: CoroutineScope) = runBlocking {
withTimeout(1000) { scope.coroutineContext[Job]!!.children.toList().joinAll() }
assertTrue(scope.coroutineContext[Job]!!.children.none())
@@ -189,3 +279,20 @@ class HostRequestServerTest {
}
}
}
+
+ /** Throws [failure] from its first read, then reports EOF so the loop does not spin. */
+ private class ThrowingTransport(private val failure: Exception) : HostTransport {
+ val closed = CountDownLatch(1)
+ val closeCount = AtomicInteger(0)
+ private val returned = AtomicInteger(0)
+
+ override fun readRequest(): String? {
+ if (returned.getAndIncrement() == 0) throw failure
+ return null
+ }
+
+ override fun writeResponse(line: String) = Unit
+ override fun close() {
+ if (closeCount.incrementAndGet() == 1) closed.countDown()
+ }
+ }