When Rust code calls tokio::spawn, Tokio starts an asynchronous task, a unit of work that can pause while it waits, and returns a JoinHandle. The handle lets the caller wait for the task’s result or ask it to stop. Dropping an ordinary JoinHandle does neither. Tokio detaches the task, so it keeps running without a handle the caller can use to wait for it.
Keep the handle while you need to observe or control the task. When the code that owns the handle is ending, decide whether to wait for the task, ask it to shut down gracefully, abort it, or let it continue without tracking it.
A spawned task keeps running when its handle is dropped
A Tokio task is a unit of asynchronous work that the Tokio runtime schedules. Calling tokio::spawn gives the work to the scheduler, which starts the task without waiting for the caller to await its handle. Tokio may run the task on the thread that spawned it or on another runtime thread, and the task may move between threads later. The JoinHandle<T> gives the caller the ability to join the task, or wait for it to finish. T is the task’s return type, as described in Tokio’s spawning tutorial and JoinHandle documentation.
Await the handle when you need the task’s result or need to know it has finished:
let handle = tokio::spawn(async {
"finished"
});
let output = handle.await.unwrap();
println!("{output}");
Awaiting a handle returns a Result. If joining fails, its error is a JoinError, which reports that the task panicked or was forcibly cancelled, such as during runtime shutdown. If the task also returns a Result, the awaited value has two layers: the outer result reports whether joining succeeded, and the inner result is the task’s result. In a function that can convert both errors, handle.await?? propagates either failure.
If you drop an ordinary JoinHandle, Tokio detaches the task and it keeps running. The caller can no longer await it or get its return value through that handle. Dropping the handle does not stop the task. A child task that code spawns independently can also keep running after its parent task ends, unless the code explicitly manages the child’s lifetime.
Keep handles when you need to join several tasks
If the tasks have compatible handle types, store their handles in a Vec and await them later:
let mut handles = Vec::new();
for item in items {
handles.push(tokio::spawn(async move {
process(item).await
}));
}
for handle in handles {
handle.await??;
}
The tasks run at the same time, but this loop observes their results in the order of the handles in the vector. A later task might finish first, but the loop waits on earlier handles before it reaches that task’s result.
To collect task results as the tasks finish, use Tokio’s JoinSet, which stores task handles. Its join_next method returns a completed task in the order tasks finish. It returns None when the set is empty:
let mut tasks = tokio::task::JoinSet::new();
for item in items {
tasks.spawn(async move {
process(item).await
});
}
while let Some(result) = tasks.join_next().await {
result??;
}
Use JoinSet when tasks share a return type and you want to handle their results as they finish. Dropping a JoinSet aborts tasks that remain in it, unlike dropping an ordinary handle. To let those tasks keep running without tracking them, call detach_all before dropping the set. See the JoinSet API for details.
Choose how the owner ends
When the code that owns a task is ending, choose what should happen to the task. Await its handle if the owner needs the result or needs to confirm that the task has stopped. To stop an asynchronous task, call abort and then await its handle to observe what happened:
handle.abort();
let result = handle.await;
Calling abort requests cancellation, but the task might not have stopped yet. Await the handle to see the outcome, which is typically a JoinError reporting cancellation. If the task finished before the abort request, it can still finish normally. Tokio also documents a limitation for spawn_blocking: aborting has no effect after the blocking task starts running.
For tasks in a JoinSet, abort_all requests cancellation, but it does not wait for the tasks to finish or remove them from the set. Keep calling join_next until it returns None to empty the set. Alternatively, shutdown aborts all tasks and waits for them to finish. The set is empty when shutdown returns.
If tasks need to flush data or do other cleanup, ask them to shut down gracefully instead of aborting them. Tokio’s shutdown guide describes how to use a CancellationToken to notify tasks and a task tracker to wait for them. The token sends a request. Each task must listen for cancellation and run its own cleanup before exiting. By contrast, abort requests cancellation of the task.
Keep task lifetime separate from cancellation safety
Future cancellation safety describes whether interrupting a specific asynchronous operation leaves its state or data correct. It does not determine what happens to a spawned task when its JoinHandle is dropped. Tokio documents that awaiting &mut JoinHandle is cancel safe in tokio::select!. If another branch wins, the task’s output is not lost. This guarantee applies to waiting on the handle. Dropping an ordinary handle still detaches the task. For information about interrupted operations, see designing cancellation-safe Rust async operations.
The runtime choice is a separate issue. Tokio’s documented drop behavior for an ordinary JoinHandle is the same with a current-thread or multi-thread runtime. The runtime determines where Tokio schedules tasks. The handle determines whether the owner can join or control a spawned task.