chore(session): log Android's exit record when a guest worker is killed

This commit is contained in:
The412Banner committed 2026-12-03 15:15:16 +00:00
1 parent f9eeef44ed
commit db0ee92d50
1 file changed
+14 -7
@@ -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. */