How to break from the blocking TcpListener::accept call?


#1

I’m running the accept function of a TCP listener in a loop in a separate thread. I would like to shutdown this thread gracefully, but I can’t see any kind of shutdown function which I could use to break from accepting.

My current approach looks something like this:

use std::net::TcpListener;
use std::thread::spawn;

fn main() {
    let tcp_listener = TcpListener::bind((("0.0.0.0"), 0)).unwrap();
    let tcp_listener2 = tcp_listener.try_clone().unwrap();

    let t = spawn(move || {
        loop {
            match tcp_listener2.accept() {
                Ok(_) => { }
                Err(_) => { break; }
           }
        }
    });

    drop(tcp_listener);
    assert!(t.join().is_ok());
}

But that doesn’t do the trick (probably because I drop only the cloned copy?). Any thought on how to properly shutdown such thread?


#2

I ran into the same problem. I’m about to set up a channel to send a shutdown signal, check it right after accept() and break the loop if it got a message. It would also need to start a TCP client to unblock the accept.

It sounds like a crazy way to do so, though.


#3

Yes, that could work. Btw, I asked this question on SO too here (haven’t marked it as an answer yet):

Seems what is missing is a method that would call shutdown on the native file descriptor in the TcpListener.