Status Update
Comments
ra...@google.com <ra...@google.com> #2
@Override
public void onCreate() {
super.onCreate();
mWorkManagerImpl = WorkManagerImpl.getInstance(getApplicationContext());
if (mWorkManagerImpl == null) {
// This can occur if...
// 1. The app is performing an auto-backup. Prior to O, JobScheduler could erroneously
// try to send commands to JobService in this state (
// Application#onCreate nor ContentProviders have run, WorkManager won't be
// initialized. In this case, we should ignore all JobScheduler commands and tell it
// to retry.
But, current WorkManagerImpl.getInstance(Context context) implementation is that, it will never return null but throw IllegalStateException.
Isn't there's conflict among these 2 SystemJobService.onCreate logic, and WorkManagerImpl.getInstance logic?
ra...@google.com <ra...@google.com> #3
Branch: androidx-master-dev
commit e14104eb7008f82edd50b37e16bd119b307e0554
Author: Sumir Kataria <sumir@google.com>
Date: Mon Jun 24 10:50:57 2019
Fix for JobScheduler calls when backup is in progress.
The original fix (ag/Ia1604b7c4b98f6b31869e1161a0c1efce7fe46f2) relied
on a null-check, but the new version of WorkManager.getInstance(Context)
with the Context param doesn't return null; it throws an Exception. This
CL updates the three places where we explicitly check for the return value
internally:
1. SystemJobService (the main place where this would manifest)
2. RescheduleReceiver
3. WorkManagerTestInitHelper (for consistency)
Fixes: 135858602
Test: Ran existing tests
Change-Id: Ia232869712d0c78fd80e91d0a863b8c50a080400
M work/workmanager-testing/src/main/java/androidx/work/testing/WorkManagerTestInitHelper.java
M work/workmanager/src/main/java/androidx/work/impl/background/systemalarm/RescheduleReceiver.java
M work/workmanager/src/main/java/androidx/work/impl/background/systemjob/SystemJobService.java
ha...@gmail.com <ha...@gmail.com> #4
May I know when will it be ready for production? As, currently, this IllegalStateException is impacting our users.
da...@gmail.com <da...@gmail.com> #5
da...@gmail.com <da...@gmail.com> #6
Yes. We don't need on-demand initialization. May I know how does using old (deprecated) WorkManager.getInstance() in our app code, able to bypass this problem? Isn't the crash is origin from library internal SystemJobService?
We once thinking to bypass this problem, by switching back to Version 2.0.1 (The version just before WorkManager.getInstance(Context) is introduced). However, we notice Room version is being changed in between. Downgrading WorkManager version, might cause additional SQLite problem.
ra...@google.com <ra...@google.com> #7
I am curious: do you actually have reports of this bug on Android 9? It should've been fixed in the platform layer at that point. If possible, can you share any Android 9 specific logs/bugreports/stacktraces with device models?
ra...@google.com <ra...@google.com> #8
Based on your code comment, I thought this problem shouldn't occur in new Android but it happens some how. This is the log
Asus ZenFone 5 (ZE620KL) (ASUS_X00QD), Android 9 (Happen 2 times)
Asus ZenFone 5Z (ZS620KL/ZS621KL) (ASUS_Z01R_1), Android 9 (Happen 1 time)
3 incidents are having same log trace as below
java.lang.RuntimeException:
at android.app.ActivityThread.handleCreateService (ActivityThread.java:3570)
at android.app.ActivityThread.access$1300 (ActivityThread.java:200)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1672)
at android.os.Handler.dispatchMessage (Handler.java:106)
at android.os.Looper.loop (Looper.java:198)
at android.app.ActivityThread.main (ActivityThread.java:6732)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:858)
Caused by: java.lang.IllegalStateException:
at androidx.work.impl.WorkManagerImpl.getInstance (WorkManagerImpl.java:142)
at androidx.work.impl.background.systemjob.SystemJobService.onCreate (SystemJobService.java:53)
at android.app.ActivityThread.handleCreateService (ActivityThread.java:3558)
at android.app.ActivityThread.access$1300 (ActivityThread.java:200)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1672)
at android.os.Handler.dispatchMessage (Handler.java:106)
at android.os.Looper.loop (Looper.java:198)
at android.app.ActivityThread.main (ActivityThread.java:6732)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:858)
I attach the screenshot of Google Play Console as well. (I expect the stack trace should print programmer's message side-by-side with IllegalStateException. But it doesn't)
Feel free to let me know if you need any information from my side. Happy to help.
da...@gmail.com <da...@gmail.com> #9
da...@gmail.com <da...@gmail.com> #10
Thanks.
da...@gmail.com <da...@gmail.com> #11
ra...@google.com <ra...@google.com> #12
trace:
5 java.lang.IllegalStateException:WorkManager needs to be initialized via a ContentProvider#onCreate() or an Application#onCreate().
6 androidx.work.impl.background.systemjob.SystemJobService.onCreate(SystemJobService.java:68)
7 android.app.ActivityThread.handleCreateService(ActivityThread.java:3133)
8 android.app.ActivityThread.access$1900(ActivityThread.java:196)
9 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1622)
10 com.android.tony.defenselib.hook.HookHandle$1.handleMessage(HookHandle.java:30)
11 android.os.Handler.dispatchMessage(Handler.java:107)
12 android.os.Looper.loop(Looper.java:210)
13 android.app.ActivityThread.main(ActivityThread.java:5982)
14 java.lang.reflect.Method.invoke(Native Method)
15 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
16 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742)
this code cause crash:
if (!Application.class.equals(getApplication().getClass())) {
// During auto-backup, we don't get a custom Application subclass. This code path
// indicates we are either performing auto-backup or the user never used a custom
// Application class (or both).
throw new IllegalStateException("WorkManager needs to be initialized via a "
+ "ContentProvider#onCreate() or an Application#onCreate().");
}
my analysis:
code in handleCreateService() in ActivityThread.java:
" Application app = packageInfo.makeApplication(false, mInstrumentation);
service.attach(context, this,
"
handleCreateService--->. Application app = packageInfo.makeApplication(false, mInstrumentation); ---> forceDefaultAppClass is always false-->if have custom Application,will make custom Application ---> service.attach(context, this,
--> if have customApplication, the code : "Application.class.equals(getApplication().getClass()" in Service will be false , even it's in backup mode
ra...@google.com <ra...@google.com> #13
compileSdkVersion 30
implementation 'androidx.work:work-runtime-ktx:2.4.0'
Crash
TCT (Alcatel) 5056D
Android 6.0 (SDK 23)
48.aab
2 hours ago
java.lang.RuntimeException:
at android.app.ActivityThread.handleCreateService (ActivityThread.java:2887)
at android.app.ActivityThread.access$1900 (ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1427)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:148)
at android.app.ActivityThread.main (ActivityThread.java:5417)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException:
at androidx.work.impl.background.systemjob.SystemJobService.onCreate (SystemJobService.java:2)
at android.app.ActivityThread.handleCreateService (ActivityThread.java:2877)
at android.app.ActivityThread.access$1900 (ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1427)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:148)
at android.app.ActivityThread.main (ActivityThread.java:5417)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:616)
What could the walkaround this issue? Is there another way to initialize the WorkManager instance? I'm using
private fun getWorkManager(): WorkManager =
try {
WorkManager.getInstance(App.context)
} catch (e: Exception) {
e.printStackTrace()
sleep(100)
getWorkManager()
}
da...@gmail.com <da...@gmail.com> #14
Library version: androidx.work:work-runtime:2.4.0
Samsung Galaxy S8 (dreamlte), 3840MB RAM, Android 9
java.lang.RuntimeException:
at android.app.ActivityThread.handleBindApplication (ActivityThread.java:6227)
at android.app.ActivityThread.access$1200 (ActivityThread.java:237)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1785)
at android.os.Handler.dispatchMessage (Handler.java:106)
at android.os.Looper.loop (Looper.java:214)
at android.app.ActivityThread.main (ActivityThread.java:7050)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:965)
Caused by: java.lang.IllegalStateException:
at androidx.work.impl.WorkManagerImpl.getInstance (WorkManagerImpl.java:150)
at androidx.work.WorkManager.getInstance (WorkManager.java:184)
at br.com.rcgm.dicaslotofacilfull.task.WorkRequestSincronizarConcurso.enfileirar (WorkRequestSincronizarConcurso.java:20)
at br.com.rcgm.dicaslotofacilfull.BaseApplication.onCreate (BaseApplication.java:23)
at android.app.Instrumentation.callApplicationOnCreate (Instrumentation.java:1154)
at android.app.ActivityThread.handleBindApplication (ActivityThread.java:6222)
da...@gmail.com <da...@gmail.com> #15
I'm also facing same issue, in few devices.
Instance access : WorkManager.getInstance()
I'm not doing on-demand Initialization.
version used: 'androidx.work:work-runtime:2.3.4'
Device details
Brand: samsung
Model: Galaxy J2 Pro
Android version: 6.0.1
Fatal Exception: java.lang.RuntimeException: Unable to create service androidx.work.impl.background.systemjob.SystemJobService: java.lang.IllegalStateException: WorkManager needs to be initialized via a ContentProvider#onCreate() or an Application#onCreate().
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3883)
at android.app.ActivityThread.-wrap8(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1909)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by java.lang.IllegalStateException: WorkManager needs to be initialized via a ContentProvider#onCreate() or an Application#onCreate().
at androidx.work.impl.background.systemjob.SystemJobService.onCreate(SystemJobService.java:73)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3873)
at android.app.ActivityThread.-wrap8(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1909)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
ra...@google.com <ra...@google.com> #16
da...@gmail.com <da...@gmail.com> #17
ey...@gmail.com <ey...@gmail.com> #18
if (!Application.class.equals(getApplication().getClass())) {
// During auto-backup, we don't get a custom Application subclass. This code path
// indicates we are either performing auto-backup or the user never used a custom
// Application class (or both).
throw new IllegalStateException("WorkManager needs to be initialized via a "
+ "ContentProvider#onCreate() or an Application#onCreate().");
}
ra...@google.com <ra...@google.com> #19
this issue on workManager 2.5.0 when initialize WorkManager in background thread, bring it back to UI thread
da...@gmail.com <da...@gmail.com> #20
Stacktrace:
Caused by java.lang.IllegalStateException
WorkManager needs to be initialized via a ContentProvider#onCreate() or an Application#onCreate().
androidx.work.impl.background.systemjob.SystemJobService.onCreate (SystemJobService.java:73)
android.app.ActivityThread.handleCreateService (ActivityThread.java:3833)
android.app.ActivityThread.access$1500 (ActivityThread.java:229)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1881)
android.os.Handler.dispatchMessage (Handler.java:106)
android.os.Looper.loop (Looper.java:226)
android.app.ActivityThread.main (ActivityThread.java:7178)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:503)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:942)
For now, the device affected is
android 9 Realme.
Work Manager version: 2.6.0
ra...@google.com <ra...@google.com>
an...@google.com <an...@google.com> #21
Still happening with WorkManager 2.7.1 Samsung Galaxy S21 Ultra 5G Android 12 (SDK 31)
java.lang.RuntimeException:
at android.app.ActivityThread.handleCreateService (ActivityThread.java:4953)
at android.app.ActivityThread.access$1900 (ActivityThread.java:310)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2300)
at android.os.Handler.dispatchMessage (Handler.java:106)
at android.os.Looper.loopOnce (Looper.java:226)
at android.os.Looper.loop (Looper.java:313)
at android.app.ActivityThread.main (ActivityThread.java:8669)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1135)
Caused by: java.lang.IllegalStateException:
at androidx.work.impl.WorkManagerImpl.getInstance (WorkManagerImpl.java:158)
at androidx.work.impl.foreground.SystemForegroundDispatcher.<init> (SystemForegroundDispatcher.java:105)
at androidx.work.impl.foreground.SystemForegroundService.initializeDispatcher (SystemForegroundService.java:96)
at androidx.work.impl.foreground.SystemForegroundService.onCreate (SystemForegroundService.java:59)
at android.app.ActivityThread.handleCreateService (ActivityThread.java:4940)
at android.app.ActivityThread.access$1900 (ActivityThread.java:310)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2300)
at android.os.Handler.dispatchMessage (Handler.java:106)
at android.os.Looper.loopOnce (Looper.java:226)
at android.os.Looper.loop (Looper.java:313)
at android.app.ActivityThread.main (ActivityThread.java:8669)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1135)
ra...@google.com <ra...@google.com> #22
We recently moved our WorkManager initialization to the default pattern and have started seeing this same issue.
java.lang.RuntimeException: Unable to create service androidx.work.impl.background.systemjob.SystemJobService: java.lang.IllegalStateException: WorkManager needs to be initialized via a ContentProvider#onCreate() or an Application#onCreate().
at android.app.ActivityThread.handleCreateService(ActivityThread.java:5073)
at android.app.ActivityThread.-$$Nest$mhandleCreateService(Unknown)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2425)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8741)
at java.lang.reflect.Method.invoke(Method.java:-2)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
Caused by: java.lang.IllegalStateException: WorkManager needs to be initialized via a ContentProvider#onCreate() or an Application#onCreate().
at androidx.work.impl.background.systemjob.SystemJobService.onCreate(SystemJobService:73)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:5060)
at android.app.ActivityThread.-$$Nest$mhandleCreateService(Unknown)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2425)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8741)
at java.lang.reflect.Method.invoke(Method.java:-2)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
ha...@gmail.com <ha...@gmail.com> #23
I would check to make sure that the app is not a multi-process app. If it is, you will need your Application
to additionally sub-class Configuration.Provider
or additionally define a second provider for the other process.
ra...@google.com <ra...@google.com> #24
ey...@gmail.com <ey...@gmail.com> #25
ra...@google.com <ra...@google.com> #26
ey...@gmail.com <ey...@gmail.com> #27
Got this on the snapshot. Almost 1,000 in the past 24 hours.
Fatal Exception: java.lang.IllegalStateException: Not allowed to start service Intent { act=ACTION_STOP_FOREGROUND cmp=com.myapp/androidx.work.impl.foreground.SystemForegroundService }: app is in background uid UidRecord{c03e72d u0a293 CEM bg:+15m4s172ms idle change:cached procs:1 proclist:3231, seq(0,0,0)} at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1666) at android.app.ContextImpl.startService(ContextImpl.java:1611) at android.content.ContextWrapper.startService(ContextWrapper.java:677) at androidx.work.impl.Processor.stopForegroundService(Processor.java:303) at androidx.work.impl.Processor.stopForeground(Processor.java:221) at androidx.work.impl.WorkerWrapper.resolve(WorkerWrapper.java:453) at androidx.work.impl.WorkerWrapper.rescheduleAndResolve(WorkerWrapper.java:546) at androidx.work.impl.WorkerWrapper.onWorkFinished(WorkerWrapper.java:348) at androidx.work.impl.WorkerWrapper$2.run(WorkerWrapper.java:318) 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:764)
ra...@google.com <ra...@google.com> #28
Can you reproduce this locally at all ?
I am going to need a lot more information, to figure out what might be going on.
ey...@gmail.com <ey...@gmail.com> #29
I haven't seen it once locally. Any ideas on what I can try to do to get it to happen?
My use case is setting a recurring work that goes off every hour, and retrieves a location, and uploads it to a server. If the response is a 401 then I cancel the worker. I've tested all those scenarios multiple times.
ra...@google.com <ra...@google.com> #30
I am not exactly sure. While there is nothing wrong with your approach; rather than structure your work as a PeriodicWorkRequest
you might want to try doing something like:
Enqueue a OneTimeWorkRequest
which can do one of:
- Enqueue a copy of the same work request with an
initialDelay
of your period if you get a response that is not an HTTP401
. - No-op if you get an HTTP
401
.
That might be a much simpler way of managing this.
ey...@gmail.com <ey...@gmail.com> #31
Unfortunately we had to unpublish the app today, so I can't test anything further. But it seems like this bug is not fixed, at least in the 2.4.0-SNAPSHOT build.
ra...@google.com <ra...@google.com> #32
I made another change:
It should land soon. Unfortunately won't be in the 2.3.1 release. This is not strictly necessary from an API perspective, but anecdotally this seems to be required for some OEMs.
ey...@gmail.com <ey...@gmail.com> #33
This may also be relevant if you haven't seen it -
Basically if you start a foreground service you must call startForeground or the app will crash. If you stop the service before startForeground is called then the app will still crash.
ra...@google.com <ra...@google.com> #34
That part, is clear.
There is anecdotal evidence that a popular OEM requires you to call startForeground() as part of the first onStartCommand() callback when using ContextCompat.startForeground(...)
.
ey...@gmail.com <ey...@gmail.com> #35
I have had similar issues with that OEM in another app, even though I call startForeground
in onCreate
and in every onStartCommand
...
da...@gmail.com <da...@gmail.com> #36
This might be a stupid question...
We receive the crash in Crashlytics.
But does the user actually notice the bug? A popup saying the app crashed out of nowhere?
And does the crash affects in any way the rescheduling of the periodic work or the app functionality? How?
Bonus: is there something that us, as developers, can do in our apps to help you debug the issue? Even if it requires a snapshot version with some kind of logging + non-fatal exception sent to Crashlytics just before the startService that cause the crash, just to help you debug.
ra...@google.com <ra...@google.com> #37
- The good news is the user won't see this at all (its a background crash).
- No, it does not affect the app in any meaningful way. Because
WorkManager
has its own source of truth. Also, in this particular case your app process crashed while we were asking the foregroundService
to stop. Presumably, everything that the app wanted to do was already completed.
--
For being able to provide more information - what is really useful to us for cases like this which is hard to reproduce is WorkManager
s internal logs.
You can override the Logger
used by WorkManager
by using:
This is a @Restricted
API, however you can @Suppress
it. We might have a better API going forward, but it should be easy for you to change (if we do something i.e.)
ra...@google.com <ra...@google.com> #38
We made more changes to obviate the need for the ACTION_STOP_FOREGROUND intent itself. So you want to give this a go, you can try using:
repositories {
google()
maven { url 'https://ci.android.com/builds/submitted/6188671/androidx_snapshot/latest/repository/' }
}
dependencies {
implementation "androidx.work:work-runtime:2.4.0-SNAPSHOT"
}
ra...@google.com <ra...@google.com> #39
My apologies, it looks like the artifacts were not generated for that build. I have verified that this one works:
repositories {
google()
maven { url 'https://ci.android.com/builds/submitted/6217200/androidx_snapshot/latest/repository/' }
}
dependencies {
implementation "androidx.work:work-runtime:2.4.0-SNAPSHOT"
}
ha...@gmail.com <ha...@gmail.com> #40
Can someone confirm that it's now fixed with 2.4.0? When will this release be available in the stable channel?.
This issue here is marked as "fixed" so it should be working now?!
ra...@google.com <ra...@google.com> #41
The fix also landed in version 2.3.2
.
pa...@gmail.com <pa...@gmail.com> #42
I am using workManager v2.7.1 and I am seeing this error locally and really often when I started to use
setForegroundAsync(foregroundInfo) because of android 12 restrictions on foreground services.
Unable to stop foreground service android.app.BackgroundServiceStartNotAllowedException: Not allowed to start service Intent { act=ACTION_STOP_FOREGROUND cmp=com.xx.xx/androidx.work.impl.foreground.SystemForegroundService }: app is in background uid UidRecord{3b1d726 u0a271 LAST bg:+2m45s924ms idle change:idle|procstate procs:0 seq(2348929,2348732)} at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1908) at android.app.ContextImpl.startService(ContextImpl.java:1864) at android.content.ContextWrapper.startService(ContextWrapper.java:817) 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:475) 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:1137) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637) at java.lang.Thread.run(Thread.java:1012)
ha...@gmail.com <ha...@gmail.com> #43
he...@gmail.com <he...@gmail.com> #44
Also getting the same error from doWork expedited CoroutineWorker
method if App in background.
Unable to stop foreground service
android.app.BackgroundServiceStartNotAllowedException: Not allowed to start service Intent { act=ACTION_STOP_FOREGROUND cmp=com.example.example/androidx.work.impl.foreground.SystemForegroundService }: app is in background uid UidRecord{1248515 u0a294 TRNB bg:+1m39s883ms idle change:uncached procs:0 seq(0,0,0)}
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1862)
at android.app.ContextImpl.startService(ContextImpl.java:1818)
at android.content.ContextWrapper.startService(ContextWrapper.java:776)
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.setSucceededAndResolve(WorkerWrapper.java:600)
at androidx.work.impl.WorkerWrapper.handleResult(WorkerWrapper.java:477)
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)
Only service is setForeground
method using:
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.R) {
setForegroundAsync(createForegroundInfo())
} else setForeground(createForegroundInfo())
doWork is uploading Room data and images.
Description
Version used: 2.3.0-beta01
Devices/Android versions reproduced on: Galaxy A7 (Android 9) / Galaxy S10 (Android 10)
versions.build = [
minsdk : 23,
targetsdk : 29,
buildtools: "29.0.2",
]
---
I get a crash after start using the new foreground API in 2.3.0. I'm using `CoroutineWorker` and calling `setForeground()` in its `doWork()` with a notification which has an action created by `WorkManager.getInstance(context).createCancelPendingIntent(id)`. This should stop the job when clicked.
This seemed to work fine but after releasing, it crashes for some users with the following stacktrace (on Android 10; on Android 9 the line is 1666 instead of 1687):
```
Fatal Exception: java.lang.IllegalStateException: Not allowed to start service Intent { act=ACTION_STOP_FOREGROUND cmp=de.loewen.lcsmobile/androidx.work.impl.foreground.SystemForegroundService }: app is in background uid UidRecord{da09b33 u0a309 CEM idle change:cached procs:1 proclist:29769, seq(0,0,0)}
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1687)
at android.app.ContextImpl.startService(ContextImpl.java:1632)
at android.content.ContextWrapper.startService(ContextWrapper.java:683)
at androidx.work.impl.Processor.stopForegroundService(Processor.java:303)
at androidx.work.impl.Processor.stopForeground(Processor.java:221)
at androidx.work.impl.WorkerWrapper.resolve(WorkerWrapper.java:453)
at androidx.work.impl.WorkerWrapper.setFailedAndResolve(WorkerWrapper.java:519)
at androidx.work.impl.WorkerWrapper.handleResult(WorkerWrapper.java:485)
at androidx.work.impl.WorkerWrapper.onWorkFinished(WorkerWrapper.java:343)
at androidx.work.impl.WorkerWrapper$2.run(WorkerWrapper.java:318)
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:919)
```
I've looked into the changelog of 2.3.0-beta02 but it doesn't seem like there's already a fix for this problem.
The stacktrace doesn't contain a line of my app, only the workmanager-library so I think its crashing after the users clicks the cancel-action on the notification.