Timeout for Accept in Unix Domain Sockets

I have a piece of code that binds to a Unix Domain Socket and accepts connections.
This code works and prints the Bind successful message and if there are connections from a client does process it .

Is there a way to time out in the accept ? The server listens to a socket path while the clients attempts to connect to a different socket path . I would like to have a timeout in the server and handle the case of not receiving a connection after X seconds .

The timeouts in the UDS seems to be for the read / write and not for the accept . Any help will be appreciated .

let mut listener_res =  UnixListener::bind(&socket);
        if listener_res.is_err() {
            // Socket is being used by somebody else 
        match listener_res {
                info!("Bind successful{}!",socket.display());
                match listener.accept().await {  // I need a timeout here 
                	       // Process this code
                	Err => {
                		// Error on socket 

The usual route to this would be to wrap listener.accept() with your runtime's timeout functionality.

Along the lines of

match timeout(Duration::from_secs(30), listener.accept()).await {
    Ok(Ok(listener)) => { /* process listener */ }
    Ok(Err(sock_err)) => { /* socket error */ }
    Err(elapsed) => { /* timeout */ }
1 Like

I would expect for just standard with blocking; make thread do a fake connection.
I think you can set listener to non blocking and call pselect in libc - Rust before any accept, not looked into what it would entail.
Also maybe something in nix crate.