-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Proper handling of closable resources after #896 #1044
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
Comments
Interesting use-case. Good that we have it now. There is only non-public (internal) APIs for that purpose now and they are not quite convenient to use. First internal API is
So, there's another internal API that solves that problem. Called We definitely need some public API. But what kind of API -- that is the question. For example, we can add a separate Would it help? (The name is not helpful, though. I wonder how to name it so that it is clear what it does). |
Now, to continue discussion. The problem with
Since resume is atomic, then
So alternative design would be a two argument version of
How to name this |
I was answering previous post and you confirmed that the atomic or a forced delivery was not good as it's easy to forget the cancellation check on caller side. Love the last proposal, maybe |
It sends a wrong message that other, plain
|
Yep simple and clear, On what context / scope would the callback be called? The resume side, the await side or something else? |
Here's the catch. Just like the family of |
Fine for me as my usage is mainly for database cursors and okHttp response. Just need the doc to clearly state that, and I suppose one can dispatch the closing to Dispatchers.IO if necessary for a blocking close. |
If you'll need to something long blocking op, you can always do |
Wouldn't using someCall().await().use { result ->
updateUIAfter(result)
} at the consumption site ensure |
The resources does not reach the await if there's cancellation during resume. Last proposal is not closeable specific, it just allows to ensure that either the await is successfully receiving the result and can actually do call use or that a callback is called to do whatever may be needed to do if the await receive a cancelled. (And in this case call the close but could also be anything else like notifying something waiting elsewhere or whatever). |
I'm addressing #1044 (comment). I'm aware of the problem as you know. |
Ok you missed the last proposal: #1044 (comment) |
I didn't. That addresses the problem at the publish site only, not the consumption site where there's a section suspension and cancelation. |
Then I do not understand your
There's nothing related to that now, and the cancellation after await was always possible even before #896, Once the await success it have always been the consumer job to properly check for cancellation and do needed stuff there's was no magic ignoring/changing messages unless you call another suspending mechanism that automatically check for cancellation. So really don't understand your comments. |
There were comments suggesting special casing closeable to which I expressed my distaste for. There were comments about the consumption site needing to be careful about suspension between receiving the resource and closing it to which I expressed that |
Ok, but I thought those proposal where out of choice after the previous exchanges, that's why I did not see any correlation with the last proposal. The consumption side issue was not about the closing of the ressources when cancellation occurs but the fact that await could return a value that you may want to use when the coroutine is cancelled and so the context could be invalid like destroyed activity / fragment. |
@elizarov with 1.2 in the work and this not tagged for 1.3 can you expand a little on your first explanation of the 3 internals functions so that I can use that in the meantime? I'm not really sure how I should merge the 3 in the example from first post. |
* Allows safe return of closeable resources from suspending functions, as it provides a way to close a resource if the corresponding job was cancelled. * Documentation on the context and expected behavior of CompletionHandler implementations is updated. Fixes #1044
* Allows safe return of closeable resources from suspending functions, as it provides a way to close a resource if the corresponding job was cancelled. * Documentation on the context and expected behavior of CompletionHandler implementations is updated. Fixes #1044
This is a follow up of #896 that started on slack.
Let's take the following example.
How can we ensure that the resume from
onResponse()
is either successful or that we can close the response.Adding isActive tests is race condition prone, and I can't think of a way to avoid a race condition leading to leaks of the response.
cc: @elizarov
The text was updated successfully, but these errors were encountered: