Fixed
Status Update
Comments
ra...@google.com <ra...@google.com> #2
Yigit, do you have time to fix it?
reemission of the same liveData is racy
reemission of the same liveData is racy
da...@gmail.com <da...@gmail.com> #3
yea i'll take it.
da...@gmail.com <da...@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.
da...@gmail.com <da...@gmail.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()
}
}
da...@gmail.com <da...@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)
da...@gmail.com <da...@gmail.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} )
}
ra...@google.com <ra...@google.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
da...@gmail.com <da...@gmail.com> #9
No, the Play console only contains the following crash information:
This is from a Samsung S20 running Android 10
java.lang.IllegalStateException:
at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1688)
at android.app.ContextImpl.startService (ContextImpl.java:1633)
at android.content.ContextWrapper.startService (ContextWrapper.java:683)
at androidx.work.impl.Processor.stopForegroundService (Processor.java:312)
at androidx.work.impl.Processor.stopForeground (Processor.java:222)
at androidx.work.impl.WorkerWrapper.resolve (WorkerWrapper.java:446)
at androidx.work.impl.WorkerWrapper.resetPeriodicAndResolve (WorkerWrapper.java:556)
at androidx.work.impl.WorkerWrapper.handleResult (WorkerWrapper.java:461)
at androidx.work.impl.WorkerWrapper.onWorkFinished (WorkerWrapper.java:341)
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)
This is from a Samsung S20 running Android 10
java.lang.IllegalStateException:
at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1688)
at android.app.ContextImpl.startService (ContextImpl.java:1633)
at android.content.ContextWrapper.startService (ContextWrapper.java:683)
at androidx.work.impl.Processor.stopForegroundService (Processor.java:312)
at androidx.work.impl.Processor.stopForeground (Processor.java:222)
at androidx.work.impl.WorkerWrapper.resolve (WorkerWrapper.java:446)
at androidx.work.impl.WorkerWrapper.resetPeriodicAndResolve (WorkerWrapper.java:556)
at androidx.work.impl.WorkerWrapper.handleResult (WorkerWrapper.java:461)
at androidx.work.impl.WorkerWrapper.onWorkFinished (WorkerWrapper.java:341)
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)
da...@gmail.com <da...@gmail.com> #10
Actually, I just found it in the local log on my test device:
10-27 05:17:18.804 E/AndroidRuntime(12498): java.lang.IllegalStateException: Not allowed to start service Intent { act=ACTION_STOP_FOREGROUND cmp=com.dvtonder.chronus/androidx.work.impl.foreground.SystemForegroundService }: app is in background uid UidRecord{507c6a5 u0a291 CRE bg:+6h3m58s342ms idle change:cached procs:1 seq(0,0,0)}
10-27 05:17:18.804 E/AndroidRuntime(12498): at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1715)
10-27 05:17:18.804 E/AndroidRuntime(12498): at android.app.ContextImpl.startService(ContextImpl.java:1670)
10-27 05:17:18.804 E/AndroidRuntime(12498): at android.content.ContextWrapper.startService(ContextWrapper.java:720)
10-27 05:17:18.804 E/AndroidRuntime(12498): at androidx.work.impl.Processor.stopForegroundService(SourceFile:312)
10-27 05:17:18.804 E/AndroidRuntime(12498): at androidx.work.impl.Processor.stopForeground(SourceFile:222)
10-27 05:17:18.804 E/AndroidRuntime(12498): at androidx.work.impl.WorkerWrapper.resolve(SourceFile:446)
10-27 05:17:18.804 E/AndroidRuntime(12498): at androidx.work.impl.WorkerWrapper.tryCheckForInterruptionAndResolve(SourceFile:419)
10-27 05:17:18.804 E/AndroidRuntime(12498): at androidx.work.impl.WorkerWrapper.interrupt(SourceFile:375)
10-27 05:17:18.804 E/AndroidRuntime(12498): at androidx.work.impl.Processor.interrupt(SourceFile:331)
10-27 05:17:18.804 E/AndroidRuntime(12498): at androidx.work.impl.Processor.stopWork(SourceFile:188)
10-27 05:17:18.804 E/AndroidRuntime(12498): at androidx.work.impl.utils.StopWorkRunnable.run(SourceFile:73)
10-27 05:17:18.804 E/AndroidRuntime(12498): at androidx.work.impl.utils.SerialExecutor$Task.run(SourceFile:91)
10-27 05:17:18.804 E/AndroidRuntime(12498): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
10-27 05:17:18.804 E/AndroidRuntime(12498): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
10-27 05:17:18.804 E/AndroidRuntime(12498): at java.lang.Thread.run(Thread.java:923)
--------- beginning of main
10-27 05:17:18.804 E/AndroidRuntime(12498): java.lang.IllegalStateException: Not allowed to start service Intent { act=ACTION_STOP_FOREGROUND cmp=com.dvtonder.chronus/androidx.work.impl.foreground.SystemForegroundService }: app is in background uid UidRecord{507c6a5 u0a291 CRE bg:+6h3m58s342ms idle change:cached procs:1 seq(0,0,0)}
10-27 05:17:18.804 E/AndroidRuntime(12498): at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1715)
10-27 05:17:18.804 E/AndroidRuntime(12498): at android.app.ContextImpl.startService(ContextImpl.java:1670)
10-27 05:17:18.804 E/AndroidRuntime(12498): at android.content.ContextWrapper.startService(ContextWrapper.java:720)
10-27 05:17:18.804 E/AndroidRuntime(12498): at androidx.work.impl.Processor.stopForegroundService(SourceFile:312)
10-27 05:17:18.804 E/AndroidRuntime(12498): at androidx.work.impl.Processor.stopForeground(SourceFile:222)
10-27 05:17:18.804 E/AndroidRuntime(12498): at androidx.work.impl.WorkerWrapper.resolve(SourceFile:446)
10-27 05:17:18.804 E/AndroidRuntime(12498): at androidx.work.impl.WorkerWrapper.tryCheckForInterruptionAndResolve(SourceFile:419)
10-27 05:17:18.804 E/AndroidRuntime(12498): at androidx.work.impl.WorkerWrapper.interrupt(SourceFile:375)
10-27 05:17:18.804 E/AndroidRuntime(12498): at androidx.work.impl.Processor.interrupt(SourceFile:331)
10-27 05:17:18.804 E/AndroidRuntime(12498): at androidx.work.impl.Processor.stopWork(SourceFile:188)
10-27 05:17:18.804 E/AndroidRuntime(12498): at androidx.work.impl.utils.StopWorkRunnable.run(SourceFile:73)
10-27 05:17:18.804 E/AndroidRuntime(12498): at androidx.work.impl.utils.SerialExecutor$Task.run(SourceFile:91)
10-27 05:17:18.804 E/AndroidRuntime(12498): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
10-27 05:17:18.804 E/AndroidRuntime(12498): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
10-27 05:17:18.804 E/AndroidRuntime(12498): at java.lang.Thread.run(Thread.java:923)
--------- beginning of main
ra...@google.com <ra...@google.com> #11
Thanks. That helps !
ap...@google.com <ap...@google.com> #12
Project: platform/frameworks/support
Branch: androidx-master-dev
commit 2f84e04876c0454432cb5595c73b1b1c02156331
Author: Rahul Ravikumar <rahulrav@google.com>
Date: Thu Nov 05 15:18:22 2020
Stop foreground service and ignore exceptions.
- There are bugs in the platform that causes an intent dispatch to fail.
This happens because the service and the `uid` is still in the background even
though this intent could have only been delivered _after_ startForeground(...).
Fixes: b/170924044
Test: Ran integration tests to make sure existing functionality is not broken.
Change-Id: I99dc8e3e334ccbf3a53aa475ae445560278c004a
M work/workmanager/src/main/java/androidx/work/impl/Processor.java
https://android-review.googlesource.com/1489901
Branch: androidx-master-dev
commit 2f84e04876c0454432cb5595c73b1b1c02156331
Author: Rahul Ravikumar <rahulrav@google.com>
Date: Thu Nov 05 15:18:22 2020
Stop foreground service and ignore exceptions.
- There are bugs in the platform that causes an intent dispatch to fail.
This happens because the service and the `uid` is still in the background even
though this intent could have only been delivered _after_ startForeground(...).
Fixes:
Test: Ran integration tests to make sure existing functionality is not broken.
Change-Id: I99dc8e3e334ccbf3a53aa475ae445560278c004a
M work/workmanager/src/main/java/androidx/work/impl/Processor.java
da...@gmail.com <da...@gmail.com> #13
Silly question but when can one expect the next alpha or beta of Work to be released? I would like to get the fix for this issue deployed to my users.
ra...@google.com <ra...@google.com> #14
The next beta
should have this change. We have a release planned in the first week of December.
ra...@google.com <ra...@google.com> #15
If you won't want to wait, you can always use the SNAPSHOT
version from
Description
Version used: Work Version 2.5.0-alpha03
Devices/Android versions reproduced on: Android 10 and 11
Am getting the following crash report on the Play console since updating to the alpha version yesterday.
java.lang.IllegalStateException:
at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1688)
at android.app.ContextImpl.startService (ContextImpl.java:1633)
at android.content.ContextWrapper.startService (ContextWrapper.java:683)
at androidx.work.impl.Processor.stopForegroundService (Processor.java:312)
at androidx.work.impl.Processor.stopForeground (Processor.java:222)
at androidx.work.impl.WorkerWrapper.resolve (WorkerWrapper.java:446)
at androidx.work.impl.WorkerWrapper.resetPeriodicAndResolve (WorkerWrapper.java:556)
at androidx.work.impl.WorkerWrapper.handleResult (WorkerWrapper.java:461)
at androidx.work.impl.WorkerWrapper.onWorkFinished (WorkerWrapper.java:341)
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)