From 15dc5822798f004ce10e3319ea069e5debdcf404 Mon Sep 17 00:00:00 2001 From: Jorge Martin Espinosa Date: Thu, 2 Apr 2026 11:10:47 +0200 Subject: [PATCH] Tentative fix for `ForegroundServiceStartNotAllowedException` (#6509) * Tentative fix for `ForegroundServiceStartNotAllowedException` When failing to start the service in foreground, don't crash. This is a helper to speed up the scheduling by keeping the CPU awake, not a critical part that should succeed * Simplify `DefaultPushHandlingWakeLock` It seems like restarting the service from background won't work in some cases, so don't try it. --- .../push/impl/push/DefaultPushHandlingWakeLock.kt | 12 ------------ .../push/impl/push/FetchPushForegroundService.kt | 8 +++++++- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/push/DefaultPushHandlingWakeLock.kt b/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/push/DefaultPushHandlingWakeLock.kt index 9713110042..27a921c219 100644 --- a/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/push/DefaultPushHandlingWakeLock.kt +++ b/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/push/DefaultPushHandlingWakeLock.kt @@ -14,7 +14,6 @@ import dev.zacsweers.metro.SingleIn import io.element.android.libraries.di.annotations.ApplicationContext import io.element.android.libraries.push.api.push.PushHandlingWakeLock import timber.log.Timber -import java.util.concurrent.atomic.AtomicInteger import kotlin.time.Duration @ContributesBinding(AppScope::class) @@ -22,24 +21,13 @@ import kotlin.time.Duration class DefaultPushHandlingWakeLock( @ApplicationContext private val context: Context, ) : PushHandlingWakeLock { - private val count = AtomicInteger(0) - override fun lock(time: Duration) { Timber.d("Acquiring wakelock for push handling, starting service.") FetchPushForegroundService.startIfNeeded(context) - - count.incrementAndGet() } override suspend fun unlock() { Timber.d("Releasing wakelock used for push handling.") FetchPushForegroundService.stop(context) - if (count.decrementAndGet() <= 0) { - Timber.d("No more wakelock needed for push handling, stopping service.") - count.set(0) - } else { - Timber.d("Wakelock still needed for push handling, restarting service | count: ${count.get()}.") - FetchPushForegroundService.startIfNeeded(context) - } } } diff --git a/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/push/FetchPushForegroundService.kt b/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/push/FetchPushForegroundService.kt index 6dc6bdfa0e..da5dc80707 100644 --- a/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/push/FetchPushForegroundService.kt +++ b/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/push/FetchPushForegroundService.kt @@ -145,7 +145,13 @@ class FetchPushForegroundService : Service() { fun start(context: Context) { val intent = Intent(context, FetchPushForegroundService::class.java) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - context.startForegroundService(intent) + runCatchingExceptions { context.startForegroundService(intent) } + .onFailure { throwable -> + Timber.e( + throwable, + "Failed to start FetchPushForegroundService, notifications may take longer than usual to sync" + ) + } } else { context.startService(intent) }