Skip to content

Fix resource leak when take is cancelled - #709

Open
stasimus wants to merge 1 commit into
typelevel:mainfrom
stasimus:fix-take-cancel-leak
Open

stasimus wants to merge 1 commit into
typelevel:mainfrom
stasimus:fix-take-cancel-leak

Conversation

@stasimus

Copy link
Copy Markdown

take took the pooled resource out of the map with Resource.eval, which leaves a cancellation point before the bracket registers its finalizer. A cancel there dropped the resource silently. Moving the update into the acquire makes it atomic. Test is JVM only, the race needs real parallelism.

Fixes #708

The pooled resource came out of the map via Resource.eval, which leaves a
cancellation point before makeFull registers the finalizer. A cancel landing
there dropped the resource without destroying it or returning it to the pool,
so the permit and the idle count stayed correct while the connection leaked.

Moving the map update into the acquire makes taking the resource and
registering its finalizer atomic.

Fixes typelevel#708
@stasimus

stasimus commented Sep 18, 2026

Copy link
Copy Markdown
Author

Root cause is upstream, typelevel/cats-effect#4627: Resource.eval has an extra cancellation point at the end despite documenting that it preserves interruptibility. Wrapping the map update in uncancelable doesn't help, it still leaks, so the point is after the eval returns. Hence restructuring rather than masking.

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.

take can silently drop a pooled resource when cancelled

1 participant