Resource: avoid leaking Queue based Finalizer details#1137
Merged
Conversation
hearnadam
commented
Apr 11, 2025
Collaborator
Author
Flaky test... |
fwbrasil
reviewed
Apr 12, 2025
|
|
||
| def ensure(v: => Any < (Async & Abort[Throwable]))(using Frame): Unit < IO = | ||
| IO.Unsafe { | ||
| if queue.offer(IO(v.unit)).isFailure then |
Collaborator
There was a problem hiding this comment.
This seems to ignore panics. I think JCTools can't panic for an offer but it'd be nice to cover the scenario since we might change the underlying impl at some point
Collaborator
Author
There was a problem hiding this comment.
Ah good point, I will update.
fwbrasil
reviewed
Apr 12, 2025
| .map(promise.becomeDiscard) | ||
| } | ||
|
|
||
| def await(using Frame): Unit < Async = promise.get |
Collaborator
There was a problem hiding this comment.
nice! I think this essentially implements proper back pressure?
fwbrasil
reviewed
Apr 12, 2025
| def ensure(v: => Any < (Async & Abort[Throwable]))(using Frame): Unit < IO | ||
|
|
||
| object Finalizer: | ||
| sealed abstract class Closeable extends Finalizer: |
fwbrasil
reviewed
Apr 12, 2025
| val closeable = Finalizer.Closeable.Unsafe.init(closeParallelism) | ||
| ContextEffect.handle(Tag[Resource], closeable, _ => closeable)(v) | ||
| .handle(IO.ensure(closeable.close)) | ||
| .map(result => closeable.await.andThen(result)) |
Collaborator
There was a problem hiding this comment.
another option is returning a Fiber so use can be used. Not sure it's worth it
Collaborator
Yeah, I've looked into these failures a couple of times. I suspect Monix has some race condition with interrupts |
fwbrasil
reviewed
Apr 14, 2025
Co-authored-by: Flavio Brasil <fwbrasil@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Finalizercould be implemented a number of ways. Exposing theQueuemakes the implementation hard to change. Additionally, this makes it more complex when we support hierarchical scopes in #1131.Solution
Use an
abstract class, exposing only the necessary behaviors. In a followup, I will work towards a rename toScope(optional?), then adding support forScope.forkor some similar naming.