-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Prevent basic_scheduler from panicking if a task is aborted from another thread #3672
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c6221e8
Prevent basic scheduler panic on task release (fixes #3662)
udoprog 119a854
Release remote tasks that have been dropped
udoprog 4587856
Apply suggestions from code review
udoprog ae4e3d9
Revert to futures::future::pending to stay within MSRV
udoprog File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,3 +24,83 @@ fn test_abort_without_panic_3157() { | |
| let _ = handle.await; | ||
| }); | ||
| } | ||
|
|
||
| /// Checks that a suspended task can be aborted inside of a current_thread | ||
| /// executor without panicking as reported in issue #3662: | ||
| /// <https://github.com/tokio-rs/tokio/issues/3662>. | ||
| #[test] | ||
| fn test_abort_without_panic_3662() { | ||
| use std::sync::atomic::{AtomicBool, Ordering}; | ||
| use std::sync::Arc; | ||
| use std::task::Poll; | ||
|
|
||
| struct DropCheck(Arc<AtomicBool>); | ||
|
|
||
| impl Drop for DropCheck { | ||
| fn drop(&mut self) { | ||
| self.0.store(true, Ordering::SeqCst); | ||
| } | ||
| } | ||
|
|
||
| let rt = tokio::runtime::Builder::new_current_thread() | ||
| .worker_threads(1) | ||
| .build() | ||
| .unwrap(); | ||
|
|
||
| rt.block_on(async move { | ||
| let drop_flag = Arc::new(AtomicBool::new(false)); | ||
| let drop_check = DropCheck(drop_flag.clone()); | ||
|
|
||
| let j = tokio::spawn(async move { | ||
| // NB: just grab the drop check here so that it becomes part of the | ||
| // task. | ||
| let _drop_check = drop_check; | ||
| futures::future::pending::<()>().await; | ||
udoprog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| let drop_flag2 = drop_flag.clone(); | ||
|
|
||
| let task = tokio::task::spawn_blocking(move || { | ||
udoprog marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // This runs in a separate thread so it doesn't have immediate | ||
| // thread-local access to the executor. It does however transition | ||
| // the underlying task to be completed, which will cause it to be | ||
| // dropped (in this thread no less). | ||
| assert!(!drop_flag2.load(Ordering::SeqCst)); | ||
| j.abort(); | ||
| // TODO: is this guaranteed at this point? | ||
| // assert!(drop_flag2.load(Ordering::SeqCst)); | ||
|
Comment on lines
+62
to
+69
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well if the task's destructor runs in this thread, it would be, but it's unclear to me that the destructor would necessarily run here. |
||
| j | ||
| }) | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| assert!(drop_flag.load(Ordering::SeqCst)); | ||
| let result = task.await; | ||
| assert!(result.unwrap_err().is_cancelled()); | ||
|
|
||
| // Note: We do the following to trigger a deferred task cleanup. | ||
| // | ||
| // The relevant piece of code you want to look at is in: | ||
| // `Inner::block_on` of `basic_scheduler.rs`. | ||
| // | ||
| // We cause the cleanup to happen by having a poll return Pending once | ||
| // so that the scheduler can go into the "auxilliary tasks" mode, at | ||
| // which point the task is removed from the scheduler. | ||
| let i = tokio::spawn(async move { | ||
| let mut first = true; | ||
|
|
||
| let task = futures::future::poll_fn(|cx| { | ||
| if std::mem::take(&mut first) { | ||
| cx.waker().wake_by_ref(); | ||
| Poll::Pending | ||
| } else { | ||
| Poll::Ready(()) | ||
| } | ||
| }); | ||
udoprog marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| task.await; | ||
| }); | ||
|
|
||
| i.await.unwrap(); | ||
| }); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.