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

tokio: wake localset on spawn_local #3369

Merged
merged 2 commits into from Jan 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions tokio/src/task/local.rs
Expand Up @@ -242,6 +242,8 @@ impl LocalSet {
///
/// This task is guaranteed to be run on the current thread.
///
/// Calls to this function will result in waking the current localset.
///
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure this is necessary to have in the doc?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Happy to remove it if you don't think it is necessary. I added it because a side effect that changes program behavior is worth noting imo.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think it is necessary. The "wake" is an implementation detail.

Copy link
Contributor

Choose a reason for hiding this comment

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

Besides, notes about changes in behavior go in the changelog instead. (You don't have to put it in this PR, we do them together when making the release.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense - Thanks for the review! Pushed the suggested changes.

/// Unlike the free function [`spawn_local`], this method may be used to
/// spawn local tasks when the task set is _not_ running. For example:
/// ```rust
Expand Down Expand Up @@ -283,6 +285,7 @@ impl LocalSet {
let future = crate::util::trace::task(future, "local");
let (task, handle) = unsafe { task::joinable_local(future) };
self.context.tasks.borrow_mut().queue.push_back(task);
self.context.shared.waker.wake();
handle
}

Expand Down
14 changes: 14 additions & 0 deletions tokio/tests/task_local_set.rs
@@ -1,6 +1,11 @@
#![warn(rust_2018_idioms)]
#![cfg(feature = "full")]

use futures::{
future::{pending, ready},
FutureExt,
};

use tokio::runtime::{self, Runtime};
use tokio::sync::{mpsc, oneshot};
use tokio::task::{self, LocalSet};
Expand Down Expand Up @@ -486,6 +491,15 @@ async fn acquire_mutex_in_drop() {
drop(local);
}

#[tokio::test]
async fn spawn_wakes_localset() {
let local = LocalSet::new();
futures::select! {
_ = local.run_until(pending::<()>()).fuse() => unreachable!(),
ret = async { local.spawn_local(ready(())).await.unwrap()}.fuse() => ret
}
}

fn rt() -> Runtime {
tokio::runtime::Builder::new_current_thread()
.enable_all()
Expand Down