Fixed
Status Update
Comments
ya...@gmail.com <ya...@gmail.com> #2
Yigit, do you have time to fix it?
reemission of the same liveData is racy
reemission of the same liveData is racy
su...@google.com <su...@google.com>
ya...@gmail.com <ya...@gmail.com> #4
Thanks for the detailed analysis. This may not be an issue anymore since we've started using Main.immediate there but I' not sure; I'll try to create a test case.
su...@google.com <su...@google.com> #5
just emitting same live data reproduces the issue.
@Test
fun raceTest() {
val subLiveData = MutableLiveData(1)
val subject = liveData(testScope.coroutineContext) {
emitSource(subLiveData)
emitSource(subLiveData) //crashes
}
subject.addObserver().apply {
testScope.advanceUntilIdle()
}
}
@Test
fun raceTest() {
val subLiveData = MutableLiveData(1)
val subject = liveData(testScope.coroutineContext) {
emitSource(subLiveData)
emitSource(subLiveData) //crashes
}
subject.addObserver().apply {
testScope.advanceUntilIdle()
}
}
ya...@gmail.com <ya...@gmail.com> #6
With 2.2.0-alpha04 (that use Main.immediate), the issue seems to be still there (I tested it by calling emitSource() twice, like your test case)
su...@google.com <su...@google.com> #7
yea sorry immediate does not fix it.
I actually have a WIP fix for it:
https://android-review.googlesource.com/c/platform/frameworks/support/+/1112186
if your case is the one i found (emitting same LiveData multiple times, as shown in #5) you can work around it by adding a dummy transformation.
val subLiveData = MutableLiveData(1)
val subject = liveData(testScope.coroutineContext) {
emitSource(subLiveData.map {it })
emitSource(subLiveData.map {it} )
}
I actually have a WIP fix for it:
if your case is the one i found (emitting same LiveData multiple times, as shown in #5) you can work around it by adding a dummy transformation.
val subLiveData = MutableLiveData(1)
val subject = liveData(testScope.coroutineContext) {
emitSource(subLiveData.map {it })
emitSource(subLiveData.map {it} )
}
ya...@gmail.com <ya...@gmail.com> #8
Project: platform/frameworks/support
Branch: androidx-master-dev
commit af12e75e6b4110f48e44ca121466943909de8f06
Author: Yigit Boyar <yboyar@google.com>
Date: Tue Sep 03 12:58:11 2019
Fix coroutine livedata race condition
This CL fixes a bug in liveData builder where emitting same
LiveData source twice would make it crash because the second
emission registry could possibly happen before first one is
removed as source.
We fix it by using a suspending dispose function. It does feel
a bit hacky but we cannot make DisposableHandle.dispose async
and we do not want to block there. This does not mean that there
is a problem if developer disposes it manually since our emit
functions take care of making sure it disposes (and there is
no other way to add source to the underlying MediatorLiveData)
Bug: 140249349
Test: BuildLiveDataTest#raceTest_*
Change-Id: I0b464c242a583da4669af195cf2504e2adc4de40
M lifecycle/lifecycle-livedata-ktx/api/2.2.0-alpha05.txt
M lifecycle/lifecycle-livedata-ktx/api/current.txt
M lifecycle/lifecycle-livedata-ktx/api/public_plus_experimental_2.2.0-alpha05.txt
M lifecycle/lifecycle-livedata-ktx/api/public_plus_experimental_current.txt
M lifecycle/lifecycle-livedata-ktx/api/restricted_2.2.0-alpha05.txt
M lifecycle/lifecycle-livedata-ktx/api/restricted_current.txt
M lifecycle/lifecycle-livedata-ktx/src/main/java/androidx/lifecycle/CoroutineLiveData.kt
M lifecycle/lifecycle-livedata-ktx/src/test/java/androidx/lifecycle/BuildLiveDataTest.kt
https://android-review.googlesource.com/1112186
https://goto.google.com/android-sha1/af12e75e6b4110f48e44ca121466943909de8f06
Branch: androidx-master-dev
commit af12e75e6b4110f48e44ca121466943909de8f06
Author: Yigit Boyar <yboyar@google.com>
Date: Tue Sep 03 12:58:11 2019
Fix coroutine livedata race condition
This CL fixes a bug in liveData builder where emitting same
LiveData source twice would make it crash because the second
emission registry could possibly happen before first one is
removed as source.
We fix it by using a suspending dispose function. It does feel
a bit hacky but we cannot make DisposableHandle.dispose async
and we do not want to block there. This does not mean that there
is a problem if developer disposes it manually since our emit
functions take care of making sure it disposes (and there is
no other way to add source to the underlying MediatorLiveData)
Bug: 140249349
Test: BuildLiveDataTest#raceTest_*
Change-Id: I0b464c242a583da4669af195cf2504e2adc4de40
M lifecycle/lifecycle-livedata-ktx/api/2.2.0-alpha05.txt
M lifecycle/lifecycle-livedata-ktx/api/current.txt
M lifecycle/lifecycle-livedata-ktx/api/public_plus_experimental_2.2.0-alpha05.txt
M lifecycle/lifecycle-livedata-ktx/api/public_plus_experimental_current.txt
M lifecycle/lifecycle-livedata-ktx/api/restricted_2.2.0-alpha05.txt
M lifecycle/lifecycle-livedata-ktx/api/restricted_current.txt
M lifecycle/lifecycle-livedata-ktx/src/main/java/androidx/lifecycle/CoroutineLiveData.kt
M lifecycle/lifecycle-livedata-ktx/src/test/java/androidx/lifecycle/BuildLiveDataTest.kt
su...@google.com <su...@google.com> #9
Thanks, we'll investigate this internally.
ya...@gmail.com <ya...@gmail.com> #10
Hello, wondering whether the fix will be released in another one, or two days. As, users start to complain and leaving our app, due to increased crash rate :(
Thanks.
Thanks.
su...@google.com <su...@google.com> #11
Hi, we're still aiming to get this out this week.
zh...@kuaishou.com <zh...@kuaishou.com> #12
still have bug:
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,data.info.name , data.token, app, ActivityManager.getService())
"
handleCreateService--->. Application app = packageInfo.makeApplication(false, mInstrumentation); ---> forceDefaultAppClass is always false-->if have custom Application,will make custom Application ---> service.attach(context, this,data.info.name , data.token, app, ActivityManager.getService()); ---> so getApplication() in service get custom application
--> if have customApplication, the code : "Application.class.equals(getApplication().getClass()" in Service will be false , even it's in backup mode
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
si...@gmail.com <si...@gmail.com> #13
i'm also having the same crash, here is the relevant stacktrace:
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()
}
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()
}
mr...@gmail.com <mr...@gmail.com> #14
Same Error on production when call WorkManager.getInstance(context):
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)
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)
bh...@gmail.com <bh...@gmail.com> #15
Hi,
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)
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)
pt...@gmail.com <pt...@gmail.com> #16
i have the same issue
ah...@safarifoneict.com <ah...@safarifoneict.com> #17
Comment has been deleted.
ds...@servicetitan.com <ds...@servicetitan.com> #18
Still seeing this issue in 2.5.0 using default WorkManager.getInstance() initialization. How can we prevent this situation from occurring?
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().");
}
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().");
}
li...@gmail.com <li...@gmail.com> #19
this issue on workManager 2.5.0 when initialize WorkManager in background thread, bring it back to UI thread
he...@tokopedia.com <he...@tokopedia.com> #20
I begin to see this issue after using "on demand Initialization"
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
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
d4...@gmail.com <d4...@gmail.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)
ps...@pinterest.com <ps...@pinterest.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)
ra...@google.com <ra...@google.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.
ka...@walmart.com <ka...@walmart.com> #24
Hi all, I am also facing the same crash in Work Manager 2.7.1 with on-demand initialization in Samsung X-Cover Pro(31), sdk 32. There's no concrete scenario for this crash to happen, it happens randomly at any screen. Plus the auto-backup is disabled in my app which was listed as one of the factors responsible for the crash. Can someone please assist me with a way to fix/handle this crash?
yk...@gmail.com <yk...@gmail.com> #25
Hi all , i use workManager 2.7.0 face some crash "IllegalStateExceptionWorkManager needs to be initialized via a ContentProvider#onCreate() or an Application#onCreate()" . i am sure app manifest allowBackUp=false and initWorkManger in every process. Can someone please assist me with a way to fix/handle this crash?
a....@gmail.com <a....@gmail.com> #26
I'm experience the same problem. I used suspend function to init WorkManager and used Dispatchers.Default as a scope to launch. I'm going to use runBlocking instead
Description
Version used: 2.1.0-beta01
Devices/Android versions reproduced on: Android 7.1, Android 8.1, Android 9 / W_P200 (W_P200), Xperia Z5 Compact (E5823), ZenFone, Xperia Z5 Compact, V30, FP2, ...
I'm not able to reproduce but I can see a significant number of crash at production live
java.lang.RuntimeException:
at android.app.ActivityThread.handleCreateService (ActivityThread.java:3265)
at android.app.ActivityThread.-wrap5 (ActivityThread.java)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1598)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:241)
at android.app.ActivityThread.main (ActivityThread.java:6274)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:776)
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:3255)
at android.app.ActivityThread.-wrap5 (ActivityThread.java)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1598)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:241)
at android.app.ActivityThread.main (ActivityThread.java:6274)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:776)
My detailed finding is as follow. So far, I have no idea how it happens. I cross post at
We are using the following WorkManager library
def work_version = "2.1.0-beta01"
implementation "androidx.work:work-runtime:$work_version"
Our Application class is being modified as follow so that it will work well with WorkManager
public class WeNoteApplication extends MultiDexApplication implements DefaultLifecycleObserver, Configuration.Provider {
private static WeNoteApplication me;
@Override
public void onCreate() {
super.onCreate();
me = this;
...
}
public static WeNoteApplication instance() {
return me;
}
@NonNull
@Override
public Configuration getWorkManagerConfiguration() {
return new Configuration.Builder()
.build();
}
}
We do not add any WorkManager related code in AndroidManifest.xml. We solely rely on Default initialization
This is how we construct WorkManager
public static WorkManager getWorkManager() {
WeNoteApplication weNoteApplication = WeNoteApplication.instance();
return WorkManager.getInstance(weNoteApplication);
}
The above function is called in 4 different places
Activity
Fragment
BroadcastReceiver
IntentService
Still, we receive the following crash log in production. It happens in Android 7.1, Android 8.1, Android 9 (so far)
java.lang.RuntimeException:
at android.app.ActivityThread.handleCreateService (ActivityThread.java:3265)
at android.app.ActivityThread.-wrap5 (ActivityThread.java)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1598)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:241)
at android.app.ActivityThread.main (ActivityThread.java:6274)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:776)
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:3255)
at android.app.ActivityThread.-wrap5 (ActivityThread.java)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1598)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:241)
at android.app.ActivityThread.main (ActivityThread.java:6274)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:776)
As you can see in the crash, the crash origins from WorkManager internally, and not from our app.
By looking at
WorkManagerImpl
/**
* Retrieves the singleton instance of {@link WorkManagerImpl}.
*
* @param context A context for on-demand initialization.
* @return The singleton instance of {@link WorkManagerImpl}
* @hide
*/
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public static @NonNull WorkManagerImpl getInstance(@NonNull Context context) {
synchronized (sLock) {
WorkManagerImpl instance = getInstance();
if (instance == null) {
Context appContext = context.getApplicationContext();
if (appContext instanceof Configuration.Provider) {
initialize(
appContext,
((Configuration.Provider) appContext).getWorkManagerConfiguration());
instance = getInstance(appContext);
} else {
throw new IllegalStateException("WorkManager is not initialized properly. You "
+ "have explicitly disabled WorkManagerInitializer in your manifest, "
+ "have not manually called WorkManager#initialize at this point, and "
+ "your Application does not implement Configuration.Provider.");
}
There are 2 possibilities of the crash
appContext is null. (How can it be null?)
appContext does not implement Configuration.Provider (But we already implement in our WeNoteApplication?)
To figure out how context is being passed to WorkManagerImpl from SystemJobService, we look at the code of androidx.work.impl.background.systemjob.SystemJobService.onCreate
SystemJobService
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
@RequiresApi(WorkManagerImpl.MIN_JOB_SCHEDULER_API_LEVEL)
public class SystemJobService extends JobService implements ExecutionListener {
private static final String TAG = Logger.tagWithPrefix("SystemJobService");
private WorkManagerImpl mWorkManagerImpl;
private final Map<String, JobParameters> mJobParameters = new HashMap<>();
@Override
public void onCreate() {
super.onCreate();
mWorkManagerImpl = WorkManagerImpl.getInstance(getApplicationContext());
Look fine at first sight. But, why WorkManagerImpl cannot obtain Configuration.Provider although I already implement Configuration.Provider in my Application? Is it because I'm using MultiDexApplication?