From bd17c8e5e090692c3057fd362dd79412a7e4b722 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Thu, 5 Oct 2023 09:32:38 +0700 Subject: [PATCH] Cancel certain async callback on disconnect --- .../core/src/bc_protocol/connection/bcconn.rs | 21 +++++++++++-------- src/common/streamthread.rs | 4 ++-- src/rtsp/mod.rs | 11 +++++++--- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/crates/core/src/bc_protocol/connection/bcconn.rs b/crates/core/src/bc_protocol/connection/bcconn.rs index ba6b5c9..9388460 100644 --- a/crates/core/src/bc_protocol/connection/bcconn.rs +++ b/crates/core/src/bc_protocol/connection/bcconn.rs @@ -232,6 +232,8 @@ struct Poller { impl Poller { async fn run(&mut self) -> Result<()> { + let cancel = CancellationToken::new(); + let _dropguard = cancel.clone().drop_guard(); while let Some(command) = self.reciever.next().await { // Clean Up subscribers self.subscribers @@ -270,12 +272,18 @@ impl Poller { // Move this on another thread coz I have NO idea // how long the callback will run for // and we must NOT hang + let cancel = cancel.clone(); tokio::task::spawn(async move { - if let Some(reply) = occ(&response).await { - assert!(reply.meta.msg_num == response.meta.msg_num); - sink.send(Ok(reply)).await?; + tokio::select! { + _ = cancel.cancelled() => Result::Ok(()), + v = occ(&response) => { + if let Some(reply) = v { + assert!(reply.meta.msg_num == response.meta.msg_num); + sink.send(Ok(reply)).await?; + } + Result::Ok(()) + } } - Result::Ok(()) }); log::trace!("Called ID callback"); } @@ -321,10 +329,7 @@ impl Poller { &msg_id ); } - let sender = sender.clone(); - // tokio::task::spawn(async move { let _ = sender.send(Ok(response)).await; - // }); } else { debug!( "Ignoring uninteresting message id {} (number: {})", @@ -383,11 +388,9 @@ impl Poller { occ_entry.insert(tx); } else { // log::error!("Failed to subscribe in bcconn to {:?} for {:?}", msg_num, msg_id); - // tokio::task::spawn(async move { let _ = tx .send(Err(Error::SimultaneousSubscription { msg_num })) .await; - // }); } } }; diff --git a/src/common/streamthread.rs b/src/common/streamthread.rs index 33c98b1..a0f5fd4 100644 --- a/src/common/streamthread.rs +++ b/src/common/streamthread.rs @@ -401,9 +401,8 @@ impl StreamData { AnyResult::Ok(()) }, v = async { - watchdog_rx.recv().await; // Wait forever for the first feed + let mut check_timeout = timeout(Duration::from_secs(15), watchdog_rx.recv()).await; // Wait lonfer for the first feed loop { - let check_timeout = timeout(Duration::from_secs(3), watchdog_rx.recv()).await; if let Err(_)| Ok(None) = check_timeout { // Timeout // Reply with Ok to trigger the restart @@ -411,6 +410,7 @@ impl StreamData { sleep(Duration::from_secs(1)).await; break AnyResult::Ok(()); } + check_timeout = timeout(Duration::from_secs(4), watchdog_rx.recv()).await; } } => v, result = instance.run_passive_task(|camera| { diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index 95577c2..d95a6ea 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -1055,7 +1055,9 @@ async fn make_factory( // Need and enough are fired when we // call push_buffer which is inside an // async context so blocking_send is forbiddon - tokio::task::spawn(vid_enough_tx.send(StreamData::Enough { ts })); + tokio::task::spawn(async move { + vid_enough_tx.send(StreamData::Enough { ts }).await + }); }) .need_data(move |_, _| { log::debug!("need_data:"); @@ -1064,7 +1066,9 @@ async fn make_factory( // Need and enough are fired when we // call push_buffer which is inside an // async context so blocking_send is forbiddon - tokio::task::spawn(vid_need_tx.send(StreamData::Need { ts })); + tokio::task::spawn(async move { + vid_need_tx.send(StreamData::Need { ts }).await + }); }) .build(), ); @@ -1205,9 +1209,10 @@ async fn handle_data> + Unpin, E>( last_ft = Some(ft_i); // Sync ft to rt if > 1500ms difference - const MAX_DELTA_T: Duration = Duration::from_millis(1500); + const MAX_DELTA_T: Duration = Duration::from_millis(200); let delta_t = if rt > ft { rt - ft } else { ft - rt }; if delta_t > MAX_DELTA_T { + log::debug!("delta_t > MAX_DELTA_T = {delta_t:?}, {MAX_DELTA_T:?}"); ft = rt; }