task: clarify the behavior of several spawn_local methods (#7669)

This commit is contained in:
Mattia Pitossi
2025-10-12 10:46:23 +08:00
committed by GitHub
parent e7e02fcf0f
commit c1fa25f300
5 changed files with 24 additions and 15 deletions
+5 -4
View File
@@ -167,9 +167,9 @@ impl<T: 'static> JoinSet<T> {
self.insert(handle.spawn(task))
}
/// Spawn the provided task on the current [`LocalSet`] and store it in this
/// `JoinSet`, returning an [`AbortHandle`] that can be used to remotely
/// cancel the task.
/// Spawn the provided task on the current [`LocalSet`] or [`LocalRuntime`]
/// and store it in this `JoinSet`, returning an [`AbortHandle`] that can
/// be used to remotely cancel the task.
///
/// The provided future will start running in the background immediately
/// when this method is called, even if you don't await anything on this
@@ -177,9 +177,10 @@ impl<T: 'static> JoinSet<T> {
///
/// # Panics
///
/// This method panics if it is called outside of a `LocalSet`.
/// This method panics if it is called outside of a `LocalSet`or `LocalRuntime`.
///
/// [`LocalSet`]: crate::task::LocalSet
/// [`LocalRuntime`]: crate::runtime::LocalRuntime
/// [`AbortHandle`]: crate::task::AbortHandle
#[track_caller]
pub fn spawn_local<F>(&mut self, task: F) -> AbortHandle
+3 -3
View File
@@ -345,7 +345,7 @@ cfg_rt! {
///
/// # Panics
///
/// This function panics if called outside of a [`LocalSet`].
/// This function panics if called outside of a [`LocalSet`] or [`LocalRuntime`].
///
/// Note that if [`tokio::spawn`] is used from within a `LocalSet`, the
/// resulting new task will _not_ be inside the `LocalSet`, so you must use
@@ -428,7 +428,7 @@ cfg_rt! {
unsafe { handle.spawn_local(task, id, meta.spawned_at) }
} else {
match CURRENT.with(|LocalData { ctx, .. }| ctx.get()) {
None => panic!("`spawn_local` called from outside of a `task::LocalSet` or LocalRuntime"),
None => panic!("`spawn_local` called from outside of a `task::LocalSet` or `runtime::LocalRuntime`"),
Some(cx) => cx.spawn(future.take().unwrap(), meta)
}
})
@@ -438,7 +438,7 @@ cfg_rt! {
Ok(None) => panic!("Local tasks can only be spawned on a LocalRuntime from the thread the runtime was created on"),
Ok(Some(join_handle)) => join_handle,
Err(_) => match CURRENT.with(|LocalData { ctx, .. }| ctx.get()) {
None => panic!("`spawn_local` called from outside of a `task::LocalSet` or LocalRuntime"),
None => panic!("`spawn_local` called from outside of a `task::LocalSet` or `runtime::LocalRuntime`"),
Some(cx) => cx.spawn(future.unwrap(), meta)
}
}