Fixed
Status Update
Comments
du...@google.com <du...@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
yb...@google.com <yb...@google.com> #3
yea i'll take it.
yb...@google.com <yb...@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.
yb...@google.com <yb...@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()
}
}
yb...@google.com <yb...@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)
yb...@google.com <yb...@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} )
}
yb...@google.com <yb...@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
ap...@google.com <ap...@google.com> #9
Project: platform/frameworks/support
Branch: androidx-master-dev
commit cfd1258a8911348898cd07d1b02b00d14917159d
Author: Yigit Boyar <yboyar@google.com>
Date: Tue Dec 01 17:01:50 2020
Ensure legacy data source is always created on the fetcher
This CL fixes a bug where we could possibly create the legacy data
source on the main thread (more specifically, the thread where flow
collection happens).
We already tried to prevent it by making it lazy, assuming the first
call will be a loadInitial call but it is not the case for invalidations
where we end up calling dataSource.isInvalid which might create it
(lazy) and then call isInvalid. This issue became more appearant because
Room's limit offset data source implementations might run a database
query to check for invalidation, possibly causing ANR.
In this CL, I changed the paging source factory to be a suspend function
in Fetcher (it is internal class). Public API (Pager) still expects a
regular function. To workaround it, now asPagingSource returns a known
internal class (SuspendingPagingSourceFactory) which extends Function0
but also has another suspend function to create the paging source.
Pager, when initializing PageFetcher, checks the instance of the
function to unwrap it before creating the fetcher.
This WONT work if someone wraps that function but besides that, for the
common case, this should fix the issue until we can move everyone off of
Paging2 APIs, including Room.
I've also slightly changed the legacy paging source implementation not
to rely on overriding invalidate to be able to remove that method from
public API.
Bug: 173029013
Bug: 137971356
Test: LegacyPagingSourceTest
RelNote: "PagingSource.invalidate is no longer an open function. If you
need to get notified when invalidation happens, consider calling the
registerInvalidatedCallback method instead of overriding invalidate."
Change-Id: I628d9893a4ac55409aa24e49de1e2d2c35beda4f
M paging/common/api/current.txt
M paging/common/api/public_plus_experimental_current.txt
M paging/common/api/restricted_current.txt
M paging/common/src/main/kotlin/androidx/paging/DataSource.kt
M paging/common/src/main/kotlin/androidx/paging/LegacyPagingSource.kt
M paging/common/src/main/kotlin/androidx/paging/PageFetcher.kt
M paging/common/src/main/kotlin/androidx/paging/PagedList.kt
M paging/common/src/main/kotlin/androidx/paging/Pager.kt
M paging/common/src/main/kotlin/androidx/paging/PagingSource.kt
A paging/common/src/main/kotlin/androidx/paging/SuspendingPagingSourceFactory.kt
M paging/common/src/test/kotlin/androidx/paging/LegacyPagingSourceTest.kt
M paging/common/src/test/kotlin/androidx/paging/PageFetcherSnapshotTest.kt
M paging/common/src/test/kotlin/androidx/paging/PageFetcherTest.kt
M paging/common/src/test/kotlin/androidx/paging/PageKeyedDataSourceTest.kt
M paging/common/src/test/kotlin/androidx/paging/PagedListTest.kt
M paging/runtime/src/androidTest/java/androidx/paging/AsyncPagedListDifferTest.kt
M testutils/testutils-common/src/main/java/androidx/testutils/TestExecutor.kt
https://android-review.googlesource.com/1515484
Branch: androidx-master-dev
commit cfd1258a8911348898cd07d1b02b00d14917159d
Author: Yigit Boyar <yboyar@google.com>
Date: Tue Dec 01 17:01:50 2020
Ensure legacy data source is always created on the fetcher
This CL fixes a bug where we could possibly create the legacy data
source on the main thread (more specifically, the thread where flow
collection happens).
We already tried to prevent it by making it lazy, assuming the first
call will be a loadInitial call but it is not the case for invalidations
where we end up calling dataSource.isInvalid which might create it
(lazy) and then call isInvalid. This issue became more appearant because
Room's limit offset data source implementations might run a database
query to check for invalidation, possibly causing ANR.
In this CL, I changed the paging source factory to be a suspend function
in Fetcher (it is internal class). Public API (Pager) still expects a
regular function. To workaround it, now asPagingSource returns a known
internal class (SuspendingPagingSourceFactory) which extends Function0
but also has another suspend function to create the paging source.
Pager, when initializing PageFetcher, checks the instance of the
function to unwrap it before creating the fetcher.
This WONT work if someone wraps that function but besides that, for the
common case, this should fix the issue until we can move everyone off of
Paging2 APIs, including Room.
I've also slightly changed the legacy paging source implementation not
to rely on overriding invalidate to be able to remove that method from
public API.
Bug: 173029013
Bug: 137971356
Test: LegacyPagingSourceTest
RelNote: "PagingSource.invalidate is no longer an open function. If you
need to get notified when invalidation happens, consider calling the
registerInvalidatedCallback method instead of overriding invalidate."
Change-Id: I628d9893a4ac55409aa24e49de1e2d2c35beda4f
M paging/common/api/current.txt
M paging/common/api/public_plus_experimental_current.txt
M paging/common/api/restricted_current.txt
M paging/common/src/main/kotlin/androidx/paging/DataSource.kt
M paging/common/src/main/kotlin/androidx/paging/LegacyPagingSource.kt
M paging/common/src/main/kotlin/androidx/paging/PageFetcher.kt
M paging/common/src/main/kotlin/androidx/paging/PagedList.kt
M paging/common/src/main/kotlin/androidx/paging/Pager.kt
M paging/common/src/main/kotlin/androidx/paging/PagingSource.kt
A paging/common/src/main/kotlin/androidx/paging/SuspendingPagingSourceFactory.kt
M paging/common/src/test/kotlin/androidx/paging/LegacyPagingSourceTest.kt
M paging/common/src/test/kotlin/androidx/paging/PageFetcherSnapshotTest.kt
M paging/common/src/test/kotlin/androidx/paging/PageFetcherTest.kt
M paging/common/src/test/kotlin/androidx/paging/PageKeyedDataSourceTest.kt
M paging/common/src/test/kotlin/androidx/paging/PagedListTest.kt
M paging/runtime/src/androidTest/java/androidx/paging/AsyncPagedListDifferTest.kt
M testutils/testutils-common/src/main/java/androidx/testutils/TestExecutor.kt
yb...@google.com <yb...@google.com>
sa...@gmail.com <sa...@gmail.com> #10
For the ones like me, an android dev working on a real project using the Paging 3, the suggested workaround flowOn(some background dispatcher)
was the only solution that fixed my need of creating a source in a suspendable fashion.
Why not allowing the pagingSourceFactory
to be suspendable? I didn't find a prettier solution than doing:
// viewModel
pagingSourceFactory = { runBlocking { createPagingSource() } }
// fragment
viewLifecycleOwner.lifecycleScope.launchWhenStarted {
successState.results
.flowOn(Dispatchers.IO)
.collect(searchResultsAdapter::submitData)
}
Which is not readable nor straightforward. At least, opening your internal SuspendingPagingSourceFactory<Key, Value>
would help, letting me extend my source factory to be cleaner and readable.
Description
Component used: paging3 Version used: androidx.paging:paging-runtime-ktx:3.0.0-alpha09 Devices/Android versions reproduced on: Pixel 4 xl
sometimes ANR happens, see BLOCKED threads