Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App + cats-effect hangs on 3.1.0 but works fine in 2.13.7 #2748

Closed
jmgimeno opened this issue Jan 12, 2022 · 5 comments
Closed

App + cats-effect hangs on 3.1.0 but works fine in 2.13.7 #2748

jmgimeno opened this issue Jan 12, 2022 · 5 comments

Comments

@jmgimeno
Copy link
Contributor

jmgimeno commented Jan 12, 2022

This code

import cats.effect._
import cats.implicits._
import cats.effect.unsafe.implicits.global

object IOComposition extends App {
  val hello = IO(println(s"[${Thread.currentThread.getName}] Hello"))
  val world = IO(println(s"[${Thread.currentThread.getName}] World"))
  val hw1: IO[Unit] = for {
    _ <- hello
    _ <- world
  } yield ()
  val hw2: IO[Unit] =
    (hello, world).mapN((_, _) => ())
  hw1.unsafeRunSync()
  hw2.unsafeRunSync()
}

hangs with Scala 3.1.0 after printing only "Hello"

Compiling project (Scala 3.1.0, JVM)
Compiled project (Scala 3.1.0, JVM)
[io-compute-2] Hello

but works perfecty with Scala 2.13.7

Compiling project (Scala 2.13.7, JVM)
Compiled project (Scala 2.13.7, JVM)
[io-compute-2] Hello
[io-compute-4] World
@djspiewak
Copy link
Member

Try using defs. I'd be willing to bet this is caused by the issues with MethodHandle linking to null during class load.

@SethTisue
Copy link
Member

SethTisue commented Jan 12, 2022

I'd also be interested in knowing if the problem goes away if you avoid using App. App is not recommended in Scala 3, as per https://docs.scala-lang.org/scala3/reference/changed-features/main-functions.html

@djspiewak
Copy link
Member

I suspect that, if you avoid using App, the source of the problem will actually become a hair more obvious. Or you can just push the vals into the body of the main method (which is effectively what Scala 2 is doing with the same code but Scala 3 is not), and that should also resolve the deadlock.

@jmgimeno
Copy link
Contributor Author

jmgimeno commented Jan 13, 2022

I'd also be interested in knowing if the problem goes away if you avoid using App

Yes, if I create a regular main method instead of extending App, it works perfectly

Changing the definitions of hello and world from val instead to def also solves the problem in Scala 3.1.0 even when using App.

@djspiewak
Copy link
Member

Sweet! So this is definitely the MethodHandle concurrent initialization issue then. It's actually part of the JVM itself and not really anything that either Scala or Cats Effect can address. In fact, you can reproduce this issue using bare Java.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants