Cats: Why does this sample program hang?

Created on 24 Apr 2019  路  4Comments  路  Source: typelevel/cats

Not sure what the problem is but this code sample hangs under some conditions (documented below).

package thiswillhang

import cats._
import cats.effect._

/**
  * This will hang if run with both flags. Why?
  *
  * {{{
  *   sbt "run resource raise"
  * }}}
  */
object ThisWillHang extends IOApp {
  type ApplicativeErrorThrowable[F[_]] = ApplicativeError[F, Throwable]

  def run(args: List[String]): IO[ExitCode] =
    for {
      n <- acquireIOInt[IO](args)
      _ <- doPrint[IO](n)
    } yield ExitCode.Success

  def acquireIOInt[F[_]](args: List[String])(implicit F: Sync[F]): F[Int] = {
    val err =
      ApplicativeError[F, Throwable]
        .fromEither(buildErr(args.contains("raise")))

    if (args.contains("resource"))
      Resource
        .liftF(err)
        .use(F.pure)
    else
      err
  }

  def buildErr(doHang: Boolean): Either[Throwable, Int] =
    if (doHang)
      Left(new IllegalArgumentException("hey"))
    else
      Right(12345)

  def doPrint[F[_]](n: Int)(implicit F: Sync[F]): F[Unit] =
    F.delay {
      println(n)
    }
}

Most helpful comment

Confirmed fixed in newly available cats-effect 1.3.0

All 4 comments

Yes, errors in resource acquisition can cause IOApp to hang. I reported a similar thing and @SystemFw said it's a known problem in cats-effect that he thought would be fixed soon 鈥β燜abio can you comment?

Please see here for more details https://github.com/typelevel/cats-effect/issues/487. A release of the fix is planned soon as confirmed here

Great! Filed in the wrong repo anyway. Closing as dupe.

Confirmed fixed in newly available cats-effect 1.3.0

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LukaJCB picture LukaJCB  路  3Comments

diesalbla picture diesalbla  路  4Comments

adelbertc picture adelbertc  路  5Comments

kailuowang picture kailuowang  路  3Comments

LukaJCB picture LukaJCB  路  4Comments