I've been able to reduce the problem to a fairly minimal example below. Basically I have two async tasks that communicate via a channel.
It seems to work properly about 80% of them time. The other 20% of the time, after launching the application, the sending task will run every 1 second, but the receiving task just blocks on recv
, never receiving anything.
I'm most likely doing something stupid here.. could anyone please point out my mistake?
async fn f1(ch_in:async_std::sync::Sender<String>) {
loop {
println!("here 1");
ch_in.send("hello".into()).await;
println!("here 2");
std::thread::sleep(std::time::Duration::from_millis(1000));
}
}
async fn f2(ch_out:async_std::sync::Receiver<String>) {
loop {
println!("here 3");
let s = ch_out.recv().await.unwrap();
println!("here 4");
println!("{}", s);
}
}
fn main() {
let (ch_in, ch_out) = async_std::sync::channel(1000);
async_std::task::block_on(async {
async_std::task::spawn(f1(ch_in));
f2(ch_out).await;
});
}