Skip to content

Instantly share code, notes, and snippets.

@fanf
Last active March 10, 2020 21:32
Show Gist options
  • Save fanf/90a76e732ef08e761b64170c382304fd to your computer and use it in GitHub Desktop.
Save fanf/90a76e732ef08e761b64170c382304fd to your computer and use it in GitHub Desktop.
Deadlock in ZIO with `Runtime.default` runtime
/*
Dependencies:
- scala 2.13.1
- zio 1.0.0-RC18-1+44-24f507c2+20200310-2150-SNAPSHOT (local build on master on 2020-03-10)
- with openjdk 11.0.4+11
*/
package some.pkg
import zio.Runtime
import zio.UIO
import zio.ZIO
import zio.ZLayer
class internalRuntime {
lazy val runtime = Runtime.default
runtime.unsafeRun(UIO.effectTotal(println("**** case 0: non blocking")))
unsafeRun(UIO.effectTotal(println("**** case 1: blocking")))
def blocking[E, A](e: ZIO[Any, E, A]): ZIO[Any, E, A] = {
ZIO.accessM[_root_.zio.blocking.Blocking](_.get.blocking(e)).provideLayer(ZLayer.succeedMany(runtime.environment))
}
def unsafeRun[E, A](e: => ZIO[Any, E, A]): Unit = {
runtime.unsafeRun(ZIO.accessM[_root_.zio.blocking.Blocking](_.get.blocking(e)))
}
}
object someRoot { // this is the entry point for my runtime
lazy val zioRuntime = new internalRuntime()
}
object Elsewhere { // somewhere else, a blocking unsafe run is called
someRoot.zioRuntime.runtime.unsafeRun(UIO.effectTotal(println("**** case 1000: non blocking")))
someRoot.zioRuntime.unsafeRun(UIO.effectTotal(println("**** case 2000: blocking")))
def print()= println("ok")
}
object TestRuntime {
def main(args: Array[String]): Unit = {
// in internal, it blocks
someRoot.zioRuntime.runtime.unsafeRun(UIO.effectTotal(println("**** case 100: non blocking")))
someRoot.zioRuntime.unsafeRun(UIO.effectTotal(println("**** case 200: blocking")))
// it blocks here, when calling the blocking variant
Elsewhere.print()
println("done")
/* console output:
**** case 0: non blocking
**** case 1: blocking
**** case 100: non blocking
**** case 200: blocking
**** case 1000: non blocking
-- blocks here --
"main@1" prio=5 tid=0x1 nid=NA waiting
java.lang.Thread.State: WAITING
at java.lang.Object.wait(Object.java:-1)
at java.lang.Object.wait(Object.java:328)
at zio.internal.OneShot.get(OneShot.scala:79)
at zio.Runtime.unsafeRunSync(Runtime.scala:82)
at zio.Runtime.unsafeRunSync$(Runtime.scala:77)
at zio.Runtime$$anon$2.unsafeRunSync(Runtime.scala:226)
at zio.Runtime.unsafeRun(Runtime.scala:57)
at zio.Runtime.unsafeRun$(Runtime.scala:56)
at zio.Runtime$$anon$2.unsafeRun(Runtime.scala:226)
at com.normation.internalRuntime.unsafeRun(ZioCommonsTest.scala:91)
at com.normation.Elsewhere$.<clinit>(ZioCommonsTest.scala:103)
at com.normation.TestRuntime$.main(ZioCommonsTest.scala:115)
at com.normation.TestRuntime.main(ZioCommonsTest.scala:-1)
"zio-default-blocking-1-399534175@1482" daemon prio=5 tid=0xf nid=NA runnable
java.lang.Thread.State: RUNNABLE
at com.normation.Elsewhere$$$Lambda$142.1966250569.apply$mcV$sp(Unknown Source:-1)
at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
at zio.internal.FiberContext.evaluateNow(FiberContext.scala:382)
at zio.internal.FiberContext.$anonfun$evaluateLater$1(FiberContext.scala:687)
at zio.internal.FiberContext$$Lambda$125.1907431275.run(Unknown Source:-1)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.lang.Thread.run(Thread.java:834)
* */
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment