zio / zio

ZIO — A type-safe, composable library for async and concurrent programming in Scala
https://zio.dev
Apache License 2.0
4.1k stars 1.3k forks source link

FiberRuntime.stealWork causing StackOverflow #7521

Open reimai opened 2 years ago

reimai commented 2 years ago

Hi,

in my zio app I'm reusing zio's executor as a grpc server executor. And the grpc service runs zio effects via zio.Unsafe. After an upgrade 2.0.2 -> 2.0.3 this started causing constant StackOverflows with a stack like this:

java.lang.StackOverflowError: null
...
at zio.internal.FiberRuntime.start(FiberRuntime.scala:1369)
at zio.Unsafe$.unsafe(Unsafe.scala:37)
...
at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123)
...
at zio.internal.FiberRuntime.stealWork(FiberRuntime.scala:1415)
...
at zio.internal.FiberRuntime.start(FiberRuntime.scala:1369)
at zio.Unsafe$.unsafe(Unsafe.scala:37)
...
at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123)
...
at zio.internal.FiberRuntime.stealWork(FiberRuntime.scala:1415)
...
at zio.Unsafe$.unsafe(Unsafe.scala:37)
...
at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123)
at zio.internal.ZScheduler.stealWork(ZScheduler.scala:125)
...
at zio.internal.FiberRuntime.start(FiberRuntime.scala:1369)
at zio.Unsafe$.unsafe(Unsafe.scala:37)
...
at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123)
at zio.internal.FiberRuntime.stealWork(FiberRuntime.scala:1415)
at zio.internal.FiberRuntime.runLoop(FiberRuntime.scala:1022)

which looks like an infinite recursion of work stealing. Not providing zio's executor to grpc server or turnning off work stealing via ZIO.withRuntimeFlagsScoped(RuntimeFlags.disable(RuntimeFlag.WorkStealing)) solves the problem.

Could this issue be solved or should I just avoid executing other code on zio's executor?

adamgfraser commented 2 years ago

@reimai Thanks for reporting! What would be really helpful is if you create a minimal application that reproduces this problem. That would help us fix it.

jdegoes commented 1 year ago

We disabled work stealing for now which should hopefully take care of this issue.