How to send instruction to Break from socket.recv_from().await;

In my program I need to break from the loop and from the synchronous operation thread_socket.recv_from(&mut buf).await; (see here), how could I achieve this?

The program is expecting another input but none will be ever sent because the job is done (all packets have been received). I tried to break from the different loop before realizing that (probably) the reason why it is not proceeding further is because of being stuck at this particular line. I don't really want a timeout but more like a goto from line 200 to line 266.

I have also tried to send a custom random packet with echo -n -e '\x00\x01\x00\x45\x01\x00\x00' >/dev/udp/localhost/8080 but I would get a panic error since I have already broke apart from the task::spawn, confirming that the break of line 201 happened successfully.

Thank you very much for any advice!

Sorry, I haven't written a lot of tokio or async rust lately, so apologies if this is inaccurate.

I think the easiest thing you can do is replace this:

let result = thread_socket.recv_from(&mut buf).await;

with something like this:

let result = tokio::select! {
  done = &mut _debouncer => {
    break;
  }
  result = thread_socket.recv_from(&mut buf) => {
    result
  }
};

What select does is poll both the socket and the debouncer task simultaneously. If either operation completes, then the other is "canceled". So the result is that your server will continue to read messages from the client until all the expected messages are read, as I understand your code.

1 Like

My God I cannot believe I spent more than 6 hours on this and you solved it so easily, thank you so much Andrew!! I still have thread 'main' panicked at 'unexpected task state', after creating the file but I will manage to solve it myself.

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.