Kotlinx.coroutines: Custom coroutine functions do not allow suspension functions inside the body

Created on 28 Dec 2017  路  9Comments  路  Source: Kotlin/kotlinx.coroutines

I have written a coroutine function similar to Anko bg:
inline fun bg(crossinline block: () -> T): Deferred = async(POOL) {
block()
}
which when used does not allow the following:
bg {
delay(100)
}
This gives a compile error "Suspension functions can be called only within coroutine body" when using Android Studio 3 and Kotlin 1.2.10.

Most helpful comment

All 9 comments

I wrote a similar issue in the Anko repo: https://github.com/Kotlin/anko/issues/533 since I don't know really where this issue should belong.

In order to allow suspension, the corresponding type of the block shall have a suspend modifier. Unfortunately, it not currently compatible with crossinline.

@elizarov ouch! I got the point. because bg calls async, as bg is inline, crossinline used on block to let nested function calls work.
I think bg is not implemented the way developers expect to use(and want to use).
There is a milestone to crossinline work with suspend modifier?

@elizarov According to KT-19159 this was fixed a while ago, yet I am seeing the same error in kotlin 1.2.51 when trying to compile a block of the form,

suspendCancellableCoroutine { cont ->
    requestProcessor.process(...)
    cont.completeResume(...)
}

where process is suspendable, i.e., suspend fun process(...).

That is a separate problem. The block inside suspendCancellableCoroutine is not suspendable. You cannot do any suspending call from inside of this block by design.

@elizarov Thanks and apologies; could you please point me to the relevant documentation. Since suspendCancellableCoroutine is a prerequisite for withTimeout, it seems somewhat limited since effectively one can't use existing suspend fun in conjunction with withTimeout.

@srosenberg I suggest to start reading with the section on cancellation in the guide: https://github.com/Kotlin/kotlinx.coroutines/blob/master/coroutines-guide.md#cancellation-and-timeouts

@elizarov a bit unrelated question as I am struggling with a similar situation. I couldn't find legit resources which can explain the connection between withTimeout and recommendation of using suspendCancellableCoroutine. Can you share any insights or direct me to a resource? I was using suspendCoroutine but don't see a difference in manual testing but maybe in real working, it might be the reason for the concurrency problems.

While investigating, I found out this thread and also one of your answer on stackoverflow which intrigued me to understand this topic.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

elizarov picture elizarov  路  35Comments

LouisCAD picture LouisCAD  路  64Comments

SUPERCILEX picture SUPERCILEX  路  40Comments

elizarov picture elizarov  路  62Comments

serebit picture serebit  路  37Comments