Skip to content

Instantly share code, notes, and snippets.

@fanf
Last active July 31, 2019 20:11
Show Gist options
  • Save fanf/c7f627d7e49fc6c5e2c0df95e6687579 to your computer and use it in GitHub Desktop.
Save fanf/c7f627d7e49fc6c5e2c0df95e6687579 to your computer and use it in GitHub Desktop.
import zio._
import zio.syntax._
// ZIO version 2.12-1.0.0-RC9.4
object Deadlock {
val runtime = new DefaultRuntime {}
trait Logger {
def log(s: AnyRef): Unit = println(s)
}
// changing either lazy val to def remove the deadlock.
trait Log {
// this one
lazy val logEffect = new Logger(){}
// here, this does not block: ... = IO.effect(logEffect.log(msg)).ignore
// but this blocks: ... = IO.yieldNow *> IO.effect(logEffect.log(msg)).ignore
final def info (msg: => AnyRef) = zio.blocking.effectBlocking(logEffect.log(msg)).ignore
}
class Blocking extends Log {
def test = info("in test") *> "oups".succeed
// or this one
lazy val block = {
runtime.unsafeRun(test.either) match {
case Right(value) => "yes"
case Left(value) => "no"
}
}
}
def main(args: Array[String]): Unit = {
println("start")
val blockling = new Blocking()
println("blocking created")
println(blockling.block)
println("never reaches end")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment