How does suspension work in Kotlin coroutines?

This is a chapter from the book Kotlin Coroutines. You can find it on LeanPub or Amazon.

Suspending functions are the hallmark of Kotlin coroutines. The suspension capability is the single most essential feature upon which all other Kotlin Coroutines concepts are built. That is why our goal in this chapter is to forge a solid understanding of how it works.

Suspending a coroutine means stopping it in the middle. It is similar to stopping a video game: you save at a checkpoint, turn off the game, and both you and your computer can focus on doing different things. Then, when you would like to continue some time later, you turn on the game again, resume from the saved checkpoint, and thus you can play from where you previously left off. This is an analogy to coroutines. When they are suspended, they return a Continuation. It is like a save in a game: we can use it to continue from the point where we stopped.

Notice that this is very different from a thread, which cannot be saved, only blocked. A coroutine is much more powerful. When suspended, it does not consume any resources. A coroutine can be resumed on a different thread, and (at least in theory) a continuation can be serialized, deserialized and then resumed.

Resume

So let's see it in action. For this, we need a coroutine. We start coroutines using coroutine builders (like runBlocking or launch), which we will introduce later. Although there is also a simpler way, we can use a suspending main function.

Suspending functions are functions that can suspend a coroutine. This means that they must be called from a coroutine (or another suspending function). In the end, they need to have something to suspend. Function main is the starting point, so Kotlin will start it in a coroutine when we run it.

import kotlin.* //sampleStart suspend fun main() { println("Before") println("After") } // Before // After //sampleEnd

This is a simple program that will print "Before" and "After". What will happen if we suspend in between these two prints? For that, we can use the suspendCoroutine function provided by the standard Kotlin library1.

import kotlin.coroutines.* //sampleStart suspend fun main() { println("Before") suspendCoroutine<Unit> { } println("After") } // Before //sampleEnd

If you call the above code, you will not see the "After", and the code will not stop running (as our main function never finished). The coroutine is suspended after "Before". Our game was stopped and never resumed. So, how can we resume? Where is this aforementioned Continuation?

Take a look again at the suspendCoroutine invocation and notice that it ends with a lambda expression ({ }). The function passed as an argument will be invoked before the suspension. This function gets a continuation as an argument.

import kotlin.coroutines.* //sampleStart suspend fun main() { println("Before") suspendCoroutine<Unit> { continuation -> println("Before too") } println("After") } // Before // Before too //sampleEnd

Such a function calling another function in place is nothing new. This is similar to let, apply, or useLines. The suspendCoroutine function is designed in the same way, which makes it possible to use the continuation just before the suspension. After the suspendCoroutine call, it would be too late. So, the lambda expression passed as a parameter to the suspendCoroutine function is invoked just before the suspension. This lambda is used to store this continuation somewhere or to plan whether to resume it.

We could use it to resume immediately:

import kotlin.coroutines.* //sampleStart suspend fun main() { println("Before") suspendCoroutine<Unit> { continuation -> continuation.resume(Unit) } println("After") } // Before // After //sampleEnd

Notice that “After” in the example above is printed because we call resume in suspendCoroutine2.

Since Kotlin 1.3, the definition of Continuation has been changed. Instead of resume and resumeWithException, there is one resumeWith function that expects Result. The resume and resumeWithException functions we are using are extension functions from the standard library that use resumeWith.

inline fun <T> Continuation<T>.resume(value: T): Unit = resumeWith(Result.success(value)) inline fun <T> Continuation<T>.resumeWithException( exception: Throwable ): Unit = resumeWith(Result.failure(exception))

We could also start a different thread that will sleep for a set duration and resume after that time:

import kotlin.concurrent.thread import kotlin.coroutines.* //sampleStart suspend fun main() { println("Before") suspendCoroutine<Unit> { continuation -> thread { println("Suspended") Thread.sleep(1000) continuation.resume(Unit) println("Resumed") } } println("After") } // Before // Suspended // (1 second delay) // After // Resumed //sampleEnd

This is an important observation. Notice that we can make a function that will resume our continuation after a defined period. In such a case, the continuation is captured by the lambda expression, as shown in the code snippet below.

import kotlin.concurrent.thread import kotlin.coroutines.* //sampleStart fun continueAfterSecond(continuation: Continuation<Unit>) { thread { Thread.sleep(1000) continuation.resume(Unit) } } suspend fun main() { println("Before") suspendCoroutine<Unit> { continuation -> continueAfterSecond(continuation) } println("After") } // Before // (1 sec) // After //sampleEnd

Such a mechanism works, but it unnecessarily creates threads only to end them after just a second of inactivity. Threads are not cheap, so why waste them? A better way would be to set up an "alarm clock". In JVM, we can use ScheduledExecutorService for that. We can set it to call some continuation.resume(Unit) after a defined amount of time.

import java.util.concurrent.* import kotlin.coroutines.* //sampleStart private val executor = Executors.newSingleThreadScheduledExecutor { Thread(it, "scheduler").apply { isDaemon = true } } suspend fun main() { println("Before") suspendCoroutine<Unit> { continuation -> executor.schedule({ continuation.resume(Unit) }, 1000, TimeUnit.MILLISECONDS) } println("After") } // Before // (1 second delay) // After //sampleEnd

Suspending for a set amount of time seems like a useful feature. Let's extract it into a function. We will name it delay.

import java.util.concurrent.* import kotlin.coroutines.* //sampleStart private val executor = Executors.newSingleThreadScheduledExecutor { Thread(it, "scheduler").apply { isDaemon = true } } suspend fun delay(timeMillis: Long): Unit = suspendCoroutine { cont -> executor.schedule({ cont.resume(Unit) }, timeMillis, TimeUnit.MILLISECONDS) } suspend fun main() { println("Before") delay(1000) println("After") } // Before // (1 second delay) // After //sampleEnd

The executor still uses a thread, but it is one thread for all coroutines using the delay function. This is much better than blocking one thread every time we need to wait for some time.

This is exactly how delay from the Kotlin Coroutines library used to be implemented. The current implementation is more complicated, mainly so as to support testing, but the essential idea remains the same.

Resuming with a value

One thing that might concern you is why we passed Unit to the resume function. You might also be wondering why we used Unit as a type argument for the suspendCoroutine. The fact that these two are the same is no coincidence. Unit is also returned from the function and is the generic type of the Continuation parameter.

val ret: Unit = suspendCoroutine<Unit> { cont: Continuation<Unit> -> cont.resume(Unit) }

When we call suspendCoroutine, we can specify which type will be returned in its continuation. The same type needs to be used when we call resume.

import kotlin.coroutines.* //sampleStart suspend fun main() { val i: Int = suspendCoroutine<Int> { cont -> cont.resume(42) } println(i) // 42 val str: String = suspendCoroutine<String> { cont -> cont.resume("Some text") } println(str) // Some text val b: Boolean = suspendCoroutine<Boolean> { cont -> cont.resume(true) } println(b) // true } //sampleEnd

This does not fit well with the game analogy. I don't know of any game in which you can put something inside the game when resuming a save3 (unless you cheated and googled how to solve the next challenge). However, it makes perfect sense with coroutines. Often we are suspended because we are waiting for some data, such as a network response from an API. This is a common scenario. Your thread is running business logic until it reaches a point where it needs some data. So, it asks your network library to deliver it. Without coroutines, this thread would then need to sit and wait. This would be a huge waste as threads are expensive, especially if this is an important thread, like the Main Thread on Android. With coroutines, it just suspends and gives the library a continuation with the instruction "Once you’ve got this data, just send it to the resume function". Then the thread can go do other things. Once the data is there, the thread will be used to resume from the point where the coroutine was suspended.

To see this in action, let's see how we might suspend until we receive some data. In the example below, we use a callback function requestUser that is implemented externally.

import kotlin.concurrent.thread import kotlin.coroutines.* data class User(val name: String) fun requestUser(callback: (User) -> Unit) { thread { Thread.sleep(1000) callback.invoke(User("Test")) } } //sampleStart suspend fun main() { println("Before") val user = suspendCoroutine<User> { cont -> requestUser { user -> cont.resume(user) } } println(user) println("After") } // Before // (1 second delay) // User(name=Test) // After //sampleEnd

Calling suspendCoroutine directly is not convenient. We would prefer to have a suspending function instead. We can extract it ourselves.

import kotlin.concurrent.thread import kotlin.coroutines.* data class User(val name: String) fun requestUser(callback: (User) -> Unit) { thread { Thread.sleep(1000) callback.invoke(User("Test")) } } //sampleStart suspend fun requestUser(): User { return suspendCoroutine<User> { cont -> requestUser { user -> cont.resume(user) } } } suspend fun main() { println("Before") val user = requestUser() println(user) println("After") } //sampleEnd

Currently, suspending functions are already supported by many popular libraries, such as Retrofit and Room. This is why we rarely need to use callback functions in suspending functions. However, if you have such a need, I recommend using suspendCancellableCoroutine (instead of suspendCoroutine), which will be explained in the Cancellation chapter.

suspend fun requestUser(): User { return suspendCancellableCoroutine<User> { cont -> requestUser { user -> cont.resume(user) } } }

You might wonder what happens if the API gives us not data but some kind of problem. What if the service is dead or responds with an error? In such a case, we cannot return data; instead, we should throw an exception from the place where the coroutine was suspended. This is where we need to resume with an exception.

Resume with an exception

Every function we call might return some value or throw an exception. The same is true for suspendCoroutine. When resume is called, it returns data passed as an argument. When resumeWithException is called, the exception that is passed as an argument is conceptually thrown from the suspension point.

import kotlin.coroutines.* //sampleStart class MyException : Throwable("Just an exception") suspend fun main() { try { suspendCoroutine<Unit> { cont -> cont.resumeWithException(MyException()) } } catch (e: MyException) { println("Caught!") } } // Caught! //sampleEnd

This mechanism is used for different kinds of problems. For instance, to signal network exceptions.

suspend fun requestUser(): User { return suspendCancellableCoroutine<User> { cont -> requestUser { resp -> if (resp.isSuccessful) { cont.resume(resp.data) } else { val e = ApiException( resp.code, resp.message ) cont.resumeWithException(e) } } } } suspend fun requestNews(): News { return suspendCancellableCoroutine<News> { cont -> requestNews( onSuccess = { news -> cont.resume(news) }, onError = { e -> cont.resumeWithException(e) } ) } }

Suspending a coroutine, not a function

One thing that needs to be stressed here is that we suspend a coroutine, not a function. Suspending functions are not coroutines, just functions that can suspend a coroutine4. Imagine that we store a function in some variable and try to resume it after the function call.

import kotlin.coroutines.* //sampleStart // Do not do this var continuation: Continuation<Unit>? = null suspend fun suspendAndSetContinuation() { suspendCoroutine<Unit> { cont -> continuation = cont } } suspend fun main() { println("Before") suspendAndSetContinuation() continuation?.resume(Unit) println("After") } // Before //sampleEnd

This makes no sense. It is equivalent to stopping a game and planning to resume it at a later point in the game. resume will never be called. You will only see "Before", and your program will never end unless we resume on another thread or another coroutine. To show this, we can set another coroutine to resume after a second.

import kotlinx.coroutines.* import kotlin.coroutines.* //sampleStart // Do not do this, potential memory leak var continuation: Continuation<Unit>? = null suspend fun suspendAndSetContinuation() { suspendCoroutine<Unit> { cont -> continuation = cont } } suspend fun main() = coroutineScope { println("Before") launch { delay(1000) continuation?.resume(Unit) } suspendAndSetContinuation() println("After") } // Before // (1 second delay) // After //sampleEnd

Summary

I hope now you have a clear picture of how suspension works from the user’s point of view. It is important, and we will see it throughout the book. It is also practical, as now you can take callback functions and make them suspending functions. If you are like me and like to know exactly how things work, you are likely still wondering about how it is implemented. If you're curious about this, it will be covered in the next chapter. If you don't feel you need to know, just skip it. It is not very practical, it just reveals the magic of Kotlin coroutines.

1:

It directly calls suspendCoroutineUninterceptedOrReturn, which is a primitive function, that means a function with intrinsic implementation.

2:

This statement is true, but I need to clarify. You might imagine that here we suspend and immediately resume. This is a good intuition, but the truth is that there is an optimization that prevents a suspension if resuming is immediate.

3:

During a workshop discussion it turned out there is such a game: in Don't Starve Together, when you resume, you can change players. I haven’t played it myself, but this sounds like a nice metaphor for resuming with a value.

4:

Suspending main function is a special case. Kotlin compiler starts it in a coroutine.