This is great, it may be worth mentioning alternatives. In Arrow Fx cancelation is not cooperative but automatic. Additionally there is an idea that the cancelation primitives should be part of the std lib suspend intrinsics and not just implemented in KotlinX coroutines so different libraries that implement suspension can interoperate in cancelation.
Cooperative cancelation is up to user discipline with KotlinX but it can be wired automatically
03/08/2020, 11:17 AM
@zsmb Thanks a lot for this informative blog post.
However one follow up question, how far you should go to achieve this cooperative-ness? because one drawback of this is that your code will be full of
03/08/2020, 11:56 AM
@zsmb thanks for the article! Here is a gist that demonstrates automatic cancellation. https://gist.github.com/raulraja/d289db68db94462bfc03811a76810d66 These examples where created by @simon.vergauwen and demonstrate how Arrow Fx wires cancellation checks across flatMap boundaries to ensure all dependent fibers are cancelled and no resources are wasted when your operation is composed of a large number of async and concurrent task. Cancellation also transports exceptions in fibers back to the caller inside IO to ensure you can wire safe resource handling when acquiring resources async that may be used in different context but they need to be released regardless of where exceptions happen.