Skip to content

Resource runs its finalizers when timed out#4617

Open
fommil wants to merge 1 commit into
typelevel:series/3.xfrom
fommil:resource-override-timeout
Open

Resource runs its finalizers when timed out#4617
fommil wants to merge 1 commit into
typelevel:series/3.xfrom
fommil:resource-override-timeout

Conversation

@fommil

@fommil fommil commented Jun 16, 2026

Copy link
Copy Markdown

This fixes #4489 that was introduced by #4059 using an approach documented by @durban in #4489 (comment) and admitted reluctantly by @djspiewak in #4489 (comment) who suggests that there is perhaps a more general solution by rethinking Resource itself.

@djspiewak

djspiewak commented Jun 20, 2026

Copy link
Copy Markdown
Member

Can we make this so that it doesn't regress #4059? The reason timeout is implemented in terms of racePair in the first place is it's possible for the effect to complete at the exact moment of the timeout, and in that case, the value can be leaked because the finalizers on the value side have already popped. Something like this comes to mind:

IO.ref(false) flatMap { fired =>
  val program = Resource.eval(IO.sleep(100.millis).onCancel(fired.set(true)).timeout(100.millis).use_

  program.attempt flatMap {
    case Left(()) => program  // worked but we didn't reproduce the error
    case Right(_) => fired.get.flatMap(f => IO(assert(f == true)))
  }
}

The above will almost certainly fail with your fix, indicating a memory leak in the case where the timeout executes but we don't run the finalizer. (also note the above is a handwave due to the way I encoded the repetition; with a proper implementation it would loop forever, so this isn't a real unit test)

Edit: Additionally, this only fixes half of the problem. We also need to address the bug in start (described here: #4489 (comment))

@mergify

mergify Bot commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@djspiewak

Copy link
Copy Markdown
Member

Oh, secondarily: can we retarget this at series/3.7.x please? That will allow us to release it faster. It's also fine if you need to target it at series/3.6.x. We're almost certainly going to do another release in that lineage.

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.

Possible Issue with Timeout in Cats-Effect 3.6.x which resulted in Http4s Connection leaks

2 participants