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

Detect TCP disconnect without reading or writing anything to that socket? #2228

Closed
masnagam opened this issue Feb 9, 2020 · 4 comments · Fixed by #2743
Closed

Detect TCP disconnect without reading or writing anything to that socket? #2228

masnagam opened this issue Feb 9, 2020 · 4 comments · Fixed by #2743
Assignees
Labels
A-tokio Area: The main tokio crate C-feature-accepted Category: A feature request that has been accepted pending implementation. C-feature-request Category: A feature request. M-net Module: tokio/net

Comments

@masnagam
Copy link
Contributor

masnagam commented Feb 9, 2020

Version

  • tokio: v0.2.11
    • tokio::net::tcp::TcpStream

Description

In #483, it was discussed to use poll_read_ready(Ready::empty()) for detecting TCP disconnect.

After that, poll_read_ready() was removed at 6d8cc4e according to a discussion on gitter as described in #1392 (comment).

Is there any alternative way to detect TCP disconnect without writing or reading anything to/from that socket?

I haven't tried yet, but we might be able to use peek() or poll_peek() for this purpose.

Background

In some situations, it's necessary to detect TCP disconnect without reading or writing anything to/from that socket.

For example, suppose an HTTP server that performs a time-consuming task for a response. This HTTP server don't like to read data from the TCP socket for detecting disconnect in order to support HTTP keep-alive and request pipelining easily. This HTTP server has no data to write until the task outputs something.

If there is no way to detect TCP disconnect other than reading/writing, this HTTP server cannot detect TCP disconnect until the task outputs something.

The similar situation occurs in actix-web. And it seems to leak TCP sockets if clients disconnect while it waits for data from a Stream object:
https://github.com/masnagam/rust-case-studies/tree/master/actix-web-streaming-and-tcp-disconnect

Related Issues

Example of detecting TCP disconnect using poll_read_ready()

@Darksonn Darksonn added A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-net Module: tokio/net labels Jul 25, 2020
@Darksonn Darksonn changed the title Question: How to detect TCP disconnect without writing or reading anything to/from that socket? Detect TCP disconnect without reading or writing anything to that socket? Jul 25, 2020
@Darksonn Darksonn added the E-help-wanted Call for participation: Help is requested to fix this issue. label Jul 25, 2020
@masnagam
Copy link
Contributor Author

Probably, the simplest solution is to restore poll_read_ready(). However, in the historical point of view, this solution may be unacceptable in this project.

Personally, I think it's great for library users that Tokio provides lower level API as already mentioned in #1392 (comment).

@Darksonn
Copy link
Contributor

I think restoring poll_read_ready() would be fine.

@Darksonn Darksonn added the C-feature-accepted Category: A feature request that has been accepted pending implementation. label Jul 28, 2020
@carllerche
Copy link
Member

cc @seanmonstar

@carllerche
Copy link
Member

This is now tracked by #2968

@Darksonn Darksonn removed the E-help-wanted Call for participation: Help is requested to fix this issue. label Dec 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-feature-accepted Category: A feature request that has been accepted pending implementation. C-feature-request Category: A feature request. M-net Module: tokio/net
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants