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

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)).unwrap();
    let tcp_listener2 = tcp_listener.try_clone().unwrap();

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


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


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.

1 Like

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.