-
Notifications
You must be signed in to change notification settings - Fork 1.9k
#1044 follow up for CompletableDeferred #1092
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
This one works on a higher level of abstraction, so, for example, it can be fixed in the following way:
I am afraid we can't propose a better solution because To make it a less painful you can extract the extension like |
@qwwdfsad Thanks for the hint did not thought that I could catch the cancellation exception on the await. Since I do not want to close all the time in a finally as it's executed all the time and I actually need the resource when not cancelled, I'm forced to do as below to not introduce nullability.
A little ugly but seems to work. Don't know if an awaitOrExecute default extension have it's place in the default coroutine for that kind of things? |
Yes, catching
I am afraid that it is not the best fit (though I will give it some thought) for the core library. Another concern is that now it implies implicit ownership that is prone to data-races: what if two threads call
it is a memory leak which depends on timing now as no one is longer responsible for closing this resource. If we are going to introduce ownership mechanism, it should be robust (otherwise it is not applicable for general use), simple and it should be possible to "own" the exceptional result as well, so every exception will be delivered. |
Thanks for the details, I did not thought about multiple await on the same deferred :( Just to be sure: given the new
a call like |
Yes if you don't have suspending paths in
then you have to move |
Then I'm lost I still have some rare leaks :( Given https://gist.github.com/Tolriq/d3df040e878da0ca8aa048bdb2b19855 And simple call
Still triggers strict mode leaks, meaning there's still one case of cancellation I do not handle |
I can try to help if you will provide any kind of stable reproducer; |
@qwwdfsad It's basically all the code in previous post. When looking at the source of the leaks I tried to builds tests to reproduce and was unable to find a proper way to tests for leaks outside of strict mode. But can't make strict mode work in unit tests :( It can be an application specific logic but it's tied to coroutines and some cancellation logic that I miss :( As I did not have issues with simpler threading solutions before and I'm sure to close resources at the correct points if they are reached. During analysis of this leak I found 1 leak in OkHttp, #1044 and this one, all are now fixed, but I fear this is the same kind of thing as this one. |
The gist is not enough because it is no self-contained, so I can't help you with that. Is this problem reproducible with |
Still happens in 1.2.1 but a little harder to get than before. The gist is complete except the creation of the closable that is application specific but anyway as I said I'm more than willing to build tests and repro for that but I need some help about how to do test that chase leaks :( |
Any chance for a reproducer? Even if it is not small. Can you, maybe, open source some example Android app that would be leaking sometimes? |
App is very large and closed source, so thought that time would be better spent writing the tests. But if you can't give a startup test for that, then I can try to build a small app to reproduce and detect via Android Strict mode. |
Yes, it definitely would help us to troubleshoot your issue |
Sorry for the delay real life is a b... and I tried to do a small repro. Here it is https://github.com/Tolriq/coroutines-closeable based on OkHttp / MockWebServer Start the app and press the FAB, might require quite a few tries or delay adjustments (on a physical device Nokia 8 Android 9 takes less than 5 attempts). The code is ugly so don't spam the FAB Android won't digest all the objects creations :) At some point the app will crash from Strict Mode with log:
|
Are you able to reproduce or do you want that I try to find something else? |
Another small bump in case it's 1.3 material and to have the "waiting for clarification" tag removed :) |
It's a semantic bug in your sample project. Let's take a look at
By But consider the following scenario:
The simplest fix is to bind lifecycles of deferred and its owner: https://gist.github.com/qwwdfsad/e92bef89b1a8311576f1c38a9f90cc61 |
@qwwdfsad Thanks a lot for the insights and details. All makes sense even if most samples about worker pool does not take this in account. Can you just explain the change:
I'm not sure to grasp what this change brings or it's impact. |
This change is just a cosmetic. |
Thanks, that's what I thought but you made me doubt about lower level impacts. I confirm that this fixes my issue in production app too on the few resources workerpool patterns I had, thanks a lot. A sample / doc about closable worker pool would be nice as I would have never found the cause without your help. It's really hard to grasp all the internal places where cancellation can occurs and their impacts. |
I got one more case with Closable resources :(
Take the following example of a typical worker pool.
If the task return a Closable, there's a possibility that the await does not return but that task is completed, leading to leaked resources.
Same issue as before but I suppose would also require something for the resume of CompletableDeferred or maybe I'm missing a better way to achieve that?
cc @elizarov
The text was updated successfully, but these errors were encountered: