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

driver: add support for registering file descriptors with user-specified flags #6089

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

Noah-Kennedy
Copy link
Contributor

@Noah-Kennedy Noah-Kennedy commented Oct 19, 2023

This change adds unstable support for custom user-specified epoll flags when registering file descriptors to our TcpListener, UnixListener, and AsyncFd APIs.

It also adds a simple unit test to demonstrate basic functionality with EPOLLEXCLUSIVE.

This is the initial implementation of #6084.

…ied epoll flags

WIP fix for #6084.

This currently only adds support for TcpListener.
@Noah-Kennedy Noah-Kennedy changed the title driver: add support for registering file descriptors with user-specif… driver: add support for registering file descriptors with user-specified flags Oct 19, 2023
@Noah-Kennedy Noah-Kennedy self-assigned this Oct 19, 2023
@Darksonn Darksonn added A-tokio Area: The main tokio crate M-net Module: tokio/net labels Oct 20, 2023
@Noah-Kennedy Noah-Kennedy linked an issue Oct 20, 2023 that may be closed by this pull request
@Noah-Kennedy Noah-Kennedy added the M-io Module: tokio/io label Oct 20, 2023
tokio/src/io/async_fd.rs Show resolved Hide resolved
tokio/src/io/poll_evented.rs Outdated Show resolved Hide resolved
tokio/src/io/async_fd.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@Ralith Ralith left a comment

Choose a reason for hiding this comment

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

Public API LGTM.

@Noah-Kennedy Noah-Kennedy marked this pull request as ready for review October 20, 2023 23:53
Comment on lines 246 to 254
/// Create a new AsyncFd with the provided raw epoll flags for registration.
///
/// These flags replace any epoll flags would normally set when registering the fd.
///
/// **Note**: This is an [unstable API][unstable]. The public API of this may break in 1.x
/// releases.
/// See [the documentation on unstable features][unstable] for details.
///
/// [unstable]: crate#unstable-features
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add to the documentation that EPOLLONESHOT must not be used, and that EPOLLET must be set. And please add debug asserts for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

…OLLET, and add debug assert to check for EPOLLONESHOT
Copy link
Member

@carllerche carllerche left a comment

Choose a reason for hiding this comment

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

Thanks for getting this started. I left some comments inline. I also left a higher-level question in the original issue asking about how edge-triggered vs. level-triggered impacts this.

#[track_caller]
#[cfg(all(target_os = "linux", tokio_unstable))]
#[cfg_attr(docsrs, doc(cfg(all(tokio_unstable, target_os = "linux"))))]
pub fn from_std_with_epoll_flags(
Copy link
Member

Choose a reason for hiding this comment

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

I would rather not have these methods on TcpListener. Consider adding them to TcpSocket instead as that is the socket builder API.

#[track_caller]
#[cfg(all(target_os = "linux", tokio_unstable))]
#[cfg_attr(docsrs, doc(cfg(all(tokio_unstable, target_os = "linux"))))]
pub fn from_std_with_epoll_flags(
Copy link
Member

Choose a reason for hiding this comment

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

If the TCP method is added to TcpSocket, this will need to be removed and either skipped initially or we will need a UnixSocket API.


// TODO: if this returns an err, the `ScheduledIo` leaks...
let res = unsafe {
libc::epoll_ctl(
Copy link
Member

Choose a reason for hiding this comment

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

We can't call epoll_* methods from Tokio as there is no guarantee that Mio uses epoll under the hood. Either we should have mio expose more APIs and call those, or we need to use epoll directly in Tokio (obviously, the first option is preferable).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What guarantees are made by Mio around it's fd?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I know that this behavior is not guaranteed, but we don't really say what behavior is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Which also I suppose begs the question "if it isn't here so people can do this as an escape hatch, why is it here at all?"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That being said, there's a deeper question here, which is "do we need Interest::CUSTOM() in Mio, and how would that work?"

Personally, I think if we're going to continue to use Mio within tokio, we probably need something like this.

@Noah-Kennedy
Copy link
Contributor Author

@carllerche since it's unstable, is it fine to initially put this on the listeners while we bikeshed UnixSocket, as it seems like we exposed another question about our AF_UNIX APIs here?

@Noah-Kennedy
Copy link
Contributor Author

Picking this back up again

@Darksonn Darksonn marked this pull request as draft October 9, 2024 13:55
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-io Module: tokio/io M-net Module: tokio/net
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants