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

tokio_tcp::TcpStream::poll_read_ready fails to detect HUP #1182

Closed
vi opened this issue Jun 24, 2019 · 6 comments
Closed

tokio_tcp::TcpStream::poll_read_ready fails to detect HUP #1182

vi opened this issue Jun 24, 2019 · 6 comments

Comments

@vi
Copy link
Contributor

vi commented Jun 24, 2019

Version

  • tokio-tcp 0.1.3
  • tokio 0.1.21
  • mio 0.6.19

Platform

x86_64-unknown-linux-gnu

Description

TcpStream::poll_read_ready claims to always include HUP on supported platform. I assume Linux (and Unix in genereal) is a supported platform. Yet arrival of FIN on the socket does not cause it to return Ok(Async::Ready).

Sample code

extern crate tokio;
extern crate tokio_tcp;
extern crate mio;

use tokio::prelude::*;

// Wait for TCP socket reset without reading or writing to it
struct WaitForJustHup(tokio_tcp::TcpStream);

impl Future for WaitForJustHup {
    type Item=();
    type Error=();
    fn poll(&mut self) -> Poll<(), ()> {
        let mut m = mio::Ready::empty();
        #[cfg(unix)] {
            m = mio::unix::UnixReady::hup().into();
        }
        let ret = self.0.poll_read_ready(m);
        println!("{:?}", ret);
        match ret {
            Ok(Async::NotReady) => Ok(Async::NotReady),
            _ => Ok(Async::Ready(())),
        }
    }
}

fn main() {
    let sa = "127.0.0.1:1234".parse().unwrap();
    let job = tokio_tcp::TcpStream::connect(&sa).map_err(drop).and_then(|tcp| {
        WaitForJustHup(tcp)
    });
    tokio::runtime::run(job);
}

When I run it against nc -nvlp 1234, I get:

Ok(NotReady)   # it connected
Ok(NotReady)   # I typed something into netcat
Ok(NotReady)   # I terminated netcat. I expect it to be Ok(Ready(_)) instead.
@vi
Copy link
Contributor Author

vi commented Jun 24, 2019

With mio = "=0.6.16" it works:

Ok(NotReady)
Ok(NotReady)
Ok(Ready(Hup))

With mio = "=0.6.17" it fails:

Ok(NotReady)
Ok(NotReady)
Ok(NotReady)
^C

@vi
Copy link
Contributor Author

vi commented Jun 24, 2019

tokio-rs/mio@b89e3bd

@carllerche
Copy link
Member

@vi yes, that was a bug. Reporting RDHUP as HUP was incorrect. I'm going to close this specific issue. I plan to revisit what events are exposed as part of 0.2.

@vi
Copy link
Contributor Author

vi commented Jun 27, 2019

Will it be possible to:

  • monitor for TCP's FIN or RSTs
  • while having the socket already shut down for writing
  • without reading from it (backpressure)

after the revisit? Ideally also portably.

@carllerche
Copy link
Member

Yes, I would like to. It will most likely require platform specific code in Tokio to do so.

@carllerche carllerche mentioned this issue Jun 27, 2019
8 tasks
@carllerche
Copy link
Member

I added a note here: #1209.

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

2 participants