Skip to content

Commit c1fa25f

Browse files
task: clarify the behavior of several spawn_local methods (#7669)
1 parent e7e02fc commit c1fa25f

File tree

5 files changed

+24
-15
lines changed

5 files changed

+24
-15
lines changed

tokio-util/src/task/join_map.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,8 @@ where
345345
self.insert(key, task);
346346
}
347347

348-
/// Spawn the provided task on the current [`LocalSet`] and store it in this
349-
/// `JoinMap` with the provided key.
348+
/// Spawn the provided task on the current [`LocalSet`] or [`LocalRuntime`]
349+
/// and store it in this `JoinMap` with the provided key.
350350
///
351351
/// If a task previously existed in the `JoinMap` for this key, that task
352352
/// will be cancelled and replaced with the new one. The previous task will
@@ -355,9 +355,10 @@ where
355355
///
356356
/// # Panics
357357
///
358-
/// This method panics if it is called outside of a `LocalSet`.
358+
/// This method panics if it is called outside of a `LocalSet` or `LocalRuntime`.
359359
///
360360
/// [`LocalSet`]: tokio::task::LocalSet
361+
/// [`LocalRuntime`]: tokio::runtime::LocalRuntime
361362
/// [`join_next`]: Self::join_next
362363
#[track_caller]
363364
pub fn spawn_local<F>(&mut self, key: K, task: F)

tokio-util/src/task/join_queue.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,20 @@ impl<T> JoinQueue<T> {
8888
self.push_back(handle.spawn(task))
8989
}
9090

91-
/// Spawn the provided task on the current [`LocalSet`] and store it in this
92-
/// [`JoinQueue`], returning an [`AbortHandle`] that can be used to remotely
93-
/// cancel the task.
91+
/// Spawn the provided task on the current [`LocalSet`] or [`LocalRuntime`]
92+
/// and store it in this [`JoinQueue`], returning an [`AbortHandle`] that
93+
/// can be used to remotely cancel the task.
9494
///
9595
/// The provided future will start running in the background immediately
9696
/// when this method is called, even if you don't await anything on this
9797
/// [`JoinQueue`].
9898
///
9999
/// # Panics
100100
///
101-
/// This method panics if it is called outside of a `LocalSet`.
101+
/// This method panics if it is called outside of a `LocalSet` or `LocalRuntime`.
102102
///
103103
/// [`LocalSet`]: tokio::task::LocalSet
104+
/// [`LocalRuntime`]: tokio::runtime::LocalRuntime
104105
/// [`AbortHandle`]: tokio::task::AbortHandle
105106
#[track_caller]
106107
pub fn spawn_local<F>(&mut self, task: F) -> AbortHandle

tokio-util/src/task/task_tracker.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,11 +401,17 @@ impl TaskTracker {
401401
handle.spawn(self.track_future(task))
402402
}
403403

404-
/// Spawn the provided future on the current [`LocalSet`], and track it in this `TaskTracker`.
404+
/// Spawn the provided future on the current [`LocalSet`] or [`LocalRuntime`]
405+
/// and track it in this `TaskTracker`.
405406
///
406407
/// This is equivalent to `tokio::task::spawn_local(tracker.track_future(task))`.
407408
///
409+
/// # Panics
410+
///
411+
/// This method panics if it is called outside of a `LocalSet` or `LocalRuntime`.
412+
///
408413
/// [`LocalSet`]: tokio::task::LocalSet
414+
/// [`LocalRuntime`]: tokio::runtime::LocalRuntime
409415
#[inline]
410416
#[track_caller]
411417
#[cfg(feature = "rt")]

tokio/src/task/join_set.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,20 @@ impl<T: 'static> JoinSet<T> {
167167
self.insert(handle.spawn(task))
168168
}
169169

170-
/// Spawn the provided task on the current [`LocalSet`] and store it in this
171-
/// `JoinSet`, returning an [`AbortHandle`] that can be used to remotely
172-
/// cancel the task.
170+
/// Spawn the provided task on the current [`LocalSet`] or [`LocalRuntime`]
171+
/// and store it in this `JoinSet`, returning an [`AbortHandle`] that can
172+
/// be used to remotely cancel the task.
173173
///
174174
/// The provided future will start running in the background immediately
175175
/// when this method is called, even if you don't await anything on this
176176
/// `JoinSet`.
177177
///
178178
/// # Panics
179179
///
180-
/// This method panics if it is called outside of a `LocalSet`.
180+
/// This method panics if it is called outside of a `LocalSet`or `LocalRuntime`.
181181
///
182182
/// [`LocalSet`]: crate::task::LocalSet
183+
/// [`LocalRuntime`]: crate::runtime::LocalRuntime
183184
/// [`AbortHandle`]: crate::task::AbortHandle
184185
#[track_caller]
185186
pub fn spawn_local<F>(&mut self, task: F) -> AbortHandle

tokio/src/task/local.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ cfg_rt! {
345345
///
346346
/// # Panics
347347
///
348-
/// This function panics if called outside of a [`LocalSet`].
348+
/// This function panics if called outside of a [`LocalSet`] or [`LocalRuntime`].
349349
///
350350
/// Note that if [`tokio::spawn`] is used from within a `LocalSet`, the
351351
/// resulting new task will _not_ be inside the `LocalSet`, so you must use
@@ -428,7 +428,7 @@ cfg_rt! {
428428
unsafe { handle.spawn_local(task, id, meta.spawned_at) }
429429
} else {
430430
match CURRENT.with(|LocalData { ctx, .. }| ctx.get()) {
431-
None => panic!("`spawn_local` called from outside of a `task::LocalSet` or LocalRuntime"),
431+
None => panic!("`spawn_local` called from outside of a `task::LocalSet` or `runtime::LocalRuntime`"),
432432
Some(cx) => cx.spawn(future.take().unwrap(), meta)
433433
}
434434
})
@@ -438,7 +438,7 @@ cfg_rt! {
438438
Ok(None) => panic!("Local tasks can only be spawned on a LocalRuntime from the thread the runtime was created on"),
439439
Ok(Some(join_handle)) => join_handle,
440440
Err(_) => match CURRENT.with(|LocalData { ctx, .. }| ctx.get()) {
441-
None => panic!("`spawn_local` called from outside of a `task::LocalSet` or LocalRuntime"),
441+
None => panic!("`spawn_local` called from outside of a `task::LocalSet` or `runtime::LocalRuntime`"),
442442
Some(cx) => cx.spawn(future.unwrap(), meta)
443443
}
444444
}

0 commit comments

Comments
 (0)