Skip to content

Shared Mutable State In Coroutine (ARCH8038)

Key kotlin.async.shared-mutable-state-in-coroutine · Severity warning · Category async

Flags a var declared on an object or companion that coroutines / threads write to without @Volatile, an atomic or a Mutex.

object Counter { var hits = 0 }
launch { Counter.hits++ }
object Counter { val hits = AtomicInteger() }
launch { Counter.hits.incrementAndGet() }

Use AtomicInteger / AtomicReference, a Mutex, or confine the state to one coroutine.