Skip to content

Instantly share code, notes, and snippets.

@TimWSpence
Created November 17, 2023 11:40
Show Gist options
  • Save TimWSpence/6311dcf27ef23127805a9bd836f6d15c to your computer and use it in GitHub Desktop.
Save TimWSpence/6311dcf27ef23127805a9bd836f6d15c to your computer and use it in GitHub Desktop.
Minimization of hanging caused by canceling a Doobie transaction whilst inside `WeakAsync.liftK`
//> using scala "3.3.1"
//> using dep "org.typelevel::cats-effect::3.5.2"
import cats.*
import cats.syntax.all.*
import cats.effect.*
import cats.effect.std.Dispatcher
import scala.concurrent.duration.*
object Repro extends IOApp.Simple:
val liftK: Resource[IO, IO ~> IO] =
Dispatcher.parallel[IO].map { dispatcher =>
new (IO ~> IO) {
def apply[T](
fa: IO[T]
) =
IO.fromFutureCancelable {
IO.uncancelable { poll =>
poll(IO.delay(dispatcher.unsafeToFutureCancelable(fa))).map {
case (fut, cancel) =>
(
fut,
IO.fromFuture(IO.delay(cancel()))
)
}
}
}
}
}
val run =
liftK.use { lift =>
val run = lift(IO.sleep(2.seconds)).timeoutTo(1.micros, IO.unit) >> IO.println(
"-------------------------"
)
List.fill(100)(run).sequence.void
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment