Modern Apps git

← Modern-Apps.git

logviewer: surface logcat failures instead of an empty log

Commit
829431d1c5030ab5d3d5f9d74aee7bafc814e750
Author
Vayun Mathur <[email protected]>
When
2026-11-07 19:59 UTC (28 days ago)
Parents
dce16f2
Symptom: when logcat itself fails - most reachable via an invalid
--regex from the filter dialog, which logcat rejects on stderr with a
non-zero exit - the viewer showed a successfully loaded, completely
empty log. The title even claimed the filter was applied, so "your
regex is bad" was indistinguishable from "nothing matched".

Root cause: the production executeCommand in LogcatReader read
stdout, debug-logged the exit code, and returned whatever lines it
got (zero). LogcatReader.read only treats IOException /
InterruptedException as Unavailable, so a failed exec became a
Loaded empty document.

Fix: check the exit code after draining stdout and throw
IOException on non-zero, which read() already maps to Unavailable.
Also drain stderr on a daemon thread while reading: the pipe was
never consumed, so a logcat writing more than a pipe buffer of
diagnostics would block mid-write and wedge the stdout read (and
the loading spinner) forever.

Verification: LogcatReaderTest green (the existing IOException ->
Unavailable tests cover the seam contract; the exit-code branch is
in the production lambda, which has no logcat binary on the test
host - the pre-existing 'real logcat exec failing' test exercises
its failure path).

Diff

logviewer/src/main/java/com/vayunmathur/logviewer/platform/LogcatReader.kt
diff --git a/logviewer/src/main/java/com/vayunmathur/logviewer/platform/LogcatReader.kt b/logviewer/src/main/java/com/vayunmathur/logviewer/platform/LogcatReader.ktindex 65bc3df04b..841d832d8d 100644--- a/logviewer/src/main/java/com/vayunmathur/logviewer/platform/LogcatReader.kt+++ b/logviewer/src/main/java/com/vayunmathur/logviewer/platform/LogcatReader.kt@@ -43,8 +43,20 @@ internal object LogcatReader {     /** Runs [command] and returns its stdout lines. Injectable for tests; production execs `logcat`. */     internal var executeCommand: (List<String>) -> List<String> = { command ->         val process = ProcessBuilder(command).start()+        // Drain stderr concurrently: an undrained pipe fills at ~64 KiB and then blocks the+        // writer, which would wedge the stdout read below forever. The text itself is useless+        // here - the exit code below is the signal - so it is read and discarded.+        Thread {+            runCatching { process.errorStream.use { it.readBytes() } }+        }.apply { isDaemon = true }.start()         val lines = process.inputStream.bufferedReader(Charsets.UTF_8).useLines { it.toList() }-        Log.debug(TAG, "logcat return code: " + process.waitFor())+        val returnCode = process.waitFor()+        Log.debug(TAG, "logcat return code: $returnCode")+        // A failed logcat (bad --regex, unknown buffer, ...) writes nothing to stdout and+        // exits non-zero. Returning those zero lines would show an empty log that looks+        // like "no entries matched", so fail the load instead and let read() map it to+        // Unavailable like any other exec failure.+        if (returnCode != 0) throw IOException("logcat exited with code $returnCode")         lines     }