What's the pattern to dispose of a resource when a...
# coroutines
f
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
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
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
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
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
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
@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
you just need something like
Copy code
var resource: Resource? = null
try {
    withContext(NonCancellable) {
        resource = allocateResource()
    }
    doSomething(resource)
} finally {
    resource?.close()
}
u
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
So I guess the implicit continuations from one suspend to another do not go through a dispatcher
e
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
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
what do you mean by "cancellation in return"
f
Well a return from a suspend function could really be a continuation under the hood
e
as Joffrey said, the model is that cancellation is cooperative and only happens at suspend points
u
@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
When one suspend function invokes another the return is not done via a stack
u
yes
e
yes, but the caller will always* be resumed
f
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
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
How can it return through the stack after suspending?
u
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
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
can you provide a concrete example? I think you may have a misunderstanding of the coroutine model
u
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
(I meant to ask that to Yuli, if it wasn't clear)
u
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
I see now
Suspending returns the suspended state all the way up
u
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
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
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
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
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