webrtc: signallers: attempt to close the ws when an error occurs

This commit discards the early error returns in the send tasks to log the error
and attempt to close the websocket.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/merge_requests/1435>
This commit is contained in:
François Laignel 2024-01-21 22:09:41 +01:00
parent f54d714afd
commit 91bfd0f7c3
4 changed files with 63 additions and 27 deletions

View file

@ -10,7 +10,7 @@ use std::pin::Pin;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use tokio::io::{AsyncRead, AsyncWrite}; use tokio::io::{AsyncRead, AsyncWrite};
use tokio::task; use tokio::task;
use tracing::{info, instrument, trace, warn}; use tracing::{debug, error, info, instrument, trace, warn};
struct Peer { struct Peer {
receive_task_handle: task::JoinHandle<()>, receive_task_handle: task::JoinHandle<()>,
@ -141,6 +141,7 @@ impl Server {
let this_id_clone = this_id.clone(); let this_id_clone = this_id.clone();
let (mut ws_sink, mut ws_stream) = ws.split(); let (mut ws_sink, mut ws_stream) = ws.split();
let send_task_handle = task::spawn(async move { let send_task_handle = task::spawn(async move {
let mut res = Ok(());
loop { loop {
match tokio::time::timeout( match tokio::time::timeout(
std::time::Duration::from_secs(30), std::time::Duration::from_secs(30),
@ -150,21 +151,28 @@ impl Server {
{ {
Ok(Some(msg)) => { Ok(Some(msg)) => {
trace!(this_id = %this_id_clone, "sending {}", msg); trace!(this_id = %this_id_clone, "sending {}", msg);
ws_sink.send(WsMessage::Text(msg)).await?; res = ws_sink.send(WsMessage::Text(msg)).await;
} }
Ok(None) => { Ok(None) => {
break; break;
} }
Err(_) => { Err(_) => {
trace!(this_id = %this_id_clone, "timeout, sending ping"); trace!(this_id = %this_id_clone, "timeout, sending ping");
ws_sink.send(WsMessage::Ping(vec![])).await?; res = ws_sink.send(WsMessage::Ping(vec![])).await;
} }
} }
if let Err(ref err) = res {
error!(this_id = %this_id_clone, "Quitting send loop: {err}");
break;
}
} }
ws_sink.close().await?; debug!(this_id = %this_id_clone, "Done sending");
Ok::<(), Error>(()) let _ = ws_sink.close().await;
res.map_err(Into::into)
}); });
let mut tx = self.state.lock().unwrap().tx.clone(); let mut tx = self.state.lock().unwrap().tx.clone();

View file

@ -424,6 +424,7 @@ impl Signaller {
let imp = self.downgrade(); let imp = self.downgrade();
let ping_timeout = settings.ping_timeout; let ping_timeout = settings.ping_timeout;
let send_task_handle = task::spawn(async move { let send_task_handle = task::spawn(async move {
let mut res = Ok(());
loop { loop {
match tokio::time::timeout( match tokio::time::timeout(
std::time::Duration::from_secs(ping_timeout as u64), std::time::Duration::from_secs(ping_timeout as u64),
@ -440,26 +441,38 @@ impl Signaller {
serde_json::to_string(&msg).unwrap() serde_json::to_string(&msg).unwrap()
); );
} }
ws_sink res = ws_sink
.send(WsMessage::Text(serde_json::to_string(&msg).unwrap())) .send(WsMessage::Text(serde_json::to_string(&msg).unwrap()))
.await?; .await;
} }
Ok(None) => { Ok(None) => {
break; break;
} }
Err(_) => { Err(_) => {
ws_sink.send(WsMessage::Ping(vec![])).await?; res = ws_sink.send(WsMessage::Ping(vec![])).await;
} }
} }
if let Err(ref err) = res {
if let Some(imp) = imp.upgrade() {
gst::error!(CAT, imp: imp, "Quitting send loop: {err}");
} else {
gst::error!(CAT, "Quitting send loop: {err}");
}
break;
}
} }
if let Some(imp) = imp.upgrade() { if let Some(imp) = imp.upgrade() {
gst::info!(CAT, imp: imp, "Done sending"); gst::debug!(CAT, imp: imp, "Done sending");
} else {
gst::debug!(CAT, "Done sending");
} }
ws_sink.close().await?; let _ = ws_sink.close().await;
Ok::<(), Error>(()) res.map_err(Into::into)
}); });
let imp = self.downgrade(); let imp = self.downgrade();

View file

@ -278,14 +278,15 @@ impl Signaller {
let (ws_sender, mut ws_receiver) = mpsc::channel::<OutgoingMessage>(1000); let (ws_sender, mut ws_receiver) = mpsc::channel::<OutgoingMessage>(1000);
let send_task_handle = let send_task_handle =
RUNTIME.spawn(glib::clone!(@weak-allow-none self as this => async move { RUNTIME.spawn(glib::clone!(@weak-allow-none self as this => async move {
let mut res = Ok(());
loop { loop {
tokio::select! { tokio::select! {
opt = ws_receiver.next() => match opt { opt = ws_receiver.next() => match opt {
Some(msg) => { Some(msg) => {
gst::log!(CAT, "Sending websocket message {:?}", msg); gst::log!(CAT, "Sending websocket message {:?}", msg);
ws_sink res = ws_sink
.send(WsMessage::Text(serde_json::to_string(&msg).unwrap())) .send(WsMessage::Text(serde_json::to_string(&msg).unwrap()))
.await?; .await;
}, },
None => break, None => break,
}, },
@ -301,22 +302,29 @@ impl Signaller {
transaction, transaction,
session_id, session_id,
}); });
ws_sink res = ws_sink
.send(WsMessage::Text(serde_json::to_string(&msg).unwrap())) .send(WsMessage::Text(serde_json::to_string(&msg).unwrap()))
.await?; .await;
} }
} }
} }
if let Err(ref err) = res {
this.as_ref().map_or_else(|| gst::error!(CAT, "Quitting send task: {err}"),
|this| gst::error!(CAT, imp: this, "Quitting send task: {err}")
);
break;
}
} }
let msg = "Done sending"; this.map_or_else(|| gst::debug!(CAT, "Done sending"),
this.map_or_else(|| gst::info!(CAT, "{msg}"), |this| gst::debug!(CAT, imp: this, "Done sending")
|this| gst::info!(CAT, imp: this, "{msg}")
); );
ws_sink.close().await?; let _ = ws_sink.close().await;
Ok::<(), Error>(()) res.map_err(Into::into)
})); }));
let recv_task_handle = let recv_task_handle =

View file

@ -140,21 +140,28 @@ impl Signaller {
let (websocket_sender, mut websocket_receiver) = mpsc::channel::<p::IncomingMessage>(1000); let (websocket_sender, mut websocket_receiver) = mpsc::channel::<p::IncomingMessage>(1000);
let send_task_handle = let send_task_handle =
RUNTIME.spawn(glib::clone!(@weak-allow-none self as this => async move { RUNTIME.spawn(glib::clone!(@weak-allow-none self as this => async move {
let mut res = Ok(());
while let Some(msg) = websocket_receiver.next().await { while let Some(msg) = websocket_receiver.next().await {
gst::log!(CAT, "Sending websocket message {:?}", msg); gst::log!(CAT, "Sending websocket message {:?}", msg);
ws_sink res = ws_sink
.send(WsMessage::Text(serde_json::to_string(&msg).unwrap())) .send(WsMessage::Text(serde_json::to_string(&msg).unwrap()))
.await?; .await;
if let Err(ref err) = res {
this.as_ref().map_or_else(|| gst::error!(CAT, "Quitting send loop: {err}"),
|this| gst::error!(CAT, imp: this, "Quitting send loop: {err}")
);
break;
}
} }
let msg = "Done sending"; this.map_or_else(|| gst::debug!(CAT, "Done sending"),
this.map_or_else(|| gst::info!(CAT, "{msg}"), |this| gst::debug!(CAT, imp: this, "Done sending")
|this| gst::info!(CAT, imp: this, "{msg}")
); );
ws_sink.close().await?; let _ = ws_sink.close().await;
Ok::<(), Error>(()) res.map_err(Into::into)
})); }));
let obj = self.obj(); let obj = self.obj();