Skip to content

Using timeout hangs a program on wasm32-wasip1 #7036

@surban

Description

@surban

Version

tokio-hang v0.1.0 (/data/surban/dev/rust-wasi-web/tokio-hang)
└── tokio v1.42.0 (/data/surban/dev/tokio/tokio)
    └── tokio-macros v2.4.0 (proc-macro) (/data/surban/dev/tokio/tokio-macros)

Platform
wasm32-wasip1 and wasm32-wasip1-threads
using wasmtime executor or wasmer

Description
Using the timeout function makes the following program hang on wasm32-wasip1.
The program works fine either on

  • target x86_64-unknown-linux-gnu
  • also on wasm32-wasip1 without the timeout wrapper

I tried this code:

Available at https://github.com/surban/tokio-hang

use std::time::Duration;

use futures::{SinkExt, StreamExt};
use tokio::time::timeout;

async fn send_task(mut tx: futures::channel::mpsc::Sender<u8>) {
    println!("feeding 0");
    tx.feed(0).await.unwrap();
    println!("flushing 0");
    tx.flush().await.unwrap();

    println!("feeding 1");
    tx.feed(1).await.unwrap();
    println!("flushing 1");
    tx.flush().await.unwrap();
}

async fn recv_task(mut rx: futures::channel::mpsc::Receiver<u8>) {
    loop {
        println!("waiting to receive");
        match rx.next().await {
            Some(msg) => println!("received: {msg}"),
            None => break,
        }
    }

    println!("end of receive");
}

#[tokio::main(flavor = "current_thread")]
async fn main() {
    let qlen = 0;
    let (a_tx, b_rx) = futures::channel::mpsc::channel::<u8>(qlen);

    let task = async move { tokio::join!(send_task(a_tx), recv_task(b_rx)) };
    timeout(Duration::from_secs(60), task).await.unwrap();

    // works without timeout:
    // task.await;
}

I expected to see the following output (it works on x86_64-unknown-linux-gnu):

feeding 0
flushing 0
waiting to receive
received: 0
waiting to receive
feeding 1
flushing 1
received: 1
waiting to receive
end of receive

Instead, this happened on wasm32-wasip1:

feeding 0
flushing 0
waiting to receive
received: 0
waiting to receive

Then it hangs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-tokioArea: The main tokio crateC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions