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
Add documentation on Futures APIs and/or release "ClosingFuture" API for resources that must be released/closed #3975
Comments
There is definitely a fundamental issue around releasing resources. I think you might be talking about something slightly different, but bear with me for a moment. Suppose that the transformed future is canceled:
In that case, no code is in a position to release the resource. The best you can hope for is to attach a listener to the transformed future (or perhaps some other future) that closes the resource upon completion. But this isn't necessarily always feasible. To address that problem, @netdpb had done some work internally on a " Again, I'm not 100% sure if you're talking about this problem or something slightly different. But regardless, I believe that your solution (1) solves this by preventing the transformed future from being canceled during that window. However, for Guava, we've tried to be more opinionated about how cancellation works, so we have avoided letting cancellation fail unless the future is actually done. I don't think we'd want to change that here. (IIRC, while it solves the problem in this case, it doesn't solve the problem if the managed resource comes from some prior The best chance we have of doing something that would help you is probably releasing |
Thanks @cpovirk, the problem you describe sounds exactly like what I've observed. The time between |
Next release, hopefully in the not too distant future. We should also still document |
Fantastic! Thanks, Chris. |
This is in 30.0, released Friday. |
Consider a managed resource where a successful
SettableFuture.set
transfers ownership.Where
getManagedResource
uses the return values fromSettableFuture.set
to release the resource:Unfortunately, if the transformed future is canceled while the transforming function is invoked, the managed resource is leaked.
It's unclear if this is something that can be resolved using existing APIs.
The issue is that the transformer may take arbitrarily long, and it's not clear whether the transforming function should bias toward allowing immediate cancellation to allow other listeners to complete quickly, or allow transformations to complete while the transformed future returns
false
fromboolean cancel(boolean mayInterruptIfRunning)
.The documentation mentions some details about cancellation propagation, but might benefit by describing the edge cases.
guava/guava/src/com/google/common/util/concurrent/Futures.java
Lines 463 to 466 in 0281163
A few potential solutions if we decide to make a code change:
AbstractTransformFuture.cancel
delegates toinputFuture.cancel
(requires droppingTrustedFuture
, and waiting to null theinputFuture
reference until after a successful completion). Pro: This avoids the disappearing resource problem. Con: Cancel is not guaranteed to result in immediate completion of the transformed future.Futures
with cancellation behavior matching lazyTransform, allowing callers to choose the desired behavior.What do you think? Feedback is always appreciated, thanks!
The text was updated successfully, but these errors were encountered: