Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a way to listen for tcp disconnects #29

Closed
canndrew opened this issue Nov 1, 2017 · 6 comments
Closed

Provide a way to listen for tcp disconnects #29

canndrew opened this issue Nov 1, 2017 · 6 comments

Comments

@canndrew
Copy link
Contributor

canndrew commented Nov 1, 2017

As I understand it, this wouldn't be possible with the way tokio is currently written because it doesn't distinguish between hup events and readable events. Assuming we can fix this, I'd like a way to detect when a tcp stream has closed without reading from it. The API could look something like this:

impl TcpStream {
    pub fn on_close(self) -> OnClose { .. }
}

impl OnClose {
    pub fn into_inner(self) -> TcpStream { .. }
}

impl Future for OnClose {
    type Item = ();
    type Error = io::Error;
    ..
}

That would let you put the stream into a state where you're just waiting for it to close, but you could convert that state back into a TcpStream if you later decide you want to read from the stream.

@alexcrichton
Copy link
Contributor

Could this be built by using poll_ready directly?

@canndrew
Copy link
Contributor Author

canndrew commented Nov 1, 2017

Not with the current API at least. TcpStream doesn't have a poll_ready method, nor does it implement Evented.

@alexcrichton
Copy link
Contributor

Ah yeah that's true :(

In general though something is going to be platform-specific here as "hup" as a signal is mosly a Unix-ism (and maybe only Linux? I forget)

@canndrew
Copy link
Contributor Author

canndrew commented Nov 2, 2017

According to the answers on this stack overflow question you can detect a disconnect with Windows IOCP by doing a 0-byte read. The man page for kqueue also mentions some stuff about detecting socket disconnects using the EV_EOF flag. So I'm guessing it's possible to implement this, just not easy :/

@carllerche
Copy link
Member

Yeah, it seems non trivial...

I'm going to close the issue for now as the issue is stale and it doesn't seem like this will pursued. However, if you want to continue the discussion, let me know and I can open it back up.

@p-avital
Copy link

Also interested in detecting hangups with TcpStreams, any ideas ?
Also, how should that interact with Codec::framed ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants