This statement has been closed 2 ▲ Richard’s Blog 124 days ago · Tech · hide · Fediverse post Cancellation on concurrent tasks is tricky to get right. I doubt I’ve ever succeeded.In the last few weeks I’ve re-learned that: cancellation (interruption) is a request, and you don’t know when it’ll land; and you need to defer cancellation around critical sections—which sounds dramatic, but “critical” refers to a code section you’d prefer not to stop mid-flow. This is in the context of Cats Effect, a library in the Scala programming language world.The problem was an uncaught error deep in a database library: “This statement has been closed”. We’d figured out it was benign, but infuriating to see in a log.The code triggering it was something along these lines:// Do `job`, keeping a lease active while the job runsdef doLongishThing: IO[Unit] = IO.race(keepAlive, job)// Keep on claiming the lease until someone stops us:def keepAlive: IO[Unit] = database.updateLease >> sleep(2 seconds) >> keepAlive Eventually the job finishes and the “loser” of the race, keepAlive, is cancelled. This,… Commenting on Bubbles has been removed. Why? If you want to talk about this post, write to the author, use the comments on their blog if it has them, or reply with a "RE:" post on your own blog. Suggestions? feedback@bubbles.town