Kotlin / kotlinx-atomicfu

The idiomatic way to use atomic operations in Kotlin
Other
851 stars 56 forks source link

JVM transformation: Unsupported branching/control within atomic operation #180

Open DRSchlaubi opened 3 years ago

DRSchlaubi commented 3 years ago
val atomicBool = atomic(false)

fun main() {
    on {
        atomicBool.update { true }
    }
}

fun on(block: suspend () -> Unit) {
    GlobalScope.launch {
        block()
    }
}

suspend fun on(block: suspend () -> Unit) {
    block()
}

It only happens if you actually call block, and block is suspending (both variants of the on function cause it)

Also, only AtomicBoolean seems to be affected I couldn't reproduce it with any other type

Using it like this does not bring up the error

suspend fun main() {
    on {
        setTrue()
    }
}

fun setTrue() {
    atomicBool.update { true }
}

suspend fun on(block: suspend () -> Unit) {
    block()
}

Exact error

AtomicFuTest.kt:21: AtomicFuTestKt$main$2::invokeSuspend: Unsupported branching/control within atomic operation

Maybe related to #39, #123

SokolovaMaria commented 3 years ago

Thanks for the report! This is a bug reproduced specifically on AtomicBoolean.update [getAndUpdate, updateAndGet] invocations. Will be fixed by the next release. Your workaround with wrapping this invocation works for now.

fun setTrue() {
    atomicBool.update { true }
}