Skip to content

Commit 488891e

Browse files
authored
Clean up helper task in AsyncReadStream faster (#13105)
The previous implementation would hit EOF on the input object, then hit EOF again, and then block on sending a value over the channel. There's no need for the task to stick around and infinitely generate EOF messages, however, so exit the task as soon as the first EOF is hit and a message is sent.
1 parent df64d9d commit 488891e

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

crates/wasi/src/p2/pipe.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ impl AsyncReadStream {
165165
use tokio::io::AsyncReadExt;
166166
let mut buf = bytes::BytesMut::with_capacity(crate::MAX_READ_SIZE_ALLOC);
167167
let sent = match reader.read_buf(&mut buf).await {
168-
Ok(nbytes) if nbytes == 0 => sender.send(Err(StreamError::Closed)).await,
168+
Ok(nbytes) if nbytes == 0 => {
169+
let _ = sender.send(Err(StreamError::Closed)).await;
170+
break;
171+
}
169172
Ok(_) => sender.send(Ok(buf.freeze())).await,
170173
Err(e) => {
171174
sender
@@ -239,15 +242,12 @@ impl InputStream for AsyncReadStream {
239242
self.closed = true;
240243
Err(e)
241244
}
242-
Err(TryRecvError::Empty) => {
243-
if self.closed {
244-
// Note: if the stream is already closed it should return an error,
245-
// returning empty list would break the wasi contract (returning 0 and ready)
246-
Err(StreamError::Closed)
247-
} else {
248-
Ok(Bytes::new())
249-
}
245+
// Note: if the stream is already closed it should return an error,
246+
// returning empty list would break the wasi contract (returning 0 and ready)
247+
Err(TryRecvError::Empty | TryRecvError::Disconnected) if self.closed => {
248+
Err(StreamError::Closed)
250249
}
250+
Err(TryRecvError::Empty) => Ok(Bytes::new()),
251251
Err(TryRecvError::Disconnected) => Err(StreamError::Trap(format_err!(
252252
"AsyncReadStream sender died - should be impossible"
253253
))),

0 commit comments

Comments
 (0)