-
Notifications
You must be signed in to change notification settings - Fork 659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Coroutine Launched Outside Side Effect in rememberAsyncImagePainter
results in Unsupported concurrent change during composition
crash
#1764
Comments
Thanks for the report! I'll take a closer look, however I don't think we can use |
What about this problem ? I was debugging my app to identify what causing Unsupported concurrent change during composition and come to conclusion that AsyncImage is the problem, it happens when i'm navigating back to some screen with asyncImg in it. Is there any workaround for that? i'm not sure i can use coil when my app goes to prod( |
TLDR; when I call a NavHostController's navigate() function right after the AsyncImage is rendered, I get this error. I am also experiencing the same issue. I have a list composable which contains a LazyColumn, each row in the column has an AsyncImage and clicking on the row navigates to another screen. At the top of the list composable I call a loadPhotoUrl function which calls an API, when the url is returned the AsyncImage composable is rendered. If I quickly click on a row, navigate() is called and I get the Unsupported Concurrent Change error, if I wait half a second, I never get the error. If this always happens within a half second of the API call returning this behavior will seem very unpredictable for users. Noticed the error on 2.3.0, updated to 2.4.0 and am still getting the same behavior. |
Any updates on this? Our app encountered these issues on version 2.3.0, and we're still experiencing them on version 2.6.0. |
I can confirm that this is still present in the latest 3.0.0-alpha07 |
@colinrtwhite hello. The demo provided in my issue will definitely crash. |
I can confirm that updating from See comment: #2352 (comment) |
@colinrtwhite Is there any way this fix can be included in Coil 2.x? |
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: