This statement has been closed 0 ▲ Richard’s Blog 2 hours ago · Tech · hide · 0 comments 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,… No comments yet. Log in to reply on the Fediverse. Comments will appear here.