-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Stream.asBroadcastStream - Easy to cause leaks, what is the rationale? #26686
Comments
tl;dr: Because that's how broadcast streams work, and you might be using them for something they are not intended for. Broadcast streams are intended to be streams that exist independently of their listeners. That's where the name comes from - they broadcast events into the "ether" whether anybody is listening or not. Canceling the last listener on a broadcast stream doesn't actually mean anything. Broadcast streams are intended for cases like DOM event handlers, where each event can be handled individually, it doesn't matter if you miss some of them, so you can start (and stop) listening at any point. Normally, the code that controls the stream will know how to handle the lack of listeners. Code using a broadcast For a broadcast stream created by calling The We have the problem that broadcast streams, and in particular ones created using If you want two streams that has the same events as one original stream, then you can use https://www.dartdocs.org/documentation/async/1.11.0/async/StreamSplitter-class.html from If you want to have different listeners handle the events over time, you can either keep using the same subscription and changing the callback, or use the https://www.dartdocs.org/documentation/async/1.11.0/async/StreamQueue-class.html class, also from |
Closing with "works as intended". Feel free to re-open if I'm mistaken. |
That was an excellent explanation, thank you. Sounds very reasonable. |
As detailed a bit in the original commit (1315656), Stream#asBroadcastStream has behavior in which:
The only way to close the underlying subscription is to receive a callback in
asBroadcastStream
:Internal app ran into a big memory leak because of this, so I'm just wondering what the rationale is for making it so easy to leak.
@floitschG @lrhn @cbracken
The text was updated successfully, but these errors were encountered: