Skip to content

Commit 7f455b2

Browse files
authored
task: clarify the task ID reuse guarantees (#7577)
1 parent 86de2e3 commit 7f455b2

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

tokio/src/runtime/task/id.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,28 @@ use std::{fmt, num::NonZeroU64};
55
/// An opaque ID that uniquely identifies a task relative to all other currently
66
/// running tasks.
77
///
8+
/// A task's ID may be re-used for another task only once *both* of the
9+
/// following happen:
10+
/// 1. The task itself exits.
11+
/// 2. There is no active [`JoinHandle`] associated with this task.
12+
///
13+
/// A [`JoinHandle`] is considered active in the following situations:
14+
/// - You are explicitly holding a [`JoinHandle`], [`AbortHandle`], or
15+
/// `tokio_util::task::AbortOnDropHandle`.
16+
/// - The task is being tracked by a [`JoinSet`] or `tokio_util::task::JoinMap`.
17+
///
818
/// # Notes
919
///
10-
/// - Task IDs are unique relative to other *currently running* tasks. When a
11-
/// task completes, the same ID may be used for another task.
1220
/// - Task IDs are *not* sequential, and do not indicate the order in which
1321
/// tasks are spawned, what runtime a task is spawned on, or any other data.
1422
/// - The task ID of the currently running task can be obtained from inside the
1523
/// task via the [`task::try_id()`](crate::task::try_id()) and
1624
/// [`task::id()`](crate::task::id()) functions and from outside the task via
1725
/// the [`JoinHandle::id()`](crate::task::JoinHandle::id()) function.
26+
///
27+
/// [`JoinHandle`]: crate::task::JoinHandle
28+
/// [`AbortHandle`]: crate::task::AbortHandle
29+
/// [`JoinSet`]: crate::task::JoinSet
1830
#[cfg_attr(docsrs, doc(cfg(all(feature = "rt"))))]
1931
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)]
2032
pub struct Id(pub(crate) NonZeroU64);

tokio/src/task/join_set.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ use crate::util::IdleNotifiedSet;
5050
/// }
5151
/// }
5252
/// ```
53+
///
54+
/// # Task ID guarantees
55+
///
56+
/// While a task is tracked in a `JoinSet`, that task's ID is unique relative
57+
/// to all other running tasks in Tokio. For this purpose, tracking a task in a
58+
/// `JoinSet` is equivalent to holding a [`JoinHandle`] to it. See the [task ID]
59+
/// documentation for more info.
60+
///
61+
/// [`JoinHandle`]: crate::task::JoinHandle
62+
/// [task ID]: crate::task::Id
5363
#[cfg_attr(docsrs, doc(cfg(feature = "rt")))]
5464
pub struct JoinSet<T> {
5565
inner: IdleNotifiedSet<JoinHandle<T>>,

0 commit comments

Comments
 (0)