Coroutine Launched Outside Side Effect in rememberAsyncImagePainter
results in Unsupported concurrent change during composition
crash
#1764
Labels
help wanted
Issues that are up for grabs + are good candidates for community PRs
Cause: Coroutine Launched Outside Side Effect in
rememberAsyncImagePainter
Description:
I have identified a bug in the Coil Compose library that appears to originate from the
rememberAsyncImagePainter
composable function. The bug arises when aonRemembered
function is invoked inside therememberAsyncImagePainter
function.onRemembered
is a non-composable function that establishes a new coroutine scope and launches a coroutine. The issue appears to be that the coroutine is being launched outside of a side effect block within a composable function, which is not a good practice in the Jetpack Compose framework.The main reason why launching coroutines outside a side effect in a composable function is problematic is due to the life-cycle of the composable function. Jetpack Compose recomposes (reruns) composable functions for various reasons, such as when the data they're displaying changes. This means that a coroutine could be launched multiple times if it's not managed within a side effect block such as
LaunchedEffect
,DisposableEffect
, orproduceState
.Launching a coroutine outside a side effect block can also result in memory leaks as it's not tied to the lifecycle of the composable function. Side effects ensure that when the composable leaves the composition, any resources (like the coroutine) are cleaned up.
This could lead to unexpected behavior, as the scope of the coroutine isn't necessarily tied to the lifecycle of the composable function, which can result in unexpected outcomes such as memory leaks or even application crashes.
To Reproduce
The minimum reproducible sample can be found here: https://github.com/android/nowinandroid/tree/reproduce_crash
Launch the
RotateTest
instrumentation test. It will rotate the device back and forth eventually causing the crash. It's not deterministic and might take a few iterations.Logs/Screenshots
Version
Coil version 2.3.0
Tested on: Pixel 6 API 32, Pixel 7 API 33, Samsung Fold 3 API 31
The text was updated successfully, but these errors were encountered: