From 6391fc3be2156ede836ae201456e19839293aec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20A=C3=9Fmann?= Date: Tue, 10 Mar 2026 10:30:46 +0100 Subject: [PATCH] Fix WakeLock finalized while still held (#35) Release any existing WakeLock/WifiLock before re-acquiring in startService() to prevent orphaned locks being GC'd while still held. Also handle null intents in onStartCommand (OS sticky restart) by calling startService() so locks are properly re-acquired after a system kill. --- .../flutter_background/IsolateHolderService.kt | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/android/src/main/kotlin/de/julianassmann/flutter_background/IsolateHolderService.kt b/android/src/main/kotlin/de/julianassmann/flutter_background/IsolateHolderService.kt index 21e0cc5..7c8889d 100644 --- a/android/src/main/kotlin/de/julianassmann/flutter_background/IsolateHolderService.kt +++ b/android/src/main/kotlin/de/julianassmann/flutter_background/IsolateHolderService.kt @@ -47,11 +47,12 @@ class IsolateHolderService : Service() { } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int) : Int { - if (intent?.action == ACTION_SHUTDOWN) { - cleanupService() - stopSelf() - } else if (intent?.action == ACTION_START) { - startService() + when (intent?.action) { + ACTION_SHUTDOWN -> { + cleanupService() + stopSelf() + } + else -> startService() // ACTION_START or null (OS sticky restart) } return START_STICKY } @@ -112,6 +113,7 @@ class IsolateHolderService : Service() { .setPriority(FlutterBackgroundPlugin.notificationImportance) .build() + wakeLock?.apply { if (isHeld) release() } (getSystemService(Context.POWER_SERVICE) as PowerManager).run { wakeLock = newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_TAG).apply { setReferenceCounted(false) @@ -119,6 +121,9 @@ class IsolateHolderService : Service() { } } + if (FlutterBackgroundPlugin.enableWifiLock) { + wifiLock?.apply { if (isHeld) release() } + } (applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager).run { wifiLock = createWifiLock(WifiManager.WIFI_MODE_FULL, WIFILOCK_TAG).apply { setReferenceCounted(false)