Rust echo server and client using futures blocks itself forever

后端 未结 1 1701
慢半拍i
慢半拍i 2020-12-21 04:11

I used this code for the server, and modified this tutorial for the client code. When a client connects to the server, it blocks itself forever.

Server:

相关标签:
1条回答
  • 2020-12-21 04:35

    The problem has nothing to do with futures. You have an open socket and you ask to "read it until the end". What determines the end? In this case, it's when the socket is closed; so when is that?

    Trick question!

    • The client's read socket closes when the server's write socket closes.
    • The server's write socket closes when the server's read socket closes.
    • The server's read socket closes when the the client's write socket closes.

    So when does that happen? Because there's no code that does it specifically, it will close when the socket is dropped, so:

    • The client's write socket closes when the the client ends.

    Thus the deadlock. The issue can be fixed by explicitly closing the write half of the socket:

    let response = request.and_then(|(socket, _)| {
        socket.shutdown(std::net::Shutdown::Write).expect("Couldn't shut down");
        read_to_end(socket, Vec::new())
    });
    
    0 讨论(0)
提交回复
热议问题