kotlinx.coroutines icon indicating copy to clipboard operation
kotlinx.coroutines copied to clipboard

Coroutines guide should warn about cancellation exceptions when transferring resources across suspension points

Open dkhalanskyjb opened this issue 7 months ago • 0 comments
trafficstars

Originally reported by Uli Luckas in the Kotlinlang Slack: https://kotlinlang.slack.com/archives/C1CFAFJSK/p1742815571506039?thread_ts=1742814326.569149&cid=C1CFAFJSK

The caveat with how withContext may drop closeable resources (https://github.com/Kotlin/kotlinx.coroutines/issues/3504) should be highlighted in the guide:

val socket = withContext(Dispatchers.IO) {
  Socket("localhost", 8080)
} // CancellationException can be thrown here, after the resource gets created
socket.use {
  // ...
}

The same goes for prompt cancellation guarantees in the other APIs.

dkhalanskyjb avatar Apr 15 '25 11:04 dkhalanskyjb