Status Update
Comments
du...@google.com <du...@google.com> #2
Thanks for reporting this - would you be able to share a repro or some of your code that produced this?
yb...@google.com <yb...@google.com> #3
i have a guess based on the stack traces, specifically these 3:
arch_disk_io_0 (BLOCKED)
kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:69)
androidx.paging.LegacyPagingSource.getDataSource$paging_common(null:2)
androidx.paging.LegacyPagingSource.invalidate(LegacyPagingSource.kt:85)
androidx.paging.LegacyPagingSource$dataSource$2$1$1.invoke(LegacyPagingSource.kt:40)
androidx.paging.LegacyPagingSource$dataSource$2$1$1.invoke(LegacyPagingSource.kt:32)
androidx.paging.LegacyPagingSource$sam$i$androidx_paging_DataSource_InvalidatedCallback$0.onInvalidated(null:2)
androidx.paging.DataSource.invalidate(DataSource.kt:381)
androidx.room.paging.LimitOffsetDataSource$1.onInvalidated(LimitOffsetDataSource.java:71)
androidx.room.InvalidationTracker$WeakObserver.onInvalidated(InvalidationTracker.java:854)
androidx.room.InvalidationTracker$ObserverWrapper.notifyByTableInvalidStatus(InvalidationTracker.java:640)
main (BLOCKED)
kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:69)
androidx.paging.LegacyPagingSource.getDataSource$paging_common(null:2)
androidx.paging.LegacyPagingSource.invalidate(LegacyPagingSource.kt:85)
androidx.paging.PageFetcher.generateNewPagingSource(PageFetcher.kt:180)
androidx.paging.PageFetcher.access$generateNewPagingSource(PageFetcher.kt:38)
androidx.paging.PageFetcher$flow$1$2.invokeSuspend(PageFetcher.kt:70)
androidx.paging.PageFetcher$flow$1$2.invoke(null:18)
DefaultDispatcher-worker-5 (BLOCKED)
androidx.room.InvalidationTracker$1.run(InvalidationTracker.java:410)
androidx.room.InvalidationTracker.refreshVersionsSync(InvalidationTracker.java:461)
androidx.room.paging.LimitOffsetDataSource.isInvalid(LimitOffsetDataSource.java:101)
androidx.paging.LegacyPagingSource$dataSource$2.invoke(LegacyPagingSource.kt:44)
androidx.paging.LegacyPagingSource$dataSource$2.invoke(LegacyPagingSource.kt:32)
kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
androidx.paging.LegacyPagingSource.getDataSource$paging_common(null:2)
androidx.paging.LegacyPagingSource$load$2.invokeSuspend(LegacyPagingSource.kt:69)
While creating the data source, it gets invalidated, which calls back the invalidate callback in LegacyPagingSource, which tries to call dataSource.invalidate
and it cannot because we are locked in lazy while creating the data source. It is locked because LimitOffsetDataSource calls isInvalid with a sync check that is blocked on dispatching the invalid above.
Will try to get back to this later, might be a bit hard to reproduce :/.
yb...@google.com <yb...@google.com> #4
i can reproduce this in the github sample app where LimitOffsetDataSource calls refreshVersionsSync
on the main thread, which is a problem :/
yb...@google.com <yb...@google.com> #5
sorry to clarify, it happens only on pull to refresh.
yb...@google.com <yb...@google.com> #7
and appearantly we already knew about this:
yb...@google.com <yb...@google.com> #8
btw, until we fix this, you can work around it by adding a flowOn(some background dispatcher)
your pagingData collector which will work around this issue by always calling the factory on a background thread.
ap...@google.com <ap...@google.com> #9
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