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 makes reads on empty objects cooperative. One of every two reads will return `Poll::Pending`, which will give the executor a chance to keep making progress elsewhere. Fixes: tokio-rs#4291
- Loading branch information
Showing
2 changed files
with
83 additions
and
7 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,57 @@ | ||
use std::time::Duration; | ||
use tokio::io::{empty, AsyncBufReadExt, AsyncReadExt}; | ||
use tokio::time::timeout; | ||
|
||
#[tokio::test] | ||
async fn empty_read_is_cooperative() { | ||
// the test would likely hang if reads on `empty` weren't | ||
// cooperative | ||
let _ = timeout(Duration::from_millis(1000), async { | ||
loop { | ||
let mut buf = [0u8; 4096]; | ||
let _ = tokio::io::empty().read(&mut buf).await; | ||
} | ||
}) | ||
.await; | ||
} | ||
|
||
#[tokio::test] | ||
async fn repeated_reads_on_same_empty_are_cooperative() { | ||
// the test would likely hang if reads on `empty` weren't | ||
// cooperative | ||
let _ = timeout(Duration::from_millis(1000), async { | ||
let mut empty = tokio::io::empty(); | ||
loop { | ||
let mut buf = [0u8; 4096]; | ||
let _ = empty.read(&mut buf).await; | ||
} | ||
}) | ||
.await; | ||
} | ||
|
||
#[tokio::test] | ||
async fn empty_buf_reads_are_cooperative() { | ||
// the test would likely hang if reads on `empty` weren't | ||
// cooperative | ||
let _ = timeout(Duration::from_millis(1000), async { | ||
loop { | ||
let mut buf = String::new(); | ||
let _ = tokio::io::empty().read_line(&mut buf).await; | ||
} | ||
}) | ||
.await; | ||
} | ||
|
||
#[tokio::test] | ||
async fn repeated_empty_buf_reads_are_cooperative() { | ||
// the test would likely hang if reads on `empty` weren't | ||
// cooperative | ||
let _ = timeout(Duration::from_millis(1000), async { | ||
let mut empty = tokio::io::empty(); | ||
loop { | ||
let mut buf = String::new(); | ||
let _ = empty.read_line(&mut buf).await; | ||
} | ||
}) | ||
.await; | ||
} |