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

docs: Clarified the point about changing the file descriptor underlyi… #3430

Merged
merged 3 commits into from Jan 17, 2021

Conversation

hgomersall
Copy link
Contributor

…ng an AsyncFd

Motivation

The info on when it's appropriate to have a clone of an underlying File object when using an AsyncFd was not clear to me from the current docs.

Solution

Adds a clarification point. This may need further clarification, which I'm happy to do.

@Darksonn Darksonn added A-tokio Area: The main tokio crate M-io Module: tokio/io T-docs Topic: documentation labels Jan 15, 2021
Comment on lines 27 to 31
/// descriptor results in unspecified behavior in the IO driver, which may
/// include breaking notifications for other sockets/etc.
/// descriptor through the underlying [`AsRawFd::as_raw_fd`] results
/// in unspecified behavior in the IO driver, which may include breaking
/// notifications for other sockets/etc. That is not to say that simply using
/// a clone of the inner object would be problematic if that is a supported
/// thing to do.
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't really like this wording. Some notes:

  • The fd is not changed through as_raw_fd.
  • I'm not sure we have to expand on the kinds of ways it may break.
  • I don't want to make promises around clones of the fd. Those can easily get you in trouble too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Isn't the point that as_raw_fs exposes the actual raw file descriptor?

If clones of File might cause UB, then we have a problem (and probably new should be unsafe).

Notwithstanding the huge number of ways in which things can break, there is a definitely a need to provide more guidance around common use cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To clarify the first point, if the raw file fd wasn't exposed, would there be a problem?

Copy link
Contributor

@Darksonn Darksonn Jan 15, 2021

Choose a reason for hiding this comment

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

I mean, the AsyncFd access the fd through as_raw_fd, but you don't change it using as_raw_fd. And the kinds of trouble are unspecified behavior, not undefined behavior. My second point was referring to "which may include breaking notifications for other sockets/etc."

Copy link
Contributor

Choose a reason for hiding this comment

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

To clarify, the problem happens when as_raw_fd returns different values when called multiple times, but you don't make it return different values through as_raw_fd — it doesn't provide mutable access to the 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.

It's a statement about the implementer of AsRawFd - if that does something strange, then you have a problem? I'm not sure of the right way of phrasing "done by but not done right here" which was the intention of "through the". How about "Changing the file descriptor through an unexpected implementation of AsRawFd..."?

Perhaps you could clarify the difference between unspecified behaviour and undefined behaviour...?

Copy link
Contributor

Choose a reason for hiding this comment

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

One possible way to word it would be:

The inner type must always return the same file descriptor, even if as_raw_fd is called multiple times. Violating this requirement can cause unspecified behavior.

Unspecified behavior means it does weird stuff. Undefined behavior means unsafe code gone wrong.

Copy link
Contributor

Choose a reason for hiding this comment

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

Or maybe reverse the sentence.

The as_raw_fd method on the inner type must always return the same file descriptor when called multiple times.

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 like the last one. I'll do that...

tokio/src/io/async_fd.rs Outdated Show resolved Hide resolved
Co-authored-by: Alice Ryhl <alice@ryhl.io>
@Darksonn Darksonn merged commit 5402c94 into tokio-rs:master Jan 17, 2021
@Darksonn
Copy link
Contributor

Thank you for the PR!

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 T-docs Topic: documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants