Status Update
Comments
ap...@google.com <ap...@google.com> #2
reemission of the same liveData is racy
pv...@gmail.com <pv...@gmail.com> #4
jb...@google.com <jb...@google.com> #5
@Test
fun raceTest() {
val subLiveData = MutableLiveData(1)
val subject = liveData(testScope.coroutineContext) {
emitSource(subLiveData)
emitSource(subLiveData) //crashes
}
subject.addObserver().apply {
testScope.advanceUntilIdle()
}
}
wi...@gmail.com <wi...@gmail.com> #6
[Deleted User] <[Deleted User]> #7
va...@gmail.com <va...@gmail.com> #8
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
an...@gmail.com <an...@gmail.com> #9
Got this error with activity 1.8.2 if registering like in the documentation:
val getContent = registerForActivityResult(GetContent()) { uri: Uri? ->
// Handle the returned Uri
}
override fun onCreate(savedInstanceState: Bundle?) {
// ...
val selectButton = findViewById<Button>(R.id.select_button)
selectButton.setOnClickListener {
// Pass in the mime type you want to let the user select
// as the input
getContent.launch("image/*")
}
}
Every time I returned to the first activity, if I tried to launch the "getContent" again the app would crash. The only way I was able to get it to work was to registerForActivityResult in onCreate() like this:
lateinit var getContent: <ActivityResultLauncher<Contract...>>
override fun onCreate(savedInstanceState: Bundle?) {
// ...
getContent = registerForActivityResult(GetContent()) { uri: Uri? ->
// Handle the returned Uri
}
val selectButton = findViewById<Button>(R.id.select_button)
selectButton.setOnClickListener {
// Pass in the mime type you want to let the user select
// as the input
getContent.launch("image/*")
}
}
lu...@gmail.com <lu...@gmail.com> #11
We're currently hitting this issue in our WIP app.
We use our own wrapper around registerForActivityResult called PermissionsGatedCallback. We are able to hit the crash with the following:
- Go to library
- Pick any album
- Pick any song
- Back gesture (onto album fragment)
- Back gesture (onto library fragment)
- retry:
-
- Pick any album
-
- Back gesture (onto album fragment)
-
- Go to Artists (by swiping or navbar, doesn't matter)
-
- Pick any artist
-
- Back gesture (onto library fragment)
- goto retry
I know the repro is rather tedious, but it's hard to repro even for us.
If done enough times you'll see java.lang.IllegalStateException: Attempting to launch an unregistered ActivityResultLauncher with contract androidx.activity.result.contract.ActivityResultContracts$RequestMultiplePermissions@d8b0d98 and input [Ljava.lang.String;@de949f1. You must ensure the ActivityResultLauncher is registered before calling launch().
Calling registerForActivityResult each time solves the issue as pointed earlier, as wrong as that can be. Also as crazy as that sounds making the contract launcher lazy (with re-init in case of null) and setting it to null on the lifecycle destroy event solves the issue as a whole.
To me that sounds possible only if somehow .launch is called when the fragment is destroyed, but the actual contract is still tied to the old fragment. Which sounds a lot scarier than the actual crash.
il...@google.com <il...@google.com> #12
Re
Description
Currently, when using the
ActivityResultRegistry
, you can calllaunch()
on anActivityResultLauncher
that is not currently registered. This means that the targeted Activity can be launched, but there will not be any result meaning the API is not behaving as intended.Instead of allowing developers to run into this issue, we should always throw from the registry when the launcher is not registered.