https://kotlinlang.org logo
#coroutines
Title
# coroutines
f

fitermay

07/27/2022, 8:30 PM
What's the pattern to dispose of a resource when a suspend function needs to allocated a disposable resource. How do I handle the case where the cancellation happens after the resource is allocated but right before it's returned?
j

Joffrey

07/27/2022, 9:25 PM
Cancellation is cooperative in coroutines, it cannot happen in every possible instant. The places where it can happen are predictable. But in any case, cancellation can be handled like any other exception. If you have a resource that needs cleanup, use a
try/finally
block to clean it up in every scenario (normal completion and exceptions)
f

fitermay

07/27/2022, 9:53 PM
It cannot happen when returning from one suspend function to another?
Internally, can't the return happen via continuation so the invoking coroutine can be cancelled?
z

Zach Klippenstein (he/him) [MOD]

07/27/2022, 11:02 PM
It cannot happen when returning from one suspend function to another?
No. For example, if you run this `foo`:
Copy code
suspend fun foo() {
    val job = coroutineContext.job
    println("In foo, calling bar (isActive=${job.isActive})")
    bar()
    println("In foo, after bar returned (isActive=${job.isActive})")

}

suspend fun bar() {
    println("In bar, cancelling job…")
    coroutineContext.job.cancel()
    println("In bar, job cancelled, returning to foo…")
}
You’ll get this sequence of messages:
Copy code
In foo, calling bar (isActive=true)
In bar, cancelling job…
In bar, job cancelled, returning to foo…
In foo, after bar returned (isActive=false)
u

uli

07/27/2022, 11:18 PM
The issue can happen in innocent looking code when unsing `withContext`:
Copy code
suspend fun allocateResource() = withContext(<http://Dispatchers.io|Dispatchers.io>) {
  // allocate resource. E.g. open a database
  return resource
}

suspend fun leakResource() {
  val ressource = allocateResource()
  try {
    doSomething(resource)
  } finally {
    ressource.close()
  }
}
withContext
would catch the cancelation before returning and leak the resource even before it is returned from
allocateRessource
. Nothing much you can do about it.
e

ephemient

07/27/2022, 11:18 PM
relatedly, 1. in a suspend fun with a long chain of blocking calls, you may need
ensureActive()
or
runInterruptible {}
to allow for cancellation in the middle 2. you should not make suspend calls or launch new coroutines in a
finally
(or
catch (Exception)
block if it could contain
CancellationException
) because the containing job may be cancelled which would cause such calls to immediately propagate cancellation instead
Uli, there's
withContext(NonCancellable)
if you need to ensure cancellation doesn't happen in the midst of a certain block
u

uli

07/27/2022, 11:23 PM
@ephemient the issue in above exmaple is after the withContext block terminates. Not sure if withContext(NonCancellable) would skip cancelation checks on exit as well. If anyone is up to test it let us know the results, please.
e

ephemient

07/27/2022, 11:34 PM
you just need something like
Copy code
var resource: Resource? = null
try {
    withContext(NonCancellable) {
        resource = allocateResource()
    }
    doSomething(resource)
} finally {
    resource?.close()
}
u

uli

07/27/2022, 11:39 PM
Correct. The pitfall is though, that the caller needs to take care of the NonCancellable context. And has to understand the issue enough to put the assignment of resource into the try block
f

fitermay

07/27/2022, 11:43 PM
So I guess the implicit continuations from one suspend to another do not go through a dispatcher
e

ephemient

07/27/2022, 11:49 PM
the caller gets resumed (with exception) even in case of cancellation. the only case I can see where this wouldn't happen is if the scope got garbage collected away
f

fitermay

07/27/2022, 11:59 PM
But if I understand you correctly the caller cannot get resumed with cancellation on return
Even though the return could be implicitly performed through a continuation
e

ephemient

07/28/2022, 12:00 AM
what do you mean by "cancellation in return"
f

fitermay

07/28/2022, 12:01 AM
Well a return from a suspend function could really be a continuation under the hood
e

ephemient

07/28/2022, 12:01 AM
as Joffrey said, the model is that cancellation is cooperative and only happens at suspend points
u

uli

07/28/2022, 12:01 AM
@fitermay calling a suspend function does not suspend (until the called function gets to a suspension pint itself) so returning from a suspend function does not resume.
f

fitermay

07/28/2022, 12:01 AM
When one suspend function invokes another the return is not done via a stack
u

uli

07/28/2022, 12:01 AM
yes
e

ephemient

07/28/2022, 12:01 AM
yes, but the caller will always* be resumed
f

fitermay

07/28/2022, 12:01 AM
That's only if you return without suspending first
Resumed without immediately throwing cancellation exception, I assume
What I'm getting is that even if the inner suspend function suspended the return continuation will never throw cancellation exception. I didn't previously know this
@ephemient thanks for explaining
u

uli

07/28/2022, 12:18 AM
Suspend functions do calls and returns through the stack. I just double checked through the kotlin bytecode decompiler. No resuming on return, no cancelation on return.
f

fitermay

07/28/2022, 12:22 AM
How can it return through the stack after suspending?
u

uli

07/28/2022, 12:22 AM
suspend only actually happen when functions like
suspend*Coroutine
are called. I like to call these the ‘leaf’ suspend functions. All other suspend functions are only suspend when the functions they call suspend. I.e. you build up a ‘real’ call stack until you run into a `suspend*Coroutine`call or alike.
f

fitermay

07/28/2022, 12:23 AM
I understand that it only suspends there but the return statement might be after it suspends
And in that cases how can that return be implemented without a continuation?
e

ephemient

07/28/2022, 12:24 AM
can you provide a concrete example? I think you may have a misunderstanding of the coroutine model
u

uli

07/28/2022, 12:24 AM
Copy code
suspend fun foo() : Int {
 println("Before suspend")
 delay(1)
 println("After suspend")
 return 1
}
All the magic happens around
delay
. entering foo and exiting does not suspend and does not resume
c’mon @ephemient;-)
Go and decompile this to java:
Copy code
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay

val job = Job()

suspend fun bar(): Int {
    println("Before suspemd")
    delay(10)
    println("After suspemd")
    return 1
}

suspend fun foo() {
    println("Before bar")
    var i = bar()
    println("After bar")
}
e

ephemient

07/28/2022, 12:25 AM
(I meant to ask that to Yuli, if it wasn't clear)
u

uli

07/28/2022, 12:25 AM
sorry @ephemient
The trick is in line 117 where it is checked, if the return value is var5 (IntrinsicsKt.getCOROUTINE_SUSPENDED())
When
delay
is called, bar returns with COROUTINE_SUSPENDED. This is used to signal that bar returned without a result and will return again, later. foo will then also return COROUTINE_SUSPENDED all in all unwrapping the whole call stack.
f

fitermay

07/28/2022, 12:33 AM
I see now
Suspending returns the suspended state all the way up
u

uli

07/28/2022, 12:40 AM
Exactly
That's where the suspending happens. Not at call time
After having all the details straight. Let me emphasize again. Be aware of
suspend fun foo() = withContext {
It is a common pattern and hides a resume and thereby a cancellation point after the inner return
e

ephemient

07/28/2022, 12:56 AM
it's not really different from the equivalent in blocking APIs
Copy code
fun allocate(): Resource {
    val future = executor.submit { ... }
    return future.get()
}
which can also leak if the current thread is interrupted
f

fitermay

07/28/2022, 1:48 AM
But I think the implementation detail of how return is handled is not obvious. They could've also implemented it by inverting the stack through a continuation
e

ephemient

07/28/2022, 2:08 AM
to truly suspend, there's no choice but to unwind the blocking call stack somehow. the way continuations work could be different but the "return to trampoline" part is inevitable
(at least, as far as present JVMs go, since the call stack can't be reified)
f

fitermay

07/28/2022, 4:39 AM
Right, looking at the disassembly again the return goes through the trampoline which invokes the return continuations
It just happens that in case of standard continuations cancellation exception won't happen during the trampoline execution of return
But if the continuations were CancellableContinuation it could happen
13 Views