-
Notifications
You must be signed in to change notification settings - Fork 1.9k
collectLatest does not cancel runInterruptible if dispatchers are the same #3109
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
Thanks for the report! The problem is straightforward -- It is a perfectly sound behaviour for the For example, it's easy to have the following in the project:
And with the change that fixes the current behaviour, the code will do up to Taking it into the account, I tend to think the behaviour is "as designed", but I'm open to other opinions. Could you please elaborate on how serious is this issue for your use case? Was it hard to find and/or surprising? |
In my app I have a feature to download some files from network. It is implemented by having It worked perfectly fine, because collecting coroutine was launched in a shared single thread dispatcher. But after some refactoring this dispatcher was changed to I tried to add additional dispatch doing I think it's at least worth mentioning somewhere in docs about this behaviour - that if dispatchers are the same, then block will be started undispatched. |
Looks like |
It actually attempts to do so except the very first emit, see: Fixing this may lead to potentially (?) unexpected behaviour in single-threaded dispatchers (namely, |
Would this really be unexpected? If we want the latest things (and we are collecting the latest)—why waste our time with things that aren't the latest? An argument can be made though that a hypothetical |
Hey. I'm facing this issue. In my use case I'm dynamically searching for what user is typing into text input. I'm processing search with Personally |
There is also 2 things I noticed from my example below (kotlin 1.7.10):
Snippet:
Here is an output
|
It seems to me the issue here is with the The thread blocking behavior is what causes this when the same dispatcher is used. Couldn't you just add a flag to the invocation to allow / disallow undispatched behavior so that the programmer can pick the right one for the use case? |
I expect this code to print
start 1
start 2
finish interruptible
But instead it prints
start 1
finish interruptible
exception java.lang.Exception: unexpected
The text was updated successfully, but these errors were encountered: