Skip to content

Commit ea3af36

Browse files
authored
fix panic on async task cancellation (#570)
1 parent efdb22e commit ea3af36

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

sctp/src/association/mod.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ impl Association {
488488
let done = Arc::new(AtomicBool::new(false));
489489
let name = Arc::new(name);
490490

491-
while !done.load(Ordering::Relaxed) {
491+
'outer: while !done.load(Ordering::Relaxed) {
492492
//log::debug!("[{}] gather_outbound begin", name);
493493
let (packets, continue_loop) = {
494494
let mut ai = association_internal.lock().await;
@@ -512,9 +512,8 @@ impl Association {
512512
// Doing it this way, tokio schedules this work on a dedicated blocking thread, this future is suspended, and the read_loop can make progress
513513
match tokio::task::spawn_blocking(move || raw.marshal_to(&mut buf).map(|_| buf))
514514
.await
515-
.unwrap()
516515
{
517-
Ok(mut buf) => {
516+
Ok(Ok(mut buf)) => {
518517
let raw = buf.as_ref();
519518
if let Err(err) = net_conn.send(raw.as_ref()).await {
520519
log::warn!("[{}] failed to write packets on net_conn: {}", name2, err);
@@ -527,9 +526,21 @@ impl Association {
527526
buf.clear();
528527
buffer = Some(buf);
529528
}
530-
Err(err) => {
529+
Ok(Err(err)) => {
531530
log::warn!("[{}] failed to serialize a packet: {:?}", name2, err);
532531
}
532+
Err(err) => {
533+
if err.is_cancelled() {
534+
log::debug!(
535+
"[{}] task cancelled while serializing a packet: {:?}",
536+
name,
537+
err
538+
);
539+
break 'outer;
540+
} else {
541+
log::error!("[{}] panic while serializing a packet: {:?}", name, err);
542+
}
543+
}
533544
}
534545
//log::debug!("[{}] sending {} bytes done", name, raw.len());
535546
}

0 commit comments

Comments
 (0)