fix: download monitor polls off the main thread so slow storage can't ANR the session (#2020)

* fix: download monitor polls off the main thread so slow storage can't ANR the session

* fix: cancel the queued download poll before the service shuts its executor down

* fix: ignore download monitor submissions that arrive after the service shut it down
This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-11-02 17:53:02 -05:00
1 parent 5fe3021e5e
commit cae095ce30
1 file changed
+37 -13
@@ -70,6 +70,7 @@ class SessionService : Service() {
private val mainHandler = Handler(Looper.getMainLooper())
private var suspendController: SessionSuspendController? = null
private val steamDownloadMonitor = SteamDownloadMonitor()
private val downloadMonitorExecutor = java.util.concurrent.Executors.newSingleThreadExecutor { Thread(it, "steam-download-monitor").apply { isDaemon = true } }
private var downloadMonitorTask: Runnable? = null
private var downloadActive = false
private var clientInstallActive = false
@@ -346,7 +347,7 @@ class SessionService : Service() {
screenOn = readScreenOn()
manualPauseRequested = false
suspendDownloadNow = false
steamDownloadMonitor.reset()
runOnDownloadMonitor { steamDownloadMonitor.reset() }
downloadActive = false
clientInstallActive = false
steamSleepToken = null
@@ -1979,6 +1980,9 @@ class SessionService : Service() {
(getSystemService(Context.DISPLAY_SERVICE) as? DisplayManager)?.unregisterDisplayListener(displayListener)
displayListenerRegistered = false
}
downloadMonitorTask?.let(mainHandler::removeCallbacks)
downloadMonitorTask = null
downloadMonitorExecutor.shutdown()
super.onDestroy()
}
@@ -2102,12 +2106,20 @@ class SessionService : Service() {
SessionState.running && SessionState.mode == MODE_STEAM && suspendPolicy != SessionPrefs.SUSPEND_NEVER &&
(steamDownloadsInBackground || !SessionState.firstFrameSeen)
private fun runOnDownloadMonitor(task: () -> Unit) {
try {
downloadMonitorExecutor.execute(task)
} catch (e: java.util.concurrent.RejectedExecutionException) {
Log.d(TAG, "download monitor already shut down")
}
}
private fun updateDownloadMonitoring() {
if (!downloadWatchWanted()) {
val wasActive = downloadActive
downloadMonitorTask?.let(mainHandler::removeCallbacks)
downloadMonitorTask = null
steamDownloadMonitor.reset()
runOnDownloadMonitor { steamDownloadMonitor.reset() }
downloadActive = false
if (clientInstallActive) SessionEvents.record("steam.client_download", mapOf("active" to false))
clientInstallActive = false
@@ -2129,18 +2141,30 @@ class SessionService : Service() {
val wasInstalling = clientInstallActive
clientInstallActive = !SessionState.firstFrameSeen &&
(clientInstallActive || SteamDownloadMonitor.clientInstallPending(root))
downloadActive = clientInstallActive || steamDownloadsInBackground && steamDownloadMonitor.poll(
root,
receivedBytes = TrafficStats.getUidRxBytes(android.os.Process.myUid()),
client = SessionState.steamClientArch,
mounts = steamLibraryMounts,
)
if (wasInstalling != clientInstallActive) {
SessionEvents.record("steam.client_download", mapOf("active" to clientInstallActive))
val receivedBytes = TrafficStats.getUidRxBytes(android.os.Process.myUid())
val client = SessionState.steamClientArch
val mounts = steamLibraryMounts
val self = this
val finish = { monitored: Boolean ->
if (downloadMonitorTask === self) {
downloadActive = clientInstallActive || monitored
if (wasInstalling != clientInstallActive) {
SessionEvents.record("steam.client_download", mapOf("active" to clientInstallActive))
}
if (wasActive != downloadActive) refreshNotification()
updateSuspendPolicy()
mainHandler.postDelayed(self, DOWNLOAD_POLL_MS)
}
}
if (clientInstallActive || !steamDownloadsInBackground) {
finish(false)
return
}
runOnDownloadMonitor {
val monitored = runCatching { steamDownloadMonitor.poll(root, receivedBytes = receivedBytes, client = client, mounts = mounts) }
.getOrDefault(false)
mainHandler.post { finish(monitored) }
}
if (wasActive != downloadActive) refreshNotification()
updateSuspendPolicy()
mainHandler.postDelayed(this, DOWNLOAD_POLL_MS)
}
}
downloadMonitorTask = poll