-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Give Notified a safe API #4005
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Darksonn
added
A-tokio
Area: The main tokio crate
M-runtime
Module: tokio/runtime
labels
Jul 29, 2021
carllerche
reviewed
Aug 9, 2021
There was a problem hiding this 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.
Darksonn
commented
Aug 10, 2021
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(); |
There was a problem hiding this comment.
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.
carllerche
approved these changes
Aug 10, 2021
Merged
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 theOwnedTasks
structure without shutting it down could lead to aNotified
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 ashutdown_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 theRefCell
guard around theLocalSet
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.