From 98db3c1e72b65de3f0fa356f45d6860a81437ae0 Mon Sep 17 00:00:00 2001 From: Pat Hickey Date: Fri, 25 Aug 2023 13:48:29 -0700 Subject: [PATCH] handle errors from try_read_buf a bit --- crates/wasi/src/preview2/tcp.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/wasi/src/preview2/tcp.rs b/crates/wasi/src/preview2/tcp.rs index ef8e0974519d..d577f3a769dc 100644 --- a/crates/wasi/src/preview2/tcp.rs +++ b/crates/wasi/src/preview2/tcp.rs @@ -81,9 +81,16 @@ impl HostInputStream for HostTcpSocketInner { } let mut buf = bytes::BytesMut::with_capacity(size); - let n = self.stream.try_read_buf(&mut buf)?; + let n = match self.stream.try_read_buf(&mut buf) { + Ok(n) => n, + Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => 0, + Err(_) => { + // FIXME: this is a closed stream, but we need to record it for future calls to + // ready + return Ok((bytes::Bytes::new(), StreamState::Closed)); + } + }; - // TODO: how do we detect a closed stream? buf.truncate(n); Ok((buf.freeze(), StreamState::Open)) }