Unable to (re)start WebsocketManager on screen on due to background restrictions
See original GitHub issueHome Assistant Android version: beta-1944-6994eacf
Android version: 12
Phone model: Google Pixel 4a
Home Assistant version: Core 2021.12.10
Last working Home Assistant release (if known): n/a, new feature
Description of problem: I upgraded to the beta version directly from the latest stable release (2022.1.1). No settings changed for websocket notifications (so: connect while screen is on). I noticed that the notification that the app was connected didn’t come up after picking up my phone again. Logcat shows an error (see below) which is caused by the Android 12 background start restrictions.
Traceback (if applicable, to get the logs you may refer to: https://companion.home-assistant.io/docs/troubleshooting/faqs/#android-crash-logs):
Slightly edited to start when the screen turns on, and to include a message from the ActivityManager
2022-01-19 21:04:29.999 9988-9988/? D/SensorReceiver: Sensor is_interactive corresponding to received event android.intent.action.SCREEN_ON is disabled, skipping sensors update
2022-01-19 21:04:30.002 9988-9988/? D/ButtonWidget: Broadcast received:
Broadcast action: android.intent.action.SCREEN_ON
AppWidgetId: -1
2022-01-19 21:04:30.002 9988-9988/? D/ButtonWidget: Updating all widgets
2022-01-19 21:04:30.048 9988-9988/? D/UrlRepository: localUrl is: true and usesInternalSsid is: true
2022-01-19 21:04:30.058 9988-9988/? D/MediaPlayCtrlsWidget: Broadcast received:
Broadcast action: android.intent.action.SCREEN_ON
AppWidgetId: -1
2022-01-19 21:04:30.063 9988-9988/? D/UrlRepository: localUrl is: true and usesInternalSsid is: true
2022-01-19 21:04:30.071 9988-9988/? D/UrlRepository: localUrl is: true and usesInternalSsid is: true
2022-01-19 21:04:30.093 9988-10057/? D/WebSockManager: Starting to listen to Websocket
2022-01-19 21:04:30.098 9988-10073/? I/WM-Processor: Moving WorkSpec (2698abf6-4e72-456d-b0bd-09ec4047eb87) to the foreground
2022-01-19 21:04:30.114 9988-10069/? E/WM-WorkerWrapper: Work [ id=2698abf6-4e72-456d-b0bd-09ec4047eb87, tags={ io.homeassistant.companion.android.websocket.WebsocketManager } ] failed because it threw an exception/error
java.util.concurrent.ExecutionException: android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service io.homeassistant.companion.android/androidx.work.impl.foreground.SystemForegroundService
at androidx.work.impl.utils.futures.AbstractFuture.getDoneValue(AbstractFuture.java:516)
at androidx.work.impl.utils.futures.AbstractFuture.get(AbstractFuture.java:475)
at androidx.work.impl.WorkerWrapper$2.run(WorkerWrapper.java:311)
at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:920)
Caused by: android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service io.homeassistant.companion.android/androidx.work.impl.foreground.SystemForegroundService
at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:54)
at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:50)
at android.os.Parcel.readParcelable(Parcel.java:3333)
at android.os.Parcel.createExceptionOrNull(Parcel.java:2420)
at android.os.Parcel.createException(Parcel.java:2409)
at android.os.Parcel.readException(Parcel.java:2392)
at android.os.Parcel.readException(Parcel.java:2334)
at android.app.IActivityManager$Stub$Proxy.startService(IActivityManager.java:5971)
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1847)
at android.app.ContextImpl.startForegroundService(ContextImpl.java:1823)
at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:779)
at androidx.core.content.ContextCompat$Api26Impl.startForegroundService(ContextCompat.java:931)
at androidx.core.content.ContextCompat.startForegroundService(ContextCompat.java:703)
at androidx.work.impl.Processor.startForeground(Processor.java:161)
at androidx.work.impl.utils.WorkForegroundUpdater$1.run(WorkForegroundUpdater.java:100)
at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:920)
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.am.ActiveServices.startServiceLocked(ActiveServices.java:691)
at com.android.server.am.ActiveServices.startServiceLocked(ActiveServices.java:616)
at com.android.server.am.ActivityManagerService.startService(ActivityManagerService.java:11850)
at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:2519)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2498)
2022-01-19 21:04:30.115 9988-10069/? I/WM-WorkerWrapper: Worker result FAILURE for Work [ id=2698abf6-4e72-456d-b0bd-09ec4047eb87, tags={ io.homeassistant.companion.android.websocket.WebsocketManager } ]
2022-01-19 21:04:30.118 1740-2686/? W/ActivityManager: Background start not allowed: service Intent { act=ACTION_STOP_FOREGROUND cmp=io.homeassistant.companion.android/androidx.work.impl.foreground.SystemForegroundService } to io.homeassistant.companion.android/androidx.work.impl.foreground.SystemForegroundService from pid=9988 uid=10365 pkg=io.homeassistant.companion.android startFg?=false
2022-01-19 21:04:30.118 9988-10069/? E/WM-Processor: Unable to stop foreground service
android.app.BackgroundServiceStartNotAllowedException: Not allowed to start service Intent { act=ACTION_STOP_FOREGROUND cmp=io.homeassistant.companion.android/androidx.work.impl.foreground.SystemForegroundService }: app is in background uid UidRecord{f43bc62 u0a365 TRNB idle procs:0 seq(0,0,0)}
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1861)
at android.app.ContextImpl.startService(ContextImpl.java:1817)
at android.content.ContextWrapper.startService(ContextWrapper.java:774)
at androidx.work.impl.Processor.stopForegroundService(Processor.java:318)
at androidx.work.impl.Processor.stopForeground(Processor.java:224)
at androidx.work.impl.WorkerWrapper.resolve(WorkerWrapper.java:460)
at androidx.work.impl.WorkerWrapper.resetPeriodicAndResolve(WorkerWrapper.java:571)
at androidx.work.impl.WorkerWrapper.handleResult(WorkerWrapper.java:490)
at androidx.work.impl.WorkerWrapper.onWorkFinished(WorkerWrapper.java:354)
at androidx.work.impl.WorkerWrapper$2.run(WorkerWrapper.java:331)
at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:920)
Screenshot of problem:
Additional information:
Issue Analytics
- State:
- Created 2 years ago
- Comments:13 (12 by maintainers)
Top GitHub Comments
yup looks to be the same from the google issue tracker:
yup and also safe to assume in those cases user had already granted background permissions as we request them with enabling location tracking.