From 9892a8e8ec125ef185963822cdcd6f21cd88e748 Mon Sep 17 00:00:00 2001 From: Tomas Dvorak Date: Thu, 5 Nov 2026 08:52:57 +0100 Subject: [PATCH 1/2] fix: survive oversized notification extras parcels MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit First read of Notification.extras unparcels the entire bundle. A notification carrying a huge parcelled payload (chat history, embedded images) materialized enough objects to exhaust the 256MB heap inside AutomationNotificationListener.onNotificationPosted — OutOfMemoryError on 0.19.18, moto g23 Android 14. Title/text/sender are now read behind runCatching and degrade to empty strings, so pkg, category, group state and timestamps still dispatch the trigger and the shade refresh still runs. Shade refreshes are also serialized behind a Mutex: each unparcels every active notification's extras, and concurrent runs multiplied the same heap spike. Authored By: TDvorak --- .../AutomationNotificationListener.kt | 53 ++++++++++++++----- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/automation-android/src/main/kotlin/com/tdvorak/nothingmodes/automation/lifecycle/AutomationNotificationListener.kt b/automation-android/src/main/kotlin/com/tdvorak/nothingmodes/automation/lifecycle/AutomationNotificationListener.kt index e6b9e5b..231da71 100644 --- a/automation-android/src/main/kotlin/com/tdvorak/nothingmodes/automation/lifecycle/AutomationNotificationListener.kt +++ b/automation-android/src/main/kotlin/com/tdvorak/nothingmodes/automation/lifecycle/AutomationNotificationListener.kt @@ -17,6 +17,8 @@ import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel import kotlinx.coroutines.isActive import kotlinx.coroutines.launch +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock import java.util.concurrent.atomic.AtomicInteger /** @@ -35,6 +37,7 @@ import java.util.concurrent.atomic.AtomicInteger class AutomationNotificationListener : NotificationListenerService() { private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO) private val refreshGeneration = AtomicInteger() + private val refreshMutex = Mutex() private var database: NothingModesDatabase? = null override fun onCreate() { @@ -52,11 +55,8 @@ class AutomationNotificationListener : NotificationListenerService() { override fun onNotificationPosted(sbn: StatusBarNotification) { val pkg = sbn.packageName ?: return val notification = sbn.notification ?: return - val extras = notification.extras - val title = extras.getString(Notification.EXTRA_TITLE, "") ?: "" - val text = extras.getString(Notification.EXTRA_TEXT, "") ?: "" val category = notification.category ?: "" - val sender = extras.getString(Notification.EXTRA_SUB_TEXT, "") ?: "" + val (title, text, sender) = notification.readExtras(pkg) Log.d(TAG, "Notification posted from $pkg") @@ -123,19 +123,46 @@ class AutomationNotificationListener : NotificationListenerService() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return val generation = refreshGeneration.incrementAndGet() scope.launch { - val notifications = - runCatching { activeNotifications?.toList() ?: emptyList() }.getOrDefault(emptyList()) - if (!isActive || generation != refreshGeneration.get()) return@launch - ActiveNotifications.update(notifications.map { it.toSnapshot() }) + // Serialize refreshes: each one unparcels every active + // notification's extras, and concurrent runs would multiply the + // heap spike that OOMed on oversized parcels. + refreshMutex.withLock { + val notifications = + runCatching { activeNotifications?.toList() ?: emptyList() } + .getOrDefault(emptyList()) + if (!isActive || generation != refreshGeneration.get()) return@withLock + ActiveNotifications.update(notifications.map { it.toSnapshot() }) + } } } private fun StatusBarNotification.toSnapshot(): ActiveNotifications.NotificationSnapshot { - val notification = this.notification ?: return ActiveNotifications.NotificationSnapshot(packageName ?: "", "", "") - val extras = notification.extras - val title = extras?.getString(Notification.EXTRA_TITLE, "") ?: "" - val text = extras?.getString(Notification.EXTRA_TEXT, "") ?: "" - return ActiveNotifications.NotificationSnapshot(packageName ?: "", title, text) + val pkg = packageName ?: "" + val notification = notification + ?: return ActiveNotifications.NotificationSnapshot(pkg, "", "") + val (title, text) = notification.readExtras(pkg) + return ActiveNotifications.NotificationSnapshot(pkg, title, text) + } + + /** + * First extras access unparcels the whole bundle — a notification + * carrying an oversized parcelled payload (chat history, embedded + * images) can OOM the process mid-read. Title/text/sender are + * best-effort: pkg, category, group state and timestamps still + * dispatch without them. + */ + private fun Notification.readExtras(pkg: String): Triple { + val bundle = extras ?: return Triple("", "", "") + return runCatching { + Triple( + bundle.getString(Notification.EXTRA_TITLE, "") ?: "", + bundle.getString(Notification.EXTRA_TEXT, "") ?: "", + bundle.getString(Notification.EXTRA_SUB_TEXT, "") ?: "", + ) + }.getOrElse { + Log.w(TAG, "Notification extras unreadable for $pkg (${it.javaClass.simpleName})") + Triple("", "", "") + } } companion object { From 98bd6df89356beb8801da76de47062df03fc8370 Mon Sep 17 00:00:00 2001 From: Tomas Dvorak Date: Thu, 5 Nov 2026 08:57:06 +0100 Subject: [PATCH 2/2] fix: skip superseded shade refreshes before the binder call Review follow-up: the generation check ran after fetching and unparcelling every active notification, so a stale refresh still paid the full cost before discarding. Check before the fetch too; the post-fetch check stays so a refresh overtaken mid-flight cannot overwrite fresher snapshots. Adds the missing changelog entry. Authored By: TDvorak --- CHANGELOG.md | 5 +++++ .../automation/lifecycle/AutomationNotificationListener.kt | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a2b520..df1364c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## [Unreleased] + +### Fixed +- `OutOfMemoryError` in the notification listener (0.19.18, moto g23). The first read of a posted notification's extras unparcels the entire bundle, so one oversized parcel — chat history, embedded images — could exhaust the heap. Title, text and sender now degrade to empty when the parcel won't read, and the trigger still fires on package and category. Shade refreshes are serialized so concurrent reads can't stack the same spike. + ## [0.19.18] ### Fixed diff --git a/automation-android/src/main/kotlin/com/tdvorak/nothingmodes/automation/lifecycle/AutomationNotificationListener.kt b/automation-android/src/main/kotlin/com/tdvorak/nothingmodes/automation/lifecycle/AutomationNotificationListener.kt index 231da71..687704c 100644 --- a/automation-android/src/main/kotlin/com/tdvorak/nothingmodes/automation/lifecycle/AutomationNotificationListener.kt +++ b/automation-android/src/main/kotlin/com/tdvorak/nothingmodes/automation/lifecycle/AutomationNotificationListener.kt @@ -127,6 +127,10 @@ class AutomationNotificationListener : NotificationListenerService() { // notification's extras, and concurrent runs would multiply the // heap spike that OOMed on oversized parcels. refreshMutex.withLock { + // A superseded generation must not even pay the binder call + // and unparcel; re-check after the fetch so one overtaken + // mid-flight can't overwrite fresher snapshots. + if (!isActive || generation != refreshGeneration.get()) return@withLock val notifications = runCatching { activeNotifications?.toList() ?: emptyList() } .getOrDefault(emptyList())