# Tokio isn't aborting tasks

**URL:** <https://users.rust-lang.org/t/tokio-isnt-aborting-tasks/127289>\
**Category:** help\
**Created:** [March 21, 2025, 2:16pm UTC](https://users.rust-lang.org/t/tokio-isnt-aborting-tasks/127289 "2025-03-21T14:16:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![maidanalle](https://avatars.discourse-cdn.com/v4/letter/m/85f322/32.png) [@maidanalle](https://users.rust-lang.org/u/maidanalle)\
**Post date:** [March 21, 2025, 2:16pm UTC](https://users.rust-lang.org/t/tokio-isnt-aborting-tasks/127289/1 "2025-03-21T14:16:38Z")

</div>

Hi,

I'm trying to implement a timeout for async tasks in tokio and for some reason it just isn't working. Is there something missing in the code? Is this a bug?

The test implementation should just:

- spawn a concurrent task with 2 concurrent tasks within
  - `long_task` takes 10 seconds
  - `short_task` takes 5 seconds
  - each task print out their name and current time every second then sleeps with an await
  - a "race" is started with `tokio::select!`
  - `short_task` should finish first and abort `long_task` while it sleeps

- This concurrent task is awaited and should end after the `tokio::select!`
- The main thread waits a little longer after the concurrent task ends to ensure its sub tasks aren't printing anymore

> **Code**
>
> ```rs
> use std::time::Duration;
> 
> #[tokio::main]
> async fn main() {
> let handle = tokio::spawn(async move {
> let timeout_handle = tokio::spawn(timout_task());
> 
> let wait_20_handle = tokio::spawn(long_task());
> let abort_handle_long = timeout_handle.abort_handle();
> 
> // Race to the first one that's finished and cancel the loser
> tokio::select! {
> _ = timeout_handle => {
> println!("Timeout of task");
> abort_handle_long.abort();
> }
> _ = wait_20_handle => {
> println!("Completed task wait_20");
> }
> }
> });
> 
> handle.await.unwrap();
> 
> println!("Waiting extra seconds");
> tokio::time::sleep(Duration::from_secs(10)).await
> }
> 
> async fn timout_task(){
> for second in 1..=5 {
> println!("wait short: Waited for {}", second);
> tokio::time::sleep(Duration::from_secs(1)).await;
> }
> }
> async fn long_task(){
> for second in 1..=10 {
> println!("wait long: Waited for {}", second);
> tokio::time::sleep(Duration::from_secs(1)).await;
> }
> println!("Shouldn't print this!");
> }
> 
> ```

[Rust playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=071d7c8b4958bb68de53fb65ee16ad8f)

**Expected result:**

```rust
wait long: Waited for 1
wait short: Waited for 1
wait short: Waited for 2
wait long: Waited for 2
wait long: Waited for 3
wait short: Waited for 3
wait short: Waited for 4
wait long: Waited for 4
wait short: Waited for 5
wait long: Waited for 5
wait long: Waited for 6
Timeout of task
Waiting extra seconds

```

**Actual result**

```rust
wait long: Waited for 1
wait short: Waited for 1
wait short: Waited for 2
wait long: Waited for 2
wait long: Waited for 3
wait short: Waited for 3
wait short: Waited for 4
wait long: Waited for 4
wait short: Waited for 5
wait long: Waited for 5
wait long: Waited for 6
Timeout of task
Waiting extra seconds
wait long: Waited for 7
wait long: Waited for 8
wait long: Waited for 9
wait long: Waited for 10
Shouldn't print this!

```

# Notes

The [doc](https://docs.rs/tokio/latest/tokio/task/index.html#cancellation) says

> When tasks are shut down, it will stop running at whichever .await it has yielded at. All local variables are destroyed by running their destructor. Once shutdown has completed, awaiting the JoinHandle will fail with a cancelled error.

Further on what "yielding" is

> When one of these methods are called, the task is signalled to shut down next time it yields at an .await point.

So I'm expecting it to cancel at the `tokio::time::sleep(...).await` call.

Weirdly enough, aborting a single spawn doesn't pose any problems

> **Code and output**
>
> ```rs
> use tokio::time::{sleep, Duration};
> 
> #[tokio::main]
> async fn main() {
> let handle = tokio::spawn(async {
> println!("Task started.");
> sleep(Duration::from_secs(5)).await;
> println!("This will not be printed if aborted.");
> });
> 
> tokio::time::sleep(Duration::from_secs(1)).await;
> handle.abort();
>     
> sleep(Duration::from_secs(8)).await;
> 
> println!("Task aborted.");
> }
> 
> ```
> 
> **Output**
> 
> ```rust
> Task started.
> Task aborted.
> 
> ```

---

<div class="post-metadata">

**Author:** ![nerditation](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@nerditation](https://users.rust-lang.org/u/nerditation)\
**Post date:** [March 21, 2025, 2:37pm UTC](https://users.rust-lang.org/t/tokio-isnt-aborting-tasks/127289/2 "2025-03-21T14:37:08Z")

</div>

you are selecting the wrong handles

---

<div class="post-metadata">

**Author:** ![jofas](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jofas/32/30690_2.png) [@jofas](https://users.rust-lang.org/u/jofas)\
**Post date:** [March 21, 2025, 2:37pm UTC](https://users.rust-lang.org/t/tokio-isnt-aborting-tasks/127289/3 "2025-03-21T14:37:08Z")

</div>

Your abort handle is for the wrong task:

```diff
        let timeout_handle = tokio::spawn(timout_task());

        let wait_20_handle = tokio::spawn(long_task());
- let abort_handle_long = timeout_handle.abort_handle();
+ let abort_handle_long = wait_20_handle.abort_handle();

```

[Playground.](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=8f5dcde4ef735325981d69ba3deebe5b)

---

<div class="post-metadata">

**Author:** ![maidanalle](https://avatars.discourse-cdn.com/v4/letter/m/85f322/32.png) [@maidanalle](https://users.rust-lang.org/u/maidanalle)\
**Post date:** [March 21, 2025, 3:20pm UTC](https://users.rust-lang.org/t/tokio-isnt-aborting-tasks/127289/4 "2025-03-21T15:20:25Z")

</div>

Godammit 😂 I stared at the code so long and even asked friends to look at it. My rubberduck was of no help either.

Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [June 19, 2025, 3:21pm UTC](https://users.rust-lang.org/t/tokio-isnt-aborting-tasks/127289/5 "2025-06-19T15:21:07Z")

</div>

Closed on June 19, 2025, 3:21pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
