Collect a session's logs off the main thread, and wait for logcat properly

Leaving the desktop hung the app until Android ANR'd it, and the log I had just
added is what showed why: stopSession runs on the main thread - the
notification's Stop action arrives there - and it called the collector
synchronously. That meant copying the compositor's log, scrubbing every Steam
log line by line (42 files on the measured run) and then waiting on logcat, all
on the main thread: about four and a half seconds, ending in signal 3 and
stack traces written to tombstoned.

    20:45:32.721  session ended: 137
    20:45:34.280  collected 42 Steam log(s), scrubbed          <- main thread
    20:45:34.282  started logcat -b crash                      <- main thread
    20:45:36.537  reacting to signal 3 ... Wrote stack traces

The collector now runs on its own thread. Nothing in it touches the service's
state after the components have stopped, and the app process outlives the
service, so there is nothing to race.

The crash dump was also wrong twice over: it polled for the output being
non-empty, but the lines arrive through a callback on another thread, so it
declared the buffer empty every time and still paid the wait; and it printed
exec()'s return value as "rc" when that is a pid, never an exit status. It now
waits on the process ending, with a real timeout, and says which of the three
things happened - could not start, did not finish, or genuinely nothing has
crashed.

Nothing else in that session had gone wrong: labwc and Firefox's shutdown noise
is ordinary, and proot needing SIGKILL on the desktop path is expected.
This commit is contained in:
The412Banner committed 2026-09-21 20:54:20 -04:00
1 parent e69dfbd5e4
commit ec14fa1785
2 files changed
+23 -11

No files matched your search

@@ -88,20 +88,27 @@ object SessionLogCapture {
fun dumpCrashBuffer(target: File) {
try {
val lines = StringBuilder()
val rc = ProcessHelper.exec(
"/system/bin/logcat -b crash -d -v threadtime -t 400", null, target.parentFile, null,
) { line -> lines.append(line).append('\n') }
// exec returns a pid and runs on; give the dump a moment to finish rather than racing it.
var waited = 0
while (waited < 3000 && lines.isEmpty()) {
Thread.sleep(100)
waited += 100
}
val done = java.util.concurrent.CountDownLatch(1)
// Wait on the process ENDING, not on the first line arriving: the lines come through a
// callback on another thread, so a poll for "is it empty yet" declared an empty buffer
// every time and still cost the wait. exec() returns a pid, never an exit status - the
// first version printed that pid as "rc", which was nonsense in the file.
val pid = ProcessHelper.exec(
"/system/bin/logcat -b crash -d -v threadtime -t 400", null, target.parentFile,
{ done.countDown() },
) { line -> synchronized(lines) { lines.append(line).append('\n') } }
val finished = pid != -1 && done.await(10, java.util.concurrent.TimeUnit.SECONDS)
val body = synchronized(lines) { lines.toString() }
target.writeText(
"Android's crash buffer, as it stood when this session ended.\n" +
"Not only this app: anything on the device that crashed is in here, which is the\n" +
"point - a session killed by the system leaves its trace here and nowhere else.\n\n" +
(if (lines.isEmpty()) "(empty, or this ROM does not hand an app the crash buffer; logcat rc=$rc)\n" else lines.toString())
when {
body.isNotEmpty() -> body
pid == -1 -> "(logcat could not be started at all)\n"
!finished -> "(logcat did not finish within 10 s; nothing captured)\n"
else -> "(the crash buffer is empty - nothing on the device has crashed recently)\n"
}
)
} catch (e: Exception) {
Log.w(TAG, "could not dump the crash buffer", e)
@@ -447,7 +447,12 @@ class SessionService : Service() {
sessionPid = -1
Thread({ teardown(prootPid) }, "session-teardown").start()
}
collectSessionArtifacts()
// On its own thread, never here: stopSession runs on the main thread (the notification's
// Stop action arrives there), and collecting means copying the compositor's log, scrubbing
// every Steam log line by line - 42 files on one measured run - and waiting for logcat to
// dump the crash buffer. That was about four and a half seconds of blocked main thread,
// and Android ANR'd the app for it: the desktop session that would not let go.
Thread({ collectSessionArtifacts() }, "session-collect").start()
components.reversed().forEach {
try {
it.stop()