Automattic / Automattic/pocket-casts-android

ForegroundServiceStartNotAllowedException stopping playback

Open
#29 12 comments 0 reactions 0 assignees View on GitHub
[Area] Playback [Priority] Medium [Type] Bug Migrated
Dominant language
Kotlin
Stars
2.8k
Forks
308
Avg merge
1d 10h
Merged PRs (30d)
187

Description

We have many reports from Android 12 users claiming that the app crashes or the audio quality deteriorates with playback while the screen is off.

Here are the logs from 1 customer:

```
E 17/3 22:25:04 Playback service error
android.app.ForegroundServiceStartNotAllowedException: Service.startForeground() not allowed due to mAllowStartForeground false: service au.com.shiftyjelly.pocketcasts/.core.player.PlaybackService
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.setServiceForeground(IActivityManager.java:7096)
at android.app.Service.startForeground(Service.java:733)
at au.com.shiftyjelly.pocketcasts.core.player.PlaybackService$MediaControllerCallback.onPlaybackStateChangedWithNotification(PlaybackService.kt:10)
at au.com.shiftyjelly.pocketcasts.core.player.PlaybackService$MediaControllerCallback.access$onPlaybackStateChangedWithNotification(PlaybackService.kt:1)
at au.com.shiftyjelly.pocketcasts.core.player.PlaybackService$MediaControllerCallback$4.invoke(PlaybackService.kt:3)
at au.com.shiftyjelly.pocketcasts.core.player.PlaybackService$MediaControllerCallback$4.invoke(PlaybackService.kt:1)
at ek.l.b(Unknown Source:2)
at pj.r.onNext(LambdaObserver.java:2)
at uj.c2$a.d(ObservableObserveOn.java:8)
at uj.c2$a.run(ObservableObserveOn.java:3)
at ij.b$b.run(HandlerScheduler.java:1)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.am.ActiveServices.setServiceForegroundInnerLocked(ActiveServices.java:1837)
at com.android.server.am.ActiveServices.setServiceForegroundLocked(ActiveServices.java:1350)
at com.android.server.am.ActivityManagerService.setServiceForeground(ActivityManagerService.java:11903)
at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:3205)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2498)

```

```
ApplicationExitInfo(timestamp=18/03/22, 9:35 pm pid=19707 realUid=10237 packageUid=10237 definingUid=10237 user=0 process=au.com.shiftyjelly.pocketcasts reason=9 (EXCESSIVE RESOURCE USAGE) subreason=7 (EXCESSIVE CPU USAGE) status=0 importance=400 pss=158MB rss=175MB description=excessive cpu 22790 during 300121 dur=1781131 limit=2 state=empty trace=null
```

**Device info:**

```
App version : 7.19.2 (7566)
Sync account: rorypnd@gmail.com
Last Sync: 2022-03-18T10:34:59.987Z
Time: 2022-03-18 23:35:09 Local
2022-03-18 10:35:09 Utc
Background refresh: true

Phone: Google - oriole - Pixel 6 - oriole
Kernel: 5.10.43-android12-9-00007-g9771767708df-ab8009062 - abfarm-release-rbe-64-00086
Android version: 12 SDK 31
Phone ID: 929e6fab-74fe-4a75-8b4d-2fa40c562bf9
```

#4871871-zen
#4885182-zen

Both reported that setting the battery optimization to unrestricted for Pocket Casts resolved it. There have been users, however, who have reported that updating the battery optimization setting has not fixed the issue.

Contributor guide

Open the contributing guide

Research direction

Start with PlaybackService.kt, especially the MediaControllerCallback.onPlaybackStateChangedWithNotification path named in the Android 12 stack trace. Reproduce playback with the screen off on Android 12, comparing battery-optimization settings and the reported ForegroundServiceStartNotAllowedException. Done means playback remains stable without the foreground-service crash or reported audio deterioration.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
mobile
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.