snapview / tokio-tungstenite

Future-based Tungstenite for Tokio. Lightweight stream-based WebSocket implementation
MIT License
1.88k stars 236 forks source link

Consider closing stream if reader was dropped #300

Closed Pzixel closed 1 year ago

Pzixel commented 1 year ago

Consider following code:

use futures_util::SinkExt;
use futures_util::StreamExt;
use tokio_tungstenite;

let Ok((ws_stream, _)) = tokio_tungstenite::connect_async(url).await.unwrap();
let (mut write, mut read) = ws_stream.split();
drop(read);
write.send(tokio_tungstenite::tungstenite::Message::Text("Hello").await.unwrap();

It executed without errors. Meanwhile channels notify writing end if reading end was closed:

use std::sync::mpsc::channel;

fn main() {
    let (tx, rx) = channel();
    drop(rx);
    tx.send(1).unwrap(); // panic on err unwrap
}

I think that in most (all?) cases when reading end of WS connection was closed there is no point of keeping writing end alive and writes there should return error. Right now users are likely to implement some sort of let mut is_reader_closed: AtomicBool which isn't ideal.

daniel-abramov commented 1 year ago

These are different things: