-
Notifications
You must be signed in to change notification settings - Fork 1.9k
invokeOnCompletion
doesn't provide thread guarantees and doesn't allow to suspend
#3505
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
NB the exact |
Thanks for the detailed explanation! I have a few questions about your expectations from such an API:
Do you expect the code from
Do you expect this code to prevent any job from completion? Do you expect it to be cancellable and, if so, how to cancel it?
It seems like you expect 2.1)
Where this 2.2) Now a bit trickier:
The same question applies. 2.3) Now consider a much more tricker case:
What is the expected behaviour here? These questions expose some flaws in the design "let invokeOnCompletion be suspendable and inherit the dispatcher", but I do not expect a sound solution for them; I'm rather interested in your expectations in these examples if the proposed solution is implemented as is, so it can help us to shape the API towards more sound form, while still solving the problems you are solving |
I'd expect the lambda to be executed after the job is in Completing or Cancelling state, before it's Completed or Cancelled.
I'd expect the lambda to be non-cancellable, It's the same as in a regular completion handler one can block the thread forever, so non-cancellable is ok if properly documented. It's the responsibility of the client to not suspend indefinitely. If a child job of the lambda fails, then the lambda should be cancelled as well (as per normal cancellation propagation to the parent).
I'd expect the completion handler to be executed in a scope (not on a job) inheriting the dispatcher from the scope context (or Dispatchers.Default if unspecified). |
A related problem reported via YouTrack (a use-case of cleaning up resources in a cancelled coroutine): https://youtrack.jetbrains.com/issue/KT-60182 |
Note to self: the above-linked YouTrack issue was my own. |
Currently,
invokeOnCompletion
does not provide any guarantees about the dispatcher, sohide
is invoked on a random thread.I cannot do the following because
show()
might block EDT and spin an inner event loop, thus making it to theawaitCancellation
call only until after the popup is closed by the user from the UI:Currently I have to do this:
I'd rather have a convenient
invokeOnCompletion
(#3259) but with convenient threading and with ability to suspend. Basically, I'm asking for an equivalent of:The text was updated successfully, but these errors were encountered: