Skip to content

Commit

Permalink
fix panic on async task cancellation (#570)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmorer authored May 18, 2024
1 parent efdb22e commit ea3af36
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions sctp/src/association/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ impl Association {
let done = Arc::new(AtomicBool::new(false));
let name = Arc::new(name);

while !done.load(Ordering::Relaxed) {
'outer: while !done.load(Ordering::Relaxed) {
//log::debug!("[{}] gather_outbound begin", name);
let (packets, continue_loop) = {
let mut ai = association_internal.lock().await;
Expand All @@ -512,9 +512,8 @@ impl Association {
// Doing it this way, tokio schedules this work on a dedicated blocking thread, this future is suspended, and the read_loop can make progress
match tokio::task::spawn_blocking(move || raw.marshal_to(&mut buf).map(|_| buf))
.await
.unwrap()
{
Ok(mut buf) => {
Ok(Ok(mut buf)) => {
let raw = buf.as_ref();
if let Err(err) = net_conn.send(raw.as_ref()).await {
log::warn!("[{}] failed to write packets on net_conn: {}", name2, err);
Expand All @@ -527,9 +526,21 @@ impl Association {
buf.clear();
buffer = Some(buf);
}
Err(err) => {
Ok(Err(err)) => {
log::warn!("[{}] failed to serialize a packet: {:?}", name2, err);
}
Err(err) => {
if err.is_cancelled() {
log::debug!(
"[{}] task cancelled while serializing a packet: {:?}",
name,
err
);
break 'outer;
} else {
log::error!("[{}] panic while serializing a packet: {:?}", name, err);
}
}
}
//log::debug!("[{}] sending {} bytes done", name, raw.len());
}
Expand Down

0 comments on commit ea3af36

Please sign in to comment.