You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A lint which checks for blocking functions inside an async block.
One I know of (and the cause of a bug I ran into today) is std::thread::sleep, but there are others. (file operations, networking, ...). If such a function has an async counterpart, then the user could be warned of this fact.
Lint Name
unwise-block
Category
suspicious
Advantage
warn of badly behaving async programs
Drawbacks
might be too noisy
Example
use std::thread::sleep;use std::time::Duration;use tokio::signal::unix::signal;use tokio::signal::unix::SignalKind;use tokio::sync::mpsc;asyncfnrx(rx:&mut mpsc::Receiver<()>){loop{sleep(Duration::new(1,0));if rx.try_recv().is_ok(){println!("got one splivet");}}}asyncfntx(tx: mpsc::Sender<()>){letmut stream = signal(SignalKind::user_defined1()).unwrap();loop{
stream.recv().await;println!("sending one splivet");
tx.send(()).await.unwrap();}}#[tokio::main(flavor = "current_thread")]asyncfnmain(){let(stx,mut srx) = mpsc::channel::<()>(5);
tokio::join!(tx(stx), rx(&mut srx));}
Could be written as:
use std::thread::sleep;use std::time::Duration;use tokio::signal::unix::signal;use tokio::signal::unix::SignalKind;use tokio::sync::mpsc;asyncfnrx(rx:&mut mpsc::Receiver<()>){loop{
tokio::time::sleep(Duration::new(1,0)).await;if rx.try_recv().is_ok(){println!("got one splivet");}}}asyncfntx(tx: mpsc::Sender<()>){letmut stream = signal(SignalKind::user_defined1()).unwrap();loop{
stream.recv().await;println!("sending one splivet");
tx.send(()).await.unwrap();}}#[tokio::main(flavor = "current_thread")]asyncfnmain(){let(stx,mut srx) = mpsc::channel::<()>(5);
tokio::join!(tx(stx), rx(&mut srx));}
Note the different way to sleep in the rx function.
The text was updated successfully, but these errors were encountered:
I like this idea! Although there are some exceptions we should probably make, such as with Mutex. Using an async Mutex only makes sense when the guard is held across an await point. In most other cases, it's totally fine to use the Mutex from the standard library (or parking_lot), even in an async context
What it does
A lint which checks for blocking functions inside an async block.
One I know of (and the cause of a bug I ran into today) is
std::thread::sleep
, but there are others. (file operations, networking, ...). If such a function has an async counterpart, then the user could be warned of this fact.Lint Name
unwise-block
Category
suspicious
Advantage
Drawbacks
Example
Could be written as:
Note the different way to sleep in the
rx
function.The text was updated successfully, but these errors were encountered: