(#) Calls to `async` or `launch` should happen inside a LaunchedEffect and not composition !!! ERROR: Calls to `async` or `launch` should happen inside a LaunchedEffect and not composition This is an error. Id : `CoroutineCreationDuringComposition` Summary : Calls to `async` or `launch` should happen inside a LaunchedEffect and not composition Severity : Error Category : Correctness Platform : Any Vendor : Jetpack Compose Identifier : androidx.compose.runtime Feedback : https://issuetracker.google.com/issues/new?component=612128 Min : Lint 8.0 and 8.1 Compiled : Lint 8.0 and 8.1 Artifact : [androidx.compose.runtime:runtime-android](androidx_compose_runtime_runtime-android.md.html) Affects : Kotlin and Java files and test sources Editing : This check runs on the fly in the IDE editor Implementation : [Source Code](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:/compose/runtime/runtime-lint/src/main/java/androidx/compose/runtime/lint/ComposableCoroutineCreationDetector.kt) Tests : [Source Code](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:/compose/runtime/runtime-lint/src/test/java/androidx/compose/runtime/lint/ComposableCoroutineCreationDetectorTest.kt) Copyright Year : 2021 Creating a coroutine with `async` or `launch` during composition is often incorrect - this means that a coroutine will be created even if the composition fails / is rolled back, and it also means that multiple coroutines could end up mutating the same state, causing inconsistent results. Instead, use `LaunchedEffect` and create coroutines inside the suspending block. The block will only run after a successful composition, and will cancel existing coroutines when `key` changes, allowing correct cleanup. (##) Example Here is an example of lint warnings produced by this check: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text src/androidx/compose/runtime/foo/test.kt:10:Error: Calls to async should happen inside a LaunchedEffect and not composition [CoroutineCreationDuringComposition] CoroutineScope.async {} ----- src/androidx/compose/runtime/foo/test.kt:11:Error: Calls to launch should happen inside a LaunchedEffect and not composition [CoroutineCreationDuringComposition] CoroutineScope.launch {} ------ src/androidx/compose/runtime/foo/test.kt:12:Error: Calls to launchIn should happen inside a LaunchedEffect and not composition [CoroutineCreationDuringComposition] flowOf(Unit).launchIn(CoroutineScope) -------- src/androidx/compose/runtime/foo/test.kt:16:Error: Calls to async should happen inside a LaunchedEffect and not composition [CoroutineCreationDuringComposition] CoroutineScope.async {} ----- src/androidx/compose/runtime/foo/test.kt:17:Error: Calls to launch should happen inside a LaunchedEffect and not composition [CoroutineCreationDuringComposition] CoroutineScope.launch {} ------ src/androidx/compose/runtime/foo/test.kt:18:Error: Calls to launchIn should happen inside a LaunchedEffect and not composition [CoroutineCreationDuringComposition] flowOf(Unit).launchIn(CoroutineScope) -------- src/androidx/compose/runtime/foo/test.kt:22:Error: Calls to async should happen inside a LaunchedEffect and not composition [CoroutineCreationDuringComposition] CoroutineScope.async {} ----- src/androidx/compose/runtime/foo/test.kt:23:Error: Calls to launch should happen inside a LaunchedEffect and not composition [CoroutineCreationDuringComposition] CoroutineScope.launch {} ------ src/androidx/compose/runtime/foo/test.kt:24:Error: Calls to launchIn should happen inside a LaunchedEffect and not composition [CoroutineCreationDuringComposition] flowOf(Unit).launchIn(CoroutineScope) -------- src/androidx/compose/runtime/foo/test.kt:33:Error: Calls to async should happen inside a LaunchedEffect and not composition [CoroutineCreationDuringComposition] CoroutineScope.async {} ----- src/androidx/compose/runtime/foo/test.kt:34:Error: Calls to launch should happen inside a LaunchedEffect and not composition [CoroutineCreationDuringComposition] CoroutineScope.launch {} ------ src/androidx/compose/runtime/foo/test.kt:35:Error: Calls to launchIn should happen inside a LaunchedEffect and not composition [CoroutineCreationDuringComposition] flowOf(Unit).launchIn(CoroutineScope) -------- src/androidx/compose/runtime/foo/test.kt:38:Error: Calls to async should happen inside a LaunchedEffect and not composition [CoroutineCreationDuringComposition] CoroutineScope.async {} ----- src/androidx/compose/runtime/foo/test.kt:39:Error: Calls to launch should happen inside a LaunchedEffect and not composition [CoroutineCreationDuringComposition] CoroutineScope.launch {} ------ src/androidx/compose/runtime/foo/test.kt:40:Error: Calls to launchIn should happen inside a LaunchedEffect and not composition [CoroutineCreationDuringComposition] flowOf(Unit).launchIn(CoroutineScope) -------- src/androidx/compose/runtime/foo/test.kt:46:Error: Calls to async should happen inside a LaunchedEffect and not composition [CoroutineCreationDuringComposition] CoroutineScope.async {} ----- src/androidx/compose/runtime/foo/test.kt:47:Error: Calls to launch should happen inside a LaunchedEffect and not composition [CoroutineCreationDuringComposition] CoroutineScope.launch {} ------ src/androidx/compose/runtime/foo/test.kt:48:Error: Calls to launchIn should happen inside a LaunchedEffect and not composition [CoroutineCreationDuringComposition] flowOf(Unit).launchIn(CoroutineScope) -------- src/androidx/compose/runtime/foo/test.kt:52:Error: Calls to async should happen inside a LaunchedEffect and not composition [CoroutineCreationDuringComposition] CoroutineScope.async {} ----- src/androidx/compose/runtime/foo/test.kt:53:Error: Calls to launch should happen inside a LaunchedEffect and not composition [CoroutineCreationDuringComposition] CoroutineScope.launch {} ------ src/androidx/compose/runtime/foo/test.kt:54:Error: Calls to launchIn should happen inside a LaunchedEffect and not composition [CoroutineCreationDuringComposition] flowOf(Unit).launchIn(CoroutineScope) -------- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Here is the source file referenced above: `src/androidx/compose/runtime/foo/test.kt`: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers package androidx.compose.runtime.foo import androidx.compose.runtime.Composable import kotlinx.coroutines.* import kotlinx.coroutines.flow.* @Composable fun Test() { CoroutineScope.async {} CoroutineScope.launch {} flowOf(Unit).launchIn(CoroutineScope) } val lambda = @Composable { CoroutineScope.async {} CoroutineScope.launch {} flowOf(Unit).launchIn(CoroutineScope) } val lambda2: @Composable () -> Unit = { CoroutineScope.async {} CoroutineScope.launch {} flowOf(Unit).launchIn(CoroutineScope) } @Composable fun LambdaParameter(content: @Composable () -> Unit) {} @Composable fun Test2() { LambdaParameter(content = { CoroutineScope.async {} CoroutineScope.launch {} flowOf(Unit).launchIn(CoroutineScope) }) LambdaParameter { CoroutineScope.async {} CoroutineScope.launch {} flowOf(Unit).launchIn(CoroutineScope) } } fun test3() { val localLambda1 = @Composable { CoroutineScope.async {} CoroutineScope.launch {} flowOf(Unit).launchIn(CoroutineScope) } val localLambda2: @Composable () -> Unit = { CoroutineScope.async {} CoroutineScope.launch {} flowOf(Unit).launchIn(CoroutineScope) } } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You can also visit the [source code](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:/compose/runtime/runtime-lint/src/test/java/androidx/compose/runtime/lint/ComposableCoroutineCreationDetectorTest.kt) for the unit tests for this check to see additional scenarios. The above example was automatically extracted from the first unit test found for this lint check, `ComposableCoroutineCreationDetector.errors`. To report a problem with this extracted sample, visit https://issuetracker.google.com/issues/new?component=612128. (##) Including !!! This is not a built-in check. To include it, add the below dependency to your project. ``` // build.gradle.kts implementation("androidx.compose.runtime:runtime-android:1.7.0-rc01") // build.gradle implementation 'androidx.compose.runtime:runtime-android:1.7.0-rc01' // build.gradle.kts with version catalogs: implementation(libs.runtime.android) # libs.versions.toml [versions] runtime-android = "1.7.0-rc01" [libraries] runtime-android = { module = "androidx.compose.runtime:runtime-android", version.ref = "runtime-android" } ``` 1.7.0-rc01 is the version this documentation was generated from; there may be newer versions available. [Additional details about androidx.compose.runtime:runtime-android](androidx_compose_runtime_runtime-android.md.html). (##) Suppressing You can suppress false positives using one of the following mechanisms: * Using a suppression annotation like this on the enclosing element: ```kt // Kotlin @Suppress("CoroutineCreationDuringComposition") fun method() { async(...) } ``` or ```java // Java @SuppressWarnings("CoroutineCreationDuringComposition") void method() { async(...); } ``` * Using a suppression comment like this on the line above: ```kt //noinspection CoroutineCreationDuringComposition problematicStatement() ``` * Using a special `lint.xml` file in the source tree which turns off the check in that folder and any sub folder. A simple file might look like this: ```xml <?xml version="1.0" encoding="UTF-8"?> <lint> <issue id="CoroutineCreationDuringComposition" severity="ignore" /> </lint> ``` Instead of `ignore` you can also change the severity here, for example from `error` to `warning`. You can find additional documentation on how to filter issues by path, regular expression and so on [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html). * In Gradle projects, using the DSL syntax to configure lint. For example, you can use something like ```gradle lintOptions { disable 'CoroutineCreationDuringComposition' } ``` In Android projects this should be nested inside an `android { }` block. * For manual invocations of `lint`, using the `--ignore` flag: ``` $ lint --ignore CoroutineCreationDuringComposition ...` ``` * Last, but not least, using baselines, as discussed [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).