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

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

Conversation

neko-kai
Copy link
Member

@neko-kai neko-kai commented Oct 3, 2020

Basically, there's a flatMap in-between a Resource.Suspend node ending and Resource.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 inside managed.zio would already finish, but the finalizer for the resourceMap wouldn't be registered yet, because it's in the Resource.Allocate node which is the result of Suspend. This moves the Resource.Allocate node before suspend, so that the finalizer for the releaseMap is registered before the managed.zio is evaluated and the finalizers are registered as promptly as possible.

Copy link
Member

@ghostdogpr ghostdogpr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch 👍

@neko-kai neko-kai merged commit 5e320e3 into zio:master Oct 3, 2020
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.

2 participants