Closed Bug 1839039 Opened 2 years ago Closed 2 years ago

Crash: android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service org.mozilla.firefox/org.mozilla.fenix.downloads.DownloadService

Categories

(Firefox for Android :: Downloads, defect, P2)

Firefox 114
All
Android
defect

Tracking

()

RESOLVED FIXED
127 Branch
Tracking Status
firefox125 --- wontfix
firefox126 --- wontfix
firefox127 --- verified

People

(Reporter: egger, Assigned: rsainani, NeedInfo)

References

(Blocks 1 open bug)

Details

(Keywords: crash, reproducible, topcrash, Whiteboard: [fxdroid] [group4] )

Attachments

(2 files)

User Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0

Steps to reproduce:

use firefox, eventually freezes

actuallyI have plenty of those

Actual results:

af59c717-5280-4930-b078-74fd4b41fb14
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
----
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:558)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Caused by: java.lang.reflect.InvocationTargetException
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
 ... 1 more
Caused by: mozilla.components.lib.state.StoreException: Exception while reducing state
 at mozilla.components.lib.state.Store$exceptionHandler$1$1.run(Store.kt:5)
 at android.os.Handler.handleCallback(Handler.java:942)
 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:7884)
 ... 3 more
Caused by: android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service org.mozilla.firefox/org.mozilla.fenix.downloads.DownloadService
 at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:54)
 at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:50)
 at android.os.Parcel.readParcelableInternal(Parcel.java:4816)
 at android.os.Parcel.readParcelable(Parcel.java:4778)
 at android.os.Parcel.createExceptionOrNull(Parcel.java:3006)
 at android.os.Parcel.createException(Parcel.java:2995)
 at android.os.Parcel.readException(Parcel.java:2978)
 at android.os.Parcel.readException(Parcel.java:2920)
 at android.app.IActivityManager$Stub$Proxy.startService(IActivityManager.java:5302)
 at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1894)
 at android.app.ContextImpl.startForegroundService(ContextImpl.java:1870)
 at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:822)
 at androidx.core.content.ContextCompat$Api26Impl$$ExternalSyntheticApiModelOutline1.m(R8$$SyntheticClass:1)
 at androidx.core.content.ContextCompat$Api26Impl.startForegroundService(ContextCompat.java:1)
 at androidx.core.content.ContextCompat.startForegroundService(ContextCompat.java:7)
 at mozilla.components.feature.downloads.DownloadMiddleware.sendDownloadIntent$feature_downloads_release(DownloadMiddleware.kt:48)
 at mozilla.components.feature.downloads.DownloadMiddleware.invoke(DownloadMiddleware.kt:448)
 at mozilla.components.lib.state.internal.ReducerChainBuilder$build$1$1.invoke(ReducerChainBuilder.kt:14)
 at mozilla.components.feature.recentlyclosed.RecentlyClosedMiddleware.invoke(RecentlyClosedMiddleware.kt:258)
 at mozilla.components.lib.state.internal.ReducerChainBuilder$build$1$1.invoke(ReducerChainBuilder.kt:14)
 at mozilla.components.feature.session.middleware.LastAccessMiddleware.invoke(LastAccessMiddleware.kt:49)
 at mozilla.components.lib.state.internal.ReducerChainBuilder$build$1$1.invoke(ReducerChainBuilder.kt:14)
 at mozilla.components.lib.state.internal.ReducerChainBuilder$build$threadCheck$1.invoke(ReducerChainBuilder.kt:61)
 at mozilla.components.lib.state.internal.ReducerChainBuilder$build$1$1.invoke(ReducerChainBuilder.kt:14)
 at mozilla.components.lib.state.Store$dispatch$1.invokeSuspend(Store.kt:15)
 at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:9)
 at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:107)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
 at java.lang.Thread.run(Thread.java:1012)

Expected results:

firefox should continue running

Blocks: 1834647
See Also: → 1834649
Summary: firefox freeze → Crash: android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service org.mozilla.firefox/org.mozilla.fenix.downloads.DownloadService

The severity field is not set for this bug.
:jonalmeida, could you have a look please?

For more information, please visit BugBot documentation.

Flags: needinfo?(jonalmeida942)

Hi Mihai, as you were recently working on some adjustments for services due to Android 14 migration, I wanted to get your opinion about smth.

Currently we are launching DowloadService via startForegroundService, but we are not declaring the service as a foreground service in the manifest. I am curious if that's the reason for this crash, if so, then why don't we get this too often? What do you think?

Severity: -- → S2
Flags: needinfo?(jonalmeida942) → needinfo?(mcarare)
Priority: -- → P3
Duplicate of this bug: 1834649

The declaration of the service type in the manifest does not make a difference in this case, MediaSessionService already has it and it would still crash with the same stacktrace. The declaration will be required when targeting Android 14.

This is likely a bug in the OS, and there are several bugs opened and assigned (that means they are accepted as bugs) in the issuetracker.

The problem is specific to Android 12 devices. It looks like there is an edge case where the app is in the foreground, the service is started, and before the notification is displayed something is interrupting the flow and sending the app in the foreground. One example of such an event could be receiving a call.

Except for Google providing a fix or us refactoring the download flow, a try-catch should be the quicker fix.

Flags: needinfo?(mcarare)
See Also: → 1774678

I'm able to reproduce this issue consistently and have a fix planned.

STR

  1. Open Fenix and download a test file that is big enough that you can pause the download once it starts.
  2. Pause the download.
  3. Force close fenix.
  4. Open fenix and press home immediately going back to the device launcher screen
  5. Quickly open another app/activity.

This results in the crash on android S (SDK 31) and above as ForegroundServiceStartNotAllowedException is thrown. RestoreDownloadsUsecase triggers DownloadMiddleware to launch a foreground service for non completed/finished downloads. We should check if the app is in foreground before launching the service on SDK 31 and above, and generally do that for all foreground services.

Component: General → Downloads
Whiteboard: [fxdroid] [group4]

Looking at Google Play vitals, this occurred over 13k times for 1.9k users, added keywords. Sentry isn't able to merge multiple variants of the crash for some reason.

Whiteboard: [fxdroid] [group4] → [fxdroid] [group4]
Assignee: nobody → rsainani
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true
Priority: P3 → P2
Pushed by rsainani@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/802a05813889 Fix download foreground service crash for SDK 31 and above r=android-reviewers,amejiamarmol,mcarare
Status: ASSIGNED → RESOLVED
Closed: 2 years ago
Resolution: --- → FIXED
Target Milestone: --- → 127 Branch
Flags: qe-verify+

The patch landed in nightly and beta is affected.
:rsainani, is this bug important enough to require an uplift?

  • If yes, please nominate the patch for beta approval.
  • If no, please set status-firefox126 to wontfix.

For more information, please visit BugBot documentation.

Flags: needinfo?(rsainani)

Once it's verified by QE, I will request uplift to 126.

Flags: needinfo?(rsainani)

Hi, Rahul!

We were not able to reproduce the crash on older builds, following your steps from Comment5.
We've also tested on today's Nightly 127.0a1 from 4/25, and no crash.

Tested on several devices:

  • Samsung Galaxy Tab S8 Ultra (Android 12),
  • Poco M4 Pro (Android 12),
  • Realme GT Master Edition (Android 13), and
  • Oppo Find X3 Lite (Android 11).

I'll ask the reporter to verify as well on the latest Nightly, on the device that encountered the crash.

Flags: needinfo?(egger)

:rsainani next week is the final week of beta for Fx126.
The patch looks like a large change to take late in beta, though without information on uplift risk I can't be sure.

We don't have much time to take this.
It looks like QA has verified it hasn't introduced any issues, but were unable to verify the fix. (comment 12 from :miralobontiu)

Flags: needinfo?(rsainani)

The change checks for certain conditions to start the service so it shouldn't be risky and is covered by tests. But since QE hasn't been able to reproduce the issue, it's fine to let this ride the trains.

Flags: needinfo?(rsainani)
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: