forked from tokio-rs/tokio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reads and buffered reads from a `tokio::io::empty` were always marked as ready. That makes sense, given that there is nothing to wait for. However, doing repeated reads on the `empty` could stall the event loop and prevent other tasks from making progress. This change uses tokio's coop system to yield control back to the executor when appropriate. Note that the issue that originally triggered this PR is not fixed yet, because the `timeout` function will not poll the timer after empty::read runs out of budget. A different change will be needed to address that. Refs: tokio-rs#4291
- Loading branch information
Showing
2 changed files
with
37 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use tokio::io::{AsyncBufReadExt, AsyncReadExt}; | ||
|
||
#[tokio::test] | ||
async fn empty_read_is_cooperative() { | ||
tokio::select! { | ||
biased; | ||
|
||
_ = async { | ||
loop { | ||
let mut buf = [0u8; 4096]; | ||
let _ = tokio::io::empty().read(&mut buf).await; | ||
} | ||
} => {}, | ||
_ = tokio::task::yield_now() => {} | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn empty_buf_reads_are_cooperative() { | ||
tokio::select! { | ||
biased; | ||
|
||
_ = async { | ||
loop { | ||
let mut buf = String::new(); | ||
let _ = tokio::io::empty().read_line(&mut buf).await; | ||
} | ||
} => {}, | ||
_ = tokio::task::yield_now() => {} | ||
} | ||
} |