Navigation Menu

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

net: use &self with TcpListener::accept #2919

Merged
merged 9 commits into from Oct 8, 2020
Merged

Conversation

carllerche
Copy link
Member

Uses the infrastructure added by #2828 to enable switching
TcpListener::accept to use &self.

This also switches poll_accept to use &self. While doing introduces
a hazard, poll_* style functions are considered low-level. Most users
will use the async fn variants which are more misuse-resistant.

TcpListener::incoming() is temporarily removed as it has the same
problem as TcpSocket::by_ref() and will be implemented later.

Uses the infrastructure added by #2828 to enable switching
`TcpListener::accept` to use `&self`.

This also switches `poll_accept` to use `&self`. While doing introduces
a hazard, `poll_*` style functions are considered low-level. Most users
will use the `async fn` variants which are more misuse-resistant.

TcpListener::incoming() is temporarily removed as it has the same
problem as `TcpSocket::by_ref()` and will be implemented later.
@carllerche carllerche added A-tokio Area: The main tokio crate M-net Module: tokio/net labels Oct 6, 2020
Copy link
Member

@seanmonstar seanmonstar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TcpListener::incoming() is temporarily removed as it has the same
problem as TcpSocket::by_ref()

This statement isn't clear unless you have all the context to know what the problems were there (I've even already forgotten).

@@ -186,33 +186,78 @@ impl ScheduledIo {
}
}

/// Notifies all pending waiters that have registered interest in `ready`.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes here solve a potential deadlock during shutdown. Waking a task must be done from outside of the lock or it could result in a deadlock due to an attempt to re-enter the lock.

@@ -132,8 +132,19 @@ impl Registration {
cfg_io_readiness! {
impl Registration {
pub(super) async fn readiness(&self, interest: mio::Interest) -> io::Result<ReadyEvent> {
// TODO: does this need to return a `Result`?
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out that it does need to return a result :)

This implementation is not great but I believe the shutdown logic needs to be revisited post 0.3.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tracked my thoughts in #2924

@carllerche
Copy link
Member Author

@seanmonstar I wrote some quick thoughts on TcpStream::by_ref() here: #2716 (comment)

I also encountered a few bugs that were introduced with the intrusive waker change. I fixed them in here and highlighted the fixes with comments.

///
/// When ready, the most recent task that called `poll_accept` is notified.
/// The caller is responsble to ensure that `poll_accept` is called from a
/// single task. Failing to do this could result in tasks hanging.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should eventually include language that poll_accept should only be used for 1 task.

@carllerche carllerche merged commit 066965c into master Oct 8, 2020
@carllerche carllerche deleted the immut-tcp-accept branch October 8, 2020 19:12
@kirs
Copy link

kirs commented Dec 19, 2020

👋 I came across this PR because I have troubles upgrading from tokio 0.2 to tokio 0.3.

let mut incoming = listener.incoming();
                            ^^^^^^^^ method not found in `tokio::net::TcpListener`

TcpListener::incoming() is temporarily removed

I see that the function was removed - but I don't think it's a good design to "temporary remove" public API methods that dozens of other projects are relying on.

I'd love to see tokio as a mature library in a way that doesn't come with surprises in "temporary removed" stuff.

Have you considered going through a deprecation cycle and keeping it in 0.3 with a warning that in future versions it might get removed?

@Darksonn
Copy link
Contributor

@kirs With Tokio 0.3 the TcpListener itself implements Stream, so you can just use the listener directly. In principle this makes incoming completely redundant, and the only reason we are considering putting it back is for consistency with the standard library.

As for a deprecation cycle, I don't think it's worth it for such easily replaced functionality such as this. Especially not when we are releasing Tokio v1.0 shortly, with which we guarantee no breaking changes for several years.

Tokio v0.3 was always intended so we can try out the final version of the APIs before we put them in stone for v1.0.0, and temporarily removing APIs so we can add them back without a breaking change in v1.x if we are not sure about them is an important step to be able to do this.

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 M-net Module: tokio/net
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants