From 2aa4596dfe7378c417a4917dcfed60f14b56c964 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20A=2E=20Muci=C3=B1o?= Date: Mon, 24 Apr 2023 19:48:26 -0600 Subject: [PATCH 1/3] fd_write: Flush on every write to a file --- lib/wasi/src/syscalls/wasi/fd_write.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/wasi/src/syscalls/wasi/fd_write.rs b/lib/wasi/src/syscalls/wasi/fd_write.rs index 75078bfdea4..93448210d85 100644 --- a/lib/wasi/src/syscalls/wasi/fd_write.rs +++ b/lib/wasi/src/syscalls/wasi/fd_write.rs @@ -145,6 +145,8 @@ fn fd_write_internal( break; } } + handle.flush().await.map_err(map_io_err)?; + Ok(written) } )? From 10ee5d8730e75664eee8bb46e4b7e0e73d976db2 Mon Sep 17 00:00:00 2001 From: "Ibrahim M. Akrab" Date: Fri, 28 Apr 2023 14:36:21 +0200 Subject: [PATCH 2/3] only flush on stdio --- lib/wasi/src/syscalls/wasi/fd_write.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/wasi/src/syscalls/wasi/fd_write.rs b/lib/wasi/src/syscalls/wasi/fd_write.rs index 93448210d85..15fb7a0a2cd 100644 --- a/lib/wasi/src/syscalls/wasi/fd_write.rs +++ b/lib/wasi/src/syscalls/wasi/fd_write.rs @@ -145,8 +145,9 @@ fn fd_write_internal( break; } } - handle.flush().await.map_err(map_io_err)?; - + if is_stdio { + handle.flush().await.map_err(map_io_err)?; + } Ok(written) } )? From 4cc5fe86c965e5ef6455bb3b10d19703d19fdb2e Mon Sep 17 00:00:00 2001 From: "Ibrahim M. Akrab" Date: Fri, 28 Apr 2023 14:38:35 +0200 Subject: [PATCH 3/3] test stdio flushing without newline --- lib/wasi/tests/stdio.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/wasi/tests/stdio.rs b/lib/wasi/tests/stdio.rs index bcdabf8d0ca..9875aae75b9 100644 --- a/lib/wasi/tests/stdio.rs +++ b/lib/wasi/tests/stdio.rs @@ -52,13 +52,13 @@ async fn test_stdout() { (export "memory" (memory 0)) ;; Write 'hello world\n' to memory at an offset of 8 bytes - ;; Note the trailing newline which is required for the text to appear - (data (i32.const 8) "hello world\n") + ;; No trailing newline is required since all stdio writes are flushing + (data (i32.const 8) "hello world") (func $main (export "_start") ;; Creating a new io vector within linear memory (i32.store (i32.const 0) (i32.const 8)) ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string - (i32.store (i32.const 4) (i32.const 12)) ;; iov.iov_len - The length of the 'hello world\n' string + (i32.store (i32.const 4) (i32.const 11)) ;; iov.iov_len - The length of the 'hello world\n' string (call $fd_write (i32.const 1) ;; file_descriptor - 1 for stdout @@ -93,7 +93,7 @@ async fn test_stdout() { let mut stdout_str = String::new(); stdout_rx.read_to_string(&mut stdout_str).await.unwrap(); let stdout_as_str = stdout_str.as_str(); - assert_eq!(stdout_as_str, "hello world\n"); + assert_eq!(stdout_as_str, "hello world"); } async fn test_env() {