Skip to content

runtime: improve safety comments of Readiness<'_> #7415

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

ADD-SP
Copy link
Member

@ADD-SP ADD-SP commented Jun 22, 2025

Motivation

Just some improvements while reading the source.

Solution

Nothing to point out in particular.

Comment on lines 432 to 435
let (scheduled_io, state, waiter) = {
// Safety: `Self` is `!Unpin`
let me = unsafe { self.get_unchecked_mut() };
(me.scheduled_io, &mut me.state, &me.waiter)
Copy link
Member Author

@ADD-SP ADD-SP Jun 22, 2025

Choose a reason for hiding this comment

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

For reviewers: The reason I didn't use pin_project to eliminate the unsafe block is that Future's implementation already has a lot of unsafe blocks, so eliminating it doesn't significantly reduce the mental burden but increases code complexity.

Copy link
Member

Choose a reason for hiding this comment

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

Take it or leave it: perhaps that's worth including in the comment?

Copy link
Member Author

@ADD-SP ADD-SP Jun 28, 2025

Choose a reason for hiding this comment

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

@hawkw Added in e2ecd1f .

@ADD-SP ADD-SP added A-tokio Area: The main tokio crate M-runtime Module: tokio/runtime labels Jun 22, 2025
Comment on lines 541 to 543
// Safety: We don't need to acquire the lock here because
// the `waiter.interest` never changes.
let interest = unsafe { (*waiter.get()).interest };
Copy link
Contributor

Choose a reason for hiding this comment

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

Could it be shared in this scenario? You removed the comment that says it can't - is that because it actually can or just because you don't need it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Could it be shared in this scenario?

The State::Done certainly indicates waiter is not shared, the original comment is right.

is that because it actually can or just because you don't need it?

Because the original comment is not comprehensive.

waiter.interest is not shared in this scenario, but this doesn't mean we can always observe the latest value of waiter.interest without the lock held, unless waiter.interest is never changes.

Copy link
Member Author

@ADD-SP ADD-SP Jun 28, 2025

Choose a reason for hiding this comment

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

I have enriched the safety comment for your concern: af0a52a .

Comment on lines 432 to 435
let (scheduled_io, state, waiter) = {
// Safety: `Self` is `!Unpin`
let me = unsafe { self.get_unchecked_mut() };
(me.scheduled_io, &mut me.state, &me.waiter)
Copy link
Member

Choose a reason for hiding this comment

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

Take it or leave it: perhaps that's worth including in the comment?

@ADD-SP ADD-SP force-pushed the add_sp/runtime-write-more-detailed-safety-comment branch 2 times, most recently from 029df9f to b8c9124 Compare June 28, 2025 15:07
@ADD-SP ADD-SP force-pushed the add_sp/runtime-write-more-detailed-safety-comment branch from b8c9124 to 6e6b73e Compare June 28, 2025 15:29
@ADD-SP ADD-SP requested review from Darksonn and hawkw July 2, 2025 03:37
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

Feel free to merge with below grammar fixed.

Comment on lines +492 to +493
// Safety: Since the `waiter` is not in the intrusive list yet,
// so we have exclusive access to it. The Mutex ensures
Copy link
Contributor

Choose a reason for hiding this comment

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

Sentences starting with "since" don't use "so".

Suggested change
// Safety: Since the `waiter` is not in the intrusive list yet,
// so we have exclusive access to it. The Mutex ensures
// Safety: Since the `waiter` is not in the intrusive list yet,
// we have exclusive access to it. The Mutex ensures

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

Successfully merging this pull request may close these issues.

3 participants