Fix resource leak in .toResourceZIO
that could happen just after ZManaged was acquired but before the finalizer was registered in Resource interpreter (https://github.com/typelevel/cats-effect/blob/bf1fa530bd651325c7b060389952d37b4852d5e5/core/shared/src/main/scala/cats/effect/Resource.scala#L133)
#234
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Basically, there's a
flatMap
in-between aResource.Suspend
node ending andResource.Allocate
node being processed (- https://github.com/typelevel/cats-effect/blob/bf1fa530bd651325c7b060389952d37b4852d5e5/core/shared/src/main/scala/cats/effect/Resource.scala#L133)If Resource produced by
toResourceZIO
was interrupted at that point, the bracketCase's insidemanaged.zio
would already finish, but the finalizer for theresourceMap
wouldn't be registered yet, because it's in theResource.Allocate
node which is the result ofSuspend
. This moves theResource.Allocate
node before suspend, so that the finalizer for thereleaseMap
is registered before themanaged.zio
is evaluated and the finalizers are registered as promptly as possible.