Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

wasi: fix zero-length blocking reads on file streams (#15132)

Xia Chao8b8dbb4217cc
diff --git a/crates/test-programs/src/bin/p2_file_read_write.rs b/crates/test-programs/src/bin/p2_file_read_write.rs
index 0c2b82045c..4c78cee357 100644
--- a/crates/test-programs/src/bin/p2_file_read_write.rs
+++ b/crates/test-programs/src/bin/p2_file_read_write.rs
@@ -51,6 +51,13 @@ fn main() {
assert_eq!(buf, b"\0\0\0\0\0Hello, World!");
drop(ready);
drop(stream);
+
+ // Zero-length blocking reads on an open stream must not report `closed`.
+ let stream = file.read_via_stream(0).unwrap();
+ assert_eq!(stream.blocking_read(0).unwrap(), b"");
+ assert_eq!(stream.blocking_skip(0).unwrap(), 0);
+ drop(stream);
+
drop(file);
dir.unlink_file_at(filename).unwrap();
diff --git a/crates/wasi/src/p2/filesystem.rs b/crates/wasi/src/p2/filesystem.rs
index 0802b913b1..9b80e3d2db 100644
--- a/crates/wasi/src/p2/filesystem.rs
+++ b/crates/wasi/src/p2/filesystem.rs
@@ -147,6 +147,9 @@ impl InputStream for FileInputStream {
/// Specialized blocking_* variant to bypass tokio's task spawning & joining
/// overhead on synchronous file I/O.
async fn blocking_read(&mut self, size: usize) -> StreamResult<Bytes> {
+ if size == 0 {
+ return self.read(size);
+ }
self.wait_ready().await;
// Before we defer to the regular `read`, make sure it has data ready to go: