Skip to content

Cancel safety auditΒ #203

@k0nserv

Description

@k0nserv

When writing futures that are likely to run in race with others using e.g. tokio::select! it's important to consider cancel safety.

A typical pattern that isn't cancel safe is

async fn read_parsed<T>() -> Result<T> 
    where T: TryFrom<Vec<u8>>
{
    let mut buf = vec![0; 100];
    let n = read_io(&mut buf).await?;
    buf.truncate(n);
    
    let parsed = T::try_from(buf)?;
    
    // If the future is dropped here the data has still been read from some I/O device
    // but the buffer and parsed result are deallocated and never returned to the caller.
    some_other_thing(&parsed).await?;
    
    Ok(parsed)
}

We should audit the codebase for patterns like this and either: make the futures cancel safe or at least document the fact that they aren't.

I've discovered three cases so far:

We might able to utilise Codec from tokio-utils in some instances to help with this.

Crates

Here's a list of all the crates and their audit status for cancel saftey

  • rtp
  • rtcp
  • sctp
  • data
  • ice
  • dtls
  • mdns
  • media
  • interceptor
  • webrtc
  • sdp
  • stun
  • turn
  • util

More reading about cancel safety

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions