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
db...@digitalfleet.com <db...@digitalfleet.com> #3
yea i'll take it.
ra...@google.com <ra...@google.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.
db...@digitalfleet.com <db...@digitalfleet.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()
}
}
ra...@google.com <ra...@google.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)
ra...@google.com <ra...@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} )
}
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
Description
Version used: 2.3.4
Devices/Android versions reproduced on: Android Studio
On the latest version getting the following lint error this issue does not occur on 2.3.3. All of my periodic jobs intervals are at least 30 minutes.
lint-results-debug.xml
```
<issue
id="LintError"
severity="Error"
message="Unexpected failure during lint analysis of MyWorkManagerWrapper.java (this is a bug in lint or one of the libraries it depends on)

Message: null cannot be cast to non-null type kotlin.Long

The crash seems to involve the detector `androidx.work.lint.InvalidPeriodicWorkRequestIntervalDetector`.
You can try disabling it with something like this:
 android {
 lintOptions {
 disable "InvalidPeriodicWorkRequestInterval"
 }
 }

Stack: `TypeCastException:InvalidPeriodicWorkRequestIntervalDetector.visitConstructor(InvalidPeriodicWorkRequestIntervalDetector.kt:70)←UElementVisitor$DelegatingPsiVisitor.visitNewExpression(UElementVisitor.kt:1099)←UElementVisitor$DelegatingPsiVisitor.visitCallExpression(UElementVisitor.kt:1063)←UCallExpression$DefaultImpls.accept(UCallExpression.kt:99)←UCallExpressionEx$DefaultImpls.accept(UCallExpression.kt:-1)←JavaConstructorUCallExpression.accept(javaUCallExpressions.kt:123)←UQualifiedReferenceExpression$DefaultImpls.accept(UQualifiedReferenceExpression.kt:47)←JavaUCompositeQualifiedExpression.accept(JavaUCompositeQualifiedExpression.kt:23)`

You can set environment variable `LINT_PRINT_STACKTRACE=true` to dump a full stacktrace to stdout."
category="Lint"
priority="10"
summary="Lint Failure"
explanation="This issue type represents a problem running lint itself. Examples include failure to find bytecode for source files (which means certain detectors could not be run), parsing errors in lint configuration files, etc.

These errors are not errors in your own code, but they are shown to make it clear that some checks were not completed.">
<location
file="FILE_PATH/MyWorkManagerWrapper.java"/>
</issue>
```
Sample Code:
```
workRequest = new PeriodicWorkRequest.Builder(CustomWorker.class, workerType.interval, workerType.timeUnit).build();
workManager.enqueueUniquePeriodicWork(workerType.uniqueName, workerType.workPolicy, workRequest);
```
```
public enum PeriodicWorkerType {
PERIODIC_JOB("PERIODIC_JOB_V1", 30, TimeUnit.Minutes, ExistingPeriodicWorkPolicy.KEEP),
public final String uniqueName;
public final long interval;
public final TimeUnit timeUnit;
public final ExistingPeriodicWorkPolicy workPolicy;
PeriodicWorkerType(String uniqueName, long interval, TimeUnit timeUnit, ExistingPeriodicWorkPolicy workPolicy) {
this.uniqueName = uniqueName;
this.interval = interval;
this.timeUnit = timeUnit;
this.workPolicy = workPolicy;
}
}
```
If this is a bug in the library, we would appreciate if you could attach:
- Sample project to trigger the issue.
- A screenrecord or screenshots showing the issue (if UI related).