From db0ee92d50de8b4d6826354dffbb173fb941e602 Mon Sep 17 00:00:00 2001 From: The412Banner <205237651+The412Banner@users.noreply.github.com> Date: Thu, 3 Dec 2026 15:15:16 +0000 Subject: [PATCH] chore(session): log Android's exit record when a guest worker is killed --- .../launcher/runtime/GuestWorkers.kt | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/GuestWorkers.kt b/app/src/main/java/com/droiddeck/launcher/runtime/GuestWorkers.kt index 4d6a1de6fb..4a848496ae 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/GuestWorkers.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/GuestWorkers.kt @@ -20,6 +20,7 @@ import android.system.Os import android.system.OsConstants import android.system.StructPollfd import android.util.Log +import com.droiddeck.launcher.core.DeviceReport import org.json.JSONObject import java.io.DataInputStream import java.io.File @@ -646,28 +647,34 @@ object GuestWorkers { private fun reportEnd(app: Context, slot: Slot) { val onExit = slot.onExit ?: return - Thread({ onExit(exitStatus(app, slot.pid)) }, "guest-worker-exit").start() + Thread({ onExit(exitStatus(app, slot.pid, slot.session > stopped)) }, "guest-worker-exit").start() } /** The worker's exit as a wait status (exit code << 8, or the signal), -1 when unknown. */ - private fun exitStatus(app: Context, pid: Int): Int { + private fun exitStatus(app: Context, pid: Int, running: Boolean): Int { if (pid <= 0 || android.os.Build.VERSION.SDK_INT < 30) return -1 val am = app.getSystemService(ActivityManager::class.java) - var recorded = false + var record: android.app.ApplicationExitInfo? = null + fun killed(status: Int): Int { + if (running) record?.let { + Log.w(TAG, "worker pid $pid ended: " + DeviceReport.exitSummary(it.processName, it.reason, it.status, it.description)) + } + return status + } repeat(20) { am.getHistoricalProcessExitReasons(app.packageName, pid, 1).firstOrNull()?.let { - recorded = true + record = it // an exit code only when the process exited itself; a signal number otherwise (a native crash) if (it.reason == android.app.ApplicationExitInfo.REASON_EXIT_SELF) return (it.status and 0xff) shl 8 - if (it.status in 1..64) return it.status and 0x7f + if (it.status in 1..64) return killed(it.status and 0x7f) // Android records the death first and the zygote's status later (as UNKNOWN, 0, then // EXIT_SELF): a reason without a status may still be completed - if (it.reason != android.app.ApplicationExitInfo.REASON_UNKNOWN) return OsConstants.SIGKILL + if (it.reason != android.app.ApplicationExitInfo.REASON_UNKNOWN) return killed(OsConstants.SIGKILL) } Thread.sleep(50) } // killed by Android with no signal recorded, not a clean exit - return if (recorded) OsConstants.SIGKILL else -1 + return if (record != null) killed(OsConstants.SIGKILL) else -1 } /** The pids of session [of]'s guest programs running in workers. */