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

Bracket finalizer should be uncancelable #2968

Merged
merged 2 commits into from
Aug 15, 2022

Conversation

armanbilge
Copy link
Member

Possibly fixes #2966?

@mpilquist
Copy link
Member

Can we try how this fix works with both this:

Stream.bracket(IO.unit)(_ => IO.sleep(2.seconds)).compile.drain.timeout(1.seconds)

...as well as the original bug in #2966?

@armanbilge
Copy link
Member Author

This one appears to be fixed:

//> using scala "3.1.3"
//> using lib "co.fs2::fs2-core::3.2.12-15-ca20578-SNAPSHOT"
//> using repository "https://s01.oss.sonatype.org/content/repositories/snapshots/"

import cats.effect._
import fs2._

import scala.concurrent.duration._

object BracketTest extends IOApp.Simple {
  def run = Stream
    .bracket(IO.unit)(_ =>
      IO.println("releasing") *>
        IO.sleep(2.seconds) *>
        IO.println("released")
    )
    .compile
    .drain
    .timeout(1.seconds)
}

As does this one:

//> using scala "3.1.3"
//> using lib "co.fs2::fs2-core::3.2.12-15-ca20578-SNAPSHOT"
//> using repository "https://s01.oss.sonatype.org/content/repositories/snapshots/"

import cats.effect.{ExitCode, IO, IOApp, Resource, Async, Sync, Temporal}
import cats.effect.syntax.all._
import cats.syntax.all._
import fs2.Stream

import scala.concurrent.duration.DurationInt

object BracketTest extends IOApp {
  case class RunF[F[_]: Async]() {
    def say(msg: String) = Sync[F].delay(println(msg))

    val sleeper = (
      Temporal[F].sleep(1.seconds) *>
        say("slept")
    ).replicateA(4).void

    val sleeperResource = Resource
      .make(sleeper)(_ =>
        say("releasing sleeper resource...") *>
          sleeper *>
          say("released sleeper resource")
      )

    val stream =
      Stream.resource(sleeperResource).evalTap(_ => say("using resource")).void
        .onFinalizeCase(ec =>
          say(s"Finalized with ec=$ec")
        )

    val runF =
      stream.compile.drain // Resource is not released OK
      //Stream(stream).parJoinUnbounded.compile.drain // Resource is released OK
  }

  override def run(args: List[String]): IO[ExitCode] = for {
    _ <- RunF[IO]().runF.timeout(6.seconds)
  } yield ExitCode.Success
}

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

Successfully merging this pull request may close these issues.

Stream.resource/Stream.bracket release action is not being completed when stream is interrupted
2 participants