-
Notifications
You must be signed in to change notification settings - Fork 520
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
Implement a safer Resource.attempt
which releases acquired resource(s) in case of error
#4128
base: series/3.x
Are you sure you want to change the base?
Changes from 1 commit
8d16f92
07fa387
2839446
3c2d6af
cb1a293
3b7d0ee
206020c
cd729a5
f00b9cd
40cb12a
840514d
6bda660
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -700,7 +700,9 @@ sealed abstract class Resource[F[_], +A] extends Serializable { | |||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
def attempt[E](implicit F: ApplicativeError[F, E]): Resource[F, Either[E, A]] = | ||||||||||||||||
@deprecated("Use overload with MonadCancelThrow", "3.6.0") | ||||||||||||||||
def attempt[E](F: ApplicativeError[F, E]): Resource[F, Either[E, A]] = { | ||||||||||||||||
implicit val x: ApplicativeError[F, E] = F | ||||||||||||||||
this match { | ||||||||||||||||
case Allocate(resource) => | ||||||||||||||||
Resource.applyFull { poll => | ||||||||||||||||
|
@@ -710,19 +712,28 @@ sealed abstract class Resource[F[_], +A] extends Serializable { | |||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
case Bind(source, f) => | ||||||||||||||||
Resource.unit.flatMap(_ => source.attempt).flatMap { | ||||||||||||||||
Resource.unit.flatMap(_ => source.attempt(F)).flatMap { | ||||||||||||||||
case Left(error) => Resource.pure(error.asLeft) | ||||||||||||||||
case Right(s) => f(s).attempt | ||||||||||||||||
case Right(s) => f(s).attempt(F) | ||||||||||||||||
} | ||||||||||||||||
case p @ Pure(_) => | ||||||||||||||||
Resource.pure(p.a.asRight) | ||||||||||||||||
case e @ Eval(_) => | ||||||||||||||||
Resource.eval(e.fa.attempt) | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
def attempt(implicit F: MonadCancelThrow[F]): Resource[F, Either[Throwable, A]] = | ||||||||||||||||
Resource.applyCase[F, Either[Throwable, A]] { | ||||||||||||||||
allocatedCase.attempt.map { | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: shouldn't the allocation itself be interruptible? like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, this looks right. Let's make sure to add a test case for this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||
case Left(error) => (error.asLeft[A], _ => ().pure[F]) | ||||||||||||||||
case Right((a, r)) => (a.asRight[Throwable], r) | ||||||||||||||||
lenguyenthanh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
def handleErrorWith[B >: A, E](f: E => Resource[F, B])( | ||||||||||||||||
implicit F: ApplicativeError[F, E]): Resource[F, B] = | ||||||||||||||||
attempt.flatMap { | ||||||||||||||||
attempt(F).flatMap { | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. imo, We should add a "safe" variant of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. You are right, we definitely should do that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed in 3c2d6af
lenguyenthanh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
case Right(a) => Resource.pure(a) | ||||||||||||||||
case Left(e) => f(e) | ||||||||||||||||
} | ||||||||||||||||
|
@@ -1462,7 +1473,7 @@ abstract private[effect] class ResourceMonadError[F[_], E] | |||||||||||||||
implicit protected def F: MonadError[F, E] | ||||||||||||||||
|
||||||||||||||||
override def attempt[A](fa: Resource[F, A]): Resource[F, Either[E, A]] = | ||||||||||||||||
fa.attempt | ||||||||||||||||
fa.attempt(F) | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this still used the old There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should mark this instance as deprecated and create a new instance that requires There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed in cb1a293. But now cats-effect/kernel/shared/src/main/scala/cats/effect/kernel/Resource.scala Lines 1300 to 1306 in cb1a293
ResourceMonadError .
I'm not sure what the best way to fix this. I feel like We can remove it completely. But maybe it broke client code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We deprecate this one too :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We deprecate this one but keep the trait hierarchy? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good question. Maybe we should try to deprioritize this instance by moving it to the bottom of the hierarchy. Alternatively, we could just remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kept the hierachy the same but removed implicit modifier and deprecated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about it more and actually I think it's okay to leave the By leaving it |
||||||||||||||||
|
||||||||||||||||
def handleErrorWith[A](fa: Resource[F, A])(f: E => Resource[F, A]): Resource[F, A] = | ||||||||||||||||
fa.handleErrorWith(f) | ||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can do a trick here to automatically fix more callsites without requirng them to recompile: we can pattern match on
F
and if it isSync
orAsync
then we can use the fixed implementation instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how to implement your suggestion.
I tried:
But it doesn't work as
attempt
withMonadCancelThrow
returnsResource[F, Either[Throwable, A]]
but notResource[F, Either[E, A]]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can cast unsafely. If
F
isSync
thenE
must beThrowable
. Also I just realized sinceAsync <: Sync
we only need to match forSync
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed it 3b7d0ee