-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
fix asFlow blocking thread #3980
fix asFlow blocking thread #3980
Conversation
This PR does not respect the contribution guidelines (https://github.com/Kotlin/kotlinx.coroutines/blob/master/CONTRIBUTING.md#submitting-prs), but more importantly, it's also not the change we'd like to introduce, as this can easily be expressed already by applying |
Having to apply Shall we still integrate the UNLIMITED buffer somewhere? |
Among other things, |
I think this situation is very much "choosing your poison": either blocking the thread or consuming more memory. The BehaviorSubject in Rx has chosen the latter. IMO,
|
I agree that Rx2 uses unbounded buffers in all non-back-pressured streams. It's OOM-susceptible by nature. I feel that using an unlimited buffer on Rx <-> coroutines integration in those cases is not adding much more risk, given the already existing OOM risk from Rx. On the other hand, having a limited buffer can easily cause thread-locking issues. Example:
This code will block the thread at We can easily see that happening by looking at the implementation:
I have used a direct dispatcher and a rendezvous channel for demonstration purposes, but this scenario can still happen in single-thread context / immediate dispatchers, such as |
Related to #3282 |
Likewise, if there was no blocking behavior, your message could be rephrased as "having to apply
What's good about these contexts is that usually, the problem is visible immediately during development and can be fixed. On the other hand, an OOM can be hidden, silently sneaking into production. |
@dkhalanskyjb I think it's fair to notice what I believe is an inconsistency in the way kotlinx.coroutines handles
|
@psteiger, here are the incompatible things:
There are two intended use cases for When converting an RxJava entity to a kotlinx-coroutines entity, we must introduce some inconsistency: either between a typical Luckily, as stated in #3979 (comment), the inconsistency between |
@dkhalanskyjb Thanks for sharing the design details, and overall I find them reasonable. But I still maintain the position for some change here (either in impl, or in docs).
No one wants to fail with a deadlock, but unfortunately it's far too easy to use If the end decision is to keep the implementation, I'd call for an emphasized warning on |
Thanks w
…On Sat, 9 Mar 2024, 5:33 am Patrick Steiger, ***@***.***> wrote:
@dkhalanskyjb <https://github.com/dkhalanskyjb> Thanks for sharing the
design details, and overall I find them reasonable. But I still maintain
the position for some change here (either in impl, or in docs).
If you want to change your failure mode, you have a way to do so.
No one wants to fail with a deadlock, but unfortunately it's far *too
easy* to use ObservableSource.asFlow() in specific contexts without
realizing the risk. It's a default that is dangerous in specific contexts;
versus some other potential default that is safe against deadlocks in all
contexts.
If the end decision is to keep the implementation, I'd call for an
emphasized warning on asFlow docs on which particular cases asFlow usage
can be dangerous.
—
Reply to this email directly, view it on GitHub
<#3980 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BFURID3DZB2VKY22S7I4MCDYXJKD3AVCNFSM6AAAAABANLELSWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSOBWGYYDAOBQGQ>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Fixes the issue #3979
Changes the ChannelFlow's buffer to UNLIMITED when asFlow() is called