ringbahn / ringbahn

safe bindings to io-uring

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

the Tcpstream won't be close after drop the Tcpstream

Sherlock-Holo opened this issue · comments

in this code, when run the test and see the /proc/{pid}/fd, we can see there is no fd which type is socket. Press enter to make program continue running until sleep, and see the /proc/{pid}/fd again, we can see there is a fd like 4 -> 'socket:[13186835]'.

In normal the Tcpstream should be closed but it doesn't, and I find the Tcpstream doesn't implement the Drop

This is because of the lack of async drop. ringbahn's io objects are closed asynchronously with io-uring, but that can't be done from the destructor. The way to close the Tcpstream is to call the close method from futures::io::AsyncWriteExt and awaiting the returned future. For example:

use futures::executor::block_on;
use futures::io::AsyncWriteExt;

use ringbahn::net::TcpStream;

fn main() {
    block_on(async move {
        let stream = TcpStream::connect("www.qq.com:80").await.unwrap();

        stream.close().await;
    })
}

We could do something cleverer here, like do a blocking drop if you haven't closed the tcpstream when its dropped. I don't know if we should or not.

I think we can close the fd directly when drop the TcpStrean, any pending io operation in io_uring should be failed, we can ignore those errors

I also think we can close it with a libc::close, but I'm not sure if we should. In any event we need to document that a correct implementation should explicitly close its handles before dropping them.