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 Scope. #3473

Merged
merged 4 commits into from
Sep 10, 2024
Merged

Fix resource leak in Scope. #3473

merged 4 commits into from
Sep 10, 2024

Conversation

ValdemarGr
Copy link
Contributor

I minimized a bug I had to this.

IO.ref(0).flatMap { ref =>
  val res = Stream.resource(Resource.make(ref.update(_ + 1))(_ => ref.update(_ - 1)))

  (0 to 100000).toList.parTraverse_ { _ =>
    val fa = res.evalMap(_ => IO.sleep(10.millis)).take(10).compile.drain
    IO.race(fa, IO.sleep(90.millis))
  } >> ref.get.map(assertEquals(_, 0))
}

The Ref will sometimes not contain 0. I tried the same test with the following fix and it does not occur anymore.

Explanation (or rather my hypothesis)

If cancellation of the stream occurs between state.modify and the subsequent flatMap then the scope's state is Closed, thus when the root scope is cancelled and traverses the scope tree, the node is now Closed, but the finalizers for the attached resources were never run.

I can't seem to come up with a good test for this.

@mpilquist
Copy link
Member

Thanks! Fix looks good. Would really like a test case for this though. Maybe something based on your minimization?

@ValdemarGr
Copy link
Contributor Author

ValdemarGr commented Sep 10, 2024

I've added a test.

I tried to use a seeded TestControl and make it property based, but it was still flaky and much slower.

@mpilquist mpilquist merged commit eea0c25 into typelevel:main Sep 10, 2024
15 checks passed
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