UdpSocket not closed after task return

Hello,
Is it supposed to drop socket after the task return

async fn main() {
  tokio::spawn({
   run_udp_server().await; // after return, socket is still using
 });
 
 do_other_work();
}


async fn run_udp_server() {
    let udp_socket = UdpSocket::bind("0.0.0.0:0")).await.unwrap();
    let udp_socket_clone = udp_socket.clone();
   
    tokio::spawn(async {
       loop {
          handle_udp_data(udp_socket_clone).await.unwrap();
       }
    });
}

You seem to spawn a task (with tokio::spawn) that keeps using the socket in an infinite loop. Isn’t that why it’s kept open?

Also, I’m not sure what exactly your code is doing. Your line

let udp_socket = UdpSocket::bind("0.0.0.0:0")).await.unwrap();

has a parenthesis to much, and if this is tokio’s UdpSocket, that type isn’t even cloneable...

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.