Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WASI fd_write to stdout is buffered and should be unbuffered #363

Closed
kanaka opened this issue Apr 16, 2019 · 0 comments · Fixed by #368
Closed

WASI fd_write to stdout is buffered and should be unbuffered #363

kanaka opened this issue Apr 16, 2019 · 0 comments · Fixed by #368
Labels
bug Something isn't working 📦 lib-wasi About wasmer-wasi

Comments

@kanaka
Copy link

kanaka commented Apr 16, 2019

Describe the bug

The current implementation of fd_write in wasmer 0.3.0 is buffered when writing to stdout (fd 1). The fd_write WASI API is designed to be similar to the POSIX write/writev calls and those write unbuffered output to stdout. In wasmtime fd_write is unbuffered. In my specific case, this prevents fd_write from being used to print a prompt that doesn't have a newline.

Steps to reproduce

The following wat code:

(module
  (import "wasi_unstable" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))
  (import "wasi_unstable" "fd_read" (func $fd_read (param i32 i32 i32 i32) (result i32)))

  (memory 10)
  (export "memory" (memory 0))

  ;; prompt __wasi_ciovec_t struct
  (data (i32.const 0) "\08\00\00\00") ;; buf: pointer to prompt string
  (data (i32.const 4) "\02\00\00\00") ;; buf_len: 2 characters
  ;; string
  (data (i32.const 8) "> ")

  ;; read buf __wasi_ciovec_t struct
  (data (i32.const 16) "\18\00\00\00") ;; buf: pointer to hello string
  (data (i32.const 20) "\64\00\00\00") ;; buf_len: 100 characters max
  ;; string
  (data (i32.const 24) "\00") ;; buf (of 100 characters) to hold read in string

  (func $main (local i32)
    (drop (call $fd_write (i32.const 1) (i32.const 0) (i32.const 1) (i32.const 256)))
    (drop (call $fd_read (i32.const 0) (i32.const 16) (i32.const 1) (i32.const 256)))
    (drop (call $fd_write (i32.const 0) (i32.const 16) (i32.const 1) (i32.const 256)))
  )

  ;;(start $main)
  (export "_start" $main)
)

results in the following behavior:

$ ~/.wasmer/bin/wasmer run wasitests/echo.wasm 
user input
> 

Expected behavior

The prompt should be printed before the user input. Here is the correct behavior from wasmtime:

$ wasmtime wasitests/echo.wasm
> user input
user input
@kanaka kanaka added the bug Something isn't working label Apr 16, 2019
@Hywan Hywan added the 📦 lib-wasi About wasmer-wasi label Apr 16, 2019
bors bot added a commit that referenced this issue Apr 20, 2019
368: fix write to flush buffer; fix warnings r=MarkMcCaskey a=MarkMcCaskey

resolves #363 

Co-authored-by: Mark McCaskey <[email protected]>
@bors bors bot closed this as completed in #368 Apr 20, 2019
surban pushed a commit to rust-wasi-web/wwrr that referenced this issue Nov 9, 2024
Improvements

fix: Automatically fix up the `wasmer_js_bg.wasm` URL when loading from `unpkg.com`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working 📦 lib-wasi About wasmer-wasi
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants