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

Give Notified a safe API #4005

Merged
merged 19 commits into from
Aug 12, 2021
Merged

Give Notified a safe API #4005

merged 19 commits into from
Aug 12, 2021

Conversation

Darksonn
Copy link
Contributor

@Darksonn Darksonn commented Jul 29, 2021

This PR works toward giving the task module a safe API. Currently, the Notified object is hard to reason about. It doesn't hold a ref-count if the task is idle and not yet completed, as the runtime already holds a ref-count via the OwnedTasks structure, however this means that the task module does not have a safe API as removing the task from the OwnedTasks structure without shutting it down could lead to a Notified existing for a deallocated task.

In this PR, The ref-count for Notified is changed. Previously a Notified had a ref-count only sometimes. Now it always has a ref-count.

Replacing the pop_back call on OwnedTasks with a shutdown_all call also simplifies the safety guarantees of the task module as we don't have to return a value that could be dropped or moved to other threads before shutdown is called. This change required removal of the RefCell guard around the LocalSet context, so it is replaced with a different cell type.

The OwnedTasks::remove method is still not totally safe, but I can look at that in a future PR.

@Darksonn Darksonn added A-tokio Area: The main tokio crate M-runtime Module: tokio/runtime labels Jul 29, 2021
@hawkw hawkw self-requested a review August 2, 2021 17:11
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.

Looks good. I had some questions inline.

tokio/src/runtime/basic_scheduler.rs Outdated Show resolved Hide resolved
tokio/src/runtime/task/state.rs Outdated Show resolved Hide resolved
tokio/src/runtime/task/harness.rs Show resolved Hide resolved
tokio/src/runtime/task/mod.rs Outdated Show resolved Hide resolved
tokio/src/util/vec_deque_cell.rs Outdated Show resolved Hide resolved
tokio/src/runtime/task/harness.rs Show resolved Hide resolved
Comment on lines 602 to 605
// The OwnedTasks was closed in Shared::close.
debug_assert!(worker.shared.owned.is_closed());
// The OwnedTasks is also closed in Shared::close, but since the inject
// queue is closed before closing the OwnedTasks, it is possible for one
// thread to get here before the OwnedTasks gets closed.
worker.shared.owned.close();
Copy link
Contributor Author

@Darksonn Darksonn Aug 10, 2021

Choose a reason for hiding this comment

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

This change fixes a race that triggers the assert. The comment explains the race.

@Darksonn Darksonn merged commit b501f25 into master Aug 12, 2021
@Darksonn Darksonn deleted the notified-changes branch August 12, 2021 08:06
@Darksonn Darksonn mentioned this pull request Aug 12, 2021
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.

None yet

2 participants