question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected finalization of uncancelable resource

See original GitHub issue

Scala version: 2.13.6 Cats-effect version: 3.2.9

Minimal example:

import cats.effect.{IO, IOApp, Resource}
import cats.effect.syntax.monadCancel._

object Main extends IOApp.Simple {

  def resource(prefix: String): Resource[IO, Unit] =
    Resource.makeCase(IO.unit) {
      case (_, Resource.ExitCase.Succeeded) => IO.println(s"[$prefix]. Release. Success")
      case (_, Resource.ExitCase.Errored(e)) => IO.println(s"[$prefix]. Release. Error: $e")
      case (_, Resource.ExitCase.Canceled) => IO.println(s"[$prefix]. Release. Canceled")
    }

  def run: IO[Unit] =
    for {
      r1 <- resource("general").use(_ => IO.raiseError(new RuntimeException("oops"))).attempt

      // emulate error
      r2 <- resource("uncancelable").uncancelable.use(_ => IO.raiseError(new RuntimeException("oops"))).attempt

      // emulate cancelation
      d <- IO.deferred[Unit]
      fiber <- resource("uncancelable cancel").uncancelable.use(_ => d.complete(()) >> IO.never).start
      _ <- d.get // wait till `use` hit `never`
      _ <- fiber.cancel
      
      _ <- IO.println("r1: " + r1)
      _ <- IO.println("r2: " + r2)
    } yield ()

}

The output:

[general]. Release. Error: java.lang.RuntimeException: oops
[uncancelable]. Release. Success
[uncancelable cancel]. Release. Success

r1: Left(java.lang.RuntimeException: oops)
r2: Left(java.lang.RuntimeException: oops)

For some reason, a resource that has been marked as uncancelable always has ExitCase.Succeeded during the release stage. Even though it has been canceled or completed with an error.

Resource.uncancelable[IO, Unit](_ => resource("uncancelable")) has the same behavior.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
TimWSpencecommented, Nov 30, 2021

If nobody objects, I’m going to have a go at this. I haven’t really dealt with Resource internals before so it should be interesting 😅

1reaction
djspiewakcommented, Nov 30, 2021

Give it a shot! 😃 Really this is about implementing an allocatedFull that is dual to applyFull

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unexpected finalization of uncancelable resource - bytemeta
For some reason, a resource that has been marked as uncancelable always has ExitCase.Succeeded during the release stage. Even though it has been...
Read more >
cats-effect/Hotswap.scala at series/3.x · typelevel/cats-effect · GitHub
The newly acquired resource is returned and is released either when the [[Hotswap]] is. * finalized or upon the next call to [[swap]],...
Read more >
Cancel subscriptions | Stripe Documentation
After all settings are finalized, click Cancel subscription. ... This occurs to prevent unexpected automatic payment attempts and reminder emails.
Read more >
Resource Safety - Monix BIO
Running finalizer. guarantee ensures that we run the provided finalizer regardless of the exit condition, be it successful completion, failure, or cancellation.
Read more >
Messages ING001I to INGY1337I - IBM
Operator response. Specify the fully qualified resource name and reissue the command. ... ING070E UNEXPECTED RESPONSE FROM system RC return code ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found