Leakcanary: Crash when starting services in background

Created on 8 Sep 2017  ·  29Comments  ·  Source: square/leakcanary

Using a Pixel on Oreo (release not beta), I put my app in the background. A minute or two later, it crashed. LeakCanary 1.5.1

09-07 19:18:24.049 31787-31804/com.myapp:leakcanary E/AndroidRuntime: FATAL EXCEPTION: IntentService[HeapAnalyzerService]
Process: com.myapp:leakcanary, PID: 31787
java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.myapp/com.squareup.leakcanary.DisplayLeakService (has extras) }: app is in background uid UidRecord{74385af u0a385 CEM  bg:+3m4s764ms idle procs:2 seq(0,0,0)}
    at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1505)
    at android.app.ContextImpl.startService(ContextImpl.java:1461)
    at android.content.ContextWrapper.startService(ContextWrapper.java:644)
    at com.squareup.leakcanary.AbstractAnalysisResultService.sendResultToListener(AbstractAnalysisResultService.java:38)
    at com.squareup.leakcanary.internal.HeapAnalyzerService.onHandleIntent(HeapAnalyzerService.java:59)
    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:68)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.os.HandlerThread.run(HandlerThread.java:65)

Most helpful comment

Yeah, that's because you can't start services in the background anymore on API 26.

@JakeWharton @jrodbx You'll have to use ContextCompat.startForegroundService(...) and post a notification while processing the leak. I think that's actually better UX anyway because I'm always like "Is LeakCanary doing anything? Why is it taking so long?" If you guys could have a progress bar showing the analysis progress, that would be really cool.

All 29 comments

Yeah, that's because you can't start services in the background anymore on API 26.

@JakeWharton @jrodbx You'll have to use ContextCompat.startForegroundService(...) and post a notification while processing the leak. I think that's actually better UX anyway because I'm always like "Is LeakCanary doing anything? Why is it taking so long?" If you guys could have a progress bar showing the analysis progress, that would be really cool.

Nexus 6p, v.8.0.
the same issue.

@jrodbx Is it possible to user JobIntentService instead of IntentService?
This would solve the issue and provide constant behavior across api versions.

https://developer.android.com/reference/android/support/v4/app/JobIntentService.html

Yes. Want to send a PR?

On Mon, Dec 18, 2017 at 10:16 AM shmuelr notifications@github.com wrote:

@jrodbx https://github.com/jrodbx Is it possible to user
JobIntentService instead of IntentService?
This would solve the issue and provide constent behaviour across api
versions.

https://developer.android.com/reference/android/support/v4/app/JobIntentService.html


You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub
https://github.com/square/leakcanary/issues/846#issuecomment-352454878,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAEEEY-JzAF6QC4vrkECM31CD4M3VG8jks5tBoGygaJpZM4PQgqW
.

Yup, working on it right now. Just wanted to make sure its ok that LeakCanary depends on the Android Support Library

Everyone's already doing it, so we might as well jump off the bridge too.

On Mon, Dec 18, 2017 at 10:17 AM shmuelr notifications@github.com wrote:

Yup, working on it right now. Just wanted to make sure its ok that
LeakCanary depends on the Android Support Library


You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub
https://github.com/square/leakcanary/issues/846#issuecomment-352455491,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAEEEWhELivx3d94mhhphRaorfpKF6JXks5tBoIMgaJpZM4PQgqW
.

A solution that doesn't depend on the Support Library would be preferred. I'll be looking into this soon as well, as it's been on my radar for way too long.

Update, the JobIntentService solution turned out to be more complicated then I anticipated.
Right now I have a working solution that replaces the IntentService with a foreground Service. Generally I'd shy away from a foreground service, but being that this is only using in development builds I'm feeling ok with it.

Honestly a foreground service is probably a good choice since there's an
uncertainty as to what's happening after the toast and before the existing
notification.

On Mon, Dec 18, 2017 at 1:11 PM shmuelr notifications@github.com wrote:

Update, the JobIntentService solution turned out to be more complicated
then I anticipated.
Right now I have a working solution that replaces the IntentService with a
foreground Service. Generally I'd shy away from a foreground service, but
being that this is only using in development builds I'm feeling ok with it.


You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub
https://github.com/square/leakcanary/issues/846#issuecomment-352511659,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAEEEdx3RN9pjzAa4F7oxqC388VhZPDRks5tBqrtgaJpZM4PQgqW
.

@jrodbx #912 comments are closed, so forgive me for posting here.
Do you want any help with the review?

+1 to foreground service.

@pyricau I signed license agreement. We are good to go.

:confounded:

there is pull a request since December, could you please just merge it.

I'm looking at it right now.

Work in progress #1003

Fixed by #1003

@pyricau Any chance a release will be made with this fix before the 1.6 release?

Probably not, this is a large change.

Ah I see there's a snapshot though. Very excited for this to be released 😁

Trying out the 1.6-SNAPSHOT, my app crashed in the background.

E/AndroidRuntime: FATAL EXCEPTION: LeakCanary-Heap-Dump
    Process: com.myapp, PID: 6427
    java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.myappr/com.squareup.leakcanary.internal.HeapAnalyzerService (has extras) }: app is in background uid UidRecord{d7c9cff u0a503 LAST bg:+2m27s688ms idle change:cached procs:2 seq(0,0,0)}
        at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1521)
        at android.app.ContextImpl.startService(ContextImpl.java:1477)
        at android.content.ContextWrapper.startService(ContextWrapper.java:650)
        at com.squareup.leakcanary.internal.HeapAnalyzerService.runAnalysis(HeapAnalyzerService.java:48)
        at com.squareup.leakcanary.ServiceHeapDumpListener.analyze(ServiceHeapDumpListener.java:36)
        at com.squareup.leakcanary.RefWatcher.ensureGone(RefWatcher.java:153)
        at com.squareup.leakcanary.RefWatcher$1.run(RefWatcher.java:120)
        at com.squareup.leakcanary.AndroidWatchExecutor$3.run(AndroidWatchExecutor.java:79)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.os.HandlerThread.run(HandlerThread.java:65)

@pyricau yeah, I don't see a ContextCompat.startServiceForeground(...) in that PR. 🤷‍♂️

The service is started and then calls startForeground from its onCreate method.

@eygraber Are you able to reproduce this with the sample app? If yes, do you have a recipe?

Yes, but that's not enough. It'll crash when you try to _start_ the service. So you need to call the context stuff I mentioned above to tell Android you plan on becoming a foreground service in the next few seconds

@pyricau I haven't tried with the sample app, and I can repro it with my app most of the time by starting the app, and pressing home. After a couple of minutes the app crashes.

What @SUPERCILEX is correct though. If the service isn't started using ContextCompat.startServiceForeground it will crash if the app is in the background.

@SUPERCILEX @eygraber I just merged that fix to master, a new snapshot should be automatically published soon, please let us know if that fixes it.

I'm going to assume this is now solved, and close.

actually, not yet, I still got crash in Android O, with leakcanary version 1.6.3

Please upgrade to the latest release of LeakCanary.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

basti-shi031 picture basti-shi031  ·  3Comments

pyricau picture pyricau  ·  4Comments

vincent-paing picture vincent-paing  ·  3Comments

divyenduz picture divyenduz  ·  6Comments

BraisGabin picture BraisGabin  ·  6Comments