Describe coroutine cancellation mechanics and cooperative suspend functions

Tests cooperative cancellation mechanics. cancel() sets a Job to cancelling; suspend functions check this at suspension points and throw CancellationException, yet a tight non-suspending loop never checks and keeps running.
What's really being asked
Whether you understand that Kotlin coroutine cancellation is cooperative rather than preemptive. Interviewers want to see that you know cancellation happens when the coroutine checks for it, that non-suspending CPU-bound work can ignore cancellation, and that structured concurrency relies on CancellationException propagation to clean up child coroutines.
The full answer
Four things in order. First, calling job.cancel() moves the Job to a cancelling state; it does not forcibly stop the underlying thread. Second, standard library suspend functions like delay and awaitCancellation are cooperative because they check the Job state at suspension points and throw CancellationException if the job is no longer active. Third, a long non-suspending computation inside a loop keeps running after cancel() because it never reaches a point where the coroutine checks for cancellation. Because the Job handle allows you to check whether the coroutine is active, you can explicitly poll that state or call a standard suspending function to introduce a cancellation check. Fourth, CancellationException is a normal mechanism for unwinding the coroutine stack so finally blocks run and resources release; if you catch it, you must rethrow it so parents and siblings are cancelled too under structured concurrency.
The mistakes people make
Claiming that cancel() forcibly stops the underlying thread. Saying that simply adding the suspend keyword to a function makes it automatically check for cancellation without any explicit checks. Swallowing CancellationException in a catch block without rethrowing, which breaks cancellation propagation and leaks child coroutines. Assuming that because a function suspends, it will always notice cancellation immediately even during heavy CPU work.
What usually comes next
How would you make a CPU-intensive loop cancellable? What is the difference between checking the Job active state and using a suspending function to check cancellation? What happens if you catch CancellationException and do not rethrow it? How does cancellation propagate from a parent Job to its children? When should you wait for a cancelled coroutine to finish?
A concrete example
Imagine a coroutine launched on Dispatchers.Default that computes prime numbers in a while loop doing purely local math. Calling job.cancel() on it has no effect because the loop contains no suspension points and never checks whether the Job is still active. To fix this, you could periodically check the Job active state or call a standard suspending function like delay so the coroutine has a chance to throw CancellationException. Once that exception is thrown, the stack unwinds, any finally block releases resources, and the parent can finish.
Interview question
Why does a CPU-intensive while-loop inside a coroutine ignore job.cancel() and keep running?
- a.The suspend modifier on the function prevents cancellation from affecting non-suspending code
- b.The cancel() call interrupts the underlying thread, which the loop ignores unless handled
- c.The coroutine runtime defers throwing CancellationException until the current computation finishes
- d.The loop never hits a suspension point where the coroutine checks its Job stateCorrect
Why? this is the answer
Kotlin coroutine cancellation is cooperative, so a tight loop without suspension points never checks the Job's cancelling state and continues running. Distractor B is wrong because cancel() does not forcibly interrupt the underlying thread; it only sets a flag that standard suspend functions check at suspension points.
Just read this? Test yourself on what you have been reading.
Read the original → kotlinlang.org
- #kotlin
- #coroutines
- #cancellation
- #structured-concurrency
- #android
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on kotlin — each one lists the topics its interview covers.
See open roles