epitaphial opened issue #15326:
wasi: zero-length blocking file reads close the input stream and make later reads return EOF
Summary
A zero-length sequential read on a regular file marks the host
FileInputStreamasReadState::Closed. The preview1 adapter maps that state to a successful zero-byte read and keeps the stream cached, so all subsequent sequential reads return EOF even though the file still contains data.This is not a one-shot short read: the stream remains closed until it is discarded and recreated (for example by
fd_seek). Independent offset reads continue to work because they bypass the cached stream.Minimal reproducer
#define _POSIX_C_SOURCE 200809L #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <unistd.h> int main(void) { char a = 0, b = 0, c = 0, d = 0; int fd = open("f", O_RDONLY); ssize_t zero = read(fd, &a, 0); ssize_t next1 = read(fd, &b, 1); ssize_t next2 = read(fd, &c, 1); ssize_t offset = pread(fd, &d, 1, 0); lseek(fd, 0, SEEK_SET); char recovered = 0; ssize_t after_seek = read(fd, &recovered, 1); printf("zero=%zd next1=%zd next2=%zd pread=%zd after_seek=%zd byte=%c\n", zero, next1, next2, offset, after_seek, recovered); close(fd); return 0; }Place a file named
fcontainingABCDin the working directory, then run:/opt/wasi-sdk/bin/clang --target=wasm32-wasip1 -O2 repro.c -o repro.wasm wasm-tools component new repro.wasm --adapt wasi_snapshot_preview1=wasi_snapshot_preview1.command.wasm -o repro.component.wasm wasmtime run --dir . repro.wasm wasmtime run --dir . repro.component.wasmExpected vs. actual output
Direct preview1 (also reproduced with WAMR in earlier controlled experiments):
zero=0 next1=1 next2=1 pread=1 after_seek=1 byte=AAdapter path:
zero=0 next1=0 next2=0 pread=1 after_seek=1 byte=AThe behavior was stable in three fresh-directory executions on each of v46 Release, v46 Debug, v47.0.2, v47.0.3, and v50.0.1.
Root cause
In
crates/wasi/src/p2/filesystem.rs, the specializedFileInputStream::blocking_readcalls the helperblocking_read(file, offset, size)while the stream is idle. The helper interpretsOk(0)fromsys::read_at_cursor_unspecifiedasReadState::Closed, including whensize == 0.The adapter's blocking mode in
crates/wasi-preview1-component-adapter/src/lib.rsconvertsStreamError::Closedinto an empty vector and therefore a successful zero-byte P1 read. Its cachedOnceCell<InputStream>is retained, so the next positive read sees the same closed state.
fd_seekdiscards the cached input stream and therefore masks the problem by creating a fresh stream.fd_preaduses the descriptor's offset-read path and is unaffected.Suggested fix
For a live stream, return an empty byte sequence immediately when
size == 0instead of issuing the underlying zero-length file read. Preserve the existingClosedandErrorbehavior for streams that are already in those states.A minimal patch-and-revert experiment confirmed the mechanism:
- Unpatched: 3/3 executions returned zero for both subsequent reads.
- Patched: 3/3 executions read
AandBas expected.- Reverted: 3/3 executions returned EOF again, and the bare host binary's SHA-256 was identical to the unpatched build.
This issue is distinct from reusing a stream after a real I/O error: here a valid zero-length request itself incorrectly closes the stream.
alexcrichton closed issue #15326:
wasi: zero-length blocking file reads close the input stream and make later reads return EOF
Summary
A zero-length sequential read on a regular file marks the host
FileInputStreamasReadState::Closed. The preview1 adapter maps that state to a successful zero-byte read and keeps the stream cached, so all subsequent sequential reads return EOF even though the file still contains data.This is not a one-shot short read: the stream remains closed until it is discarded and recreated (for example by
fd_seek). Independent offset reads continue to work because they bypass the cached stream.Minimal reproducer
#define _POSIX_C_SOURCE 200809L #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <unistd.h> int main(void) { char a = 0, b = 0, c = 0, d = 0; int fd = open("f", O_RDONLY); ssize_t zero = read(fd, &a, 0); ssize_t next1 = read(fd, &b, 1); ssize_t next2 = read(fd, &c, 1); ssize_t offset = pread(fd, &d, 1, 0); lseek(fd, 0, SEEK_SET); char recovered = 0; ssize_t after_seek = read(fd, &recovered, 1); printf("zero=%zd next1=%zd next2=%zd pread=%zd after_seek=%zd byte=%c\n", zero, next1, next2, offset, after_seek, recovered); close(fd); return 0; }Place a file named
fcontainingABCDin the working directory, then run:/opt/wasi-sdk/bin/clang --target=wasm32-wasip1 -O2 repro.c -o repro.wasm wasm-tools component new repro.wasm --adapt wasi_snapshot_preview1=wasi_snapshot_preview1.command.wasm -o repro.component.wasm wasmtime run --dir . repro.wasm wasmtime run --dir . repro.component.wasmExpected vs. actual output
Direct preview1 (also reproduced with WAMR in earlier controlled experiments):
zero=0 next1=1 next2=1 pread=1 after_seek=1 byte=AAdapter path:
zero=0 next1=0 next2=0 pread=1 after_seek=1 byte=AThe behavior was stable in three fresh-directory executions on each of v46 Release, v46 Debug, v47.0.2, v47.0.3, and v50.0.1.
Root cause
In
crates/wasi/src/p2/filesystem.rs, the specializedFileInputStream::blocking_readcalls the helperblocking_read(file, offset, size)while the stream is idle. The helper interpretsOk(0)fromsys::read_at_cursor_unspecifiedasReadState::Closed, including whensize == 0.The adapter's blocking mode in
crates/wasi-preview1-component-adapter/src/lib.rsconvertsStreamError::Closedinto an empty vector and therefore a successful zero-byte P1 read. Its cachedOnceCell<InputStream>is retained, so the next positive read sees the same closed state.
fd_seekdiscards the cached input stream and therefore masks the problem by creating a fresh stream.fd_preaduses the descriptor's offset-read path and is unaffected.Suggested fix
For a live stream, return an empty byte sequence immediately when
size == 0instead of issuing the underlying zero-length file read. Preserve the existingClosedandErrorbehavior for streams that are already in those states.A minimal patch-and-revert experiment confirmed the mechanism:
- Unpatched: 3/3 executions returned zero for both subsequent reads.
- Patched: 3/3 executions read
AandBas expected.- Reverted: 3/3 executions returned EOF again, and the bare host binary's SHA-256 was identical to the unpatched build.
This issue is distinct from reusing a stream after a real I/O error: here a valid zero-length request itself incorrectly closes the stream.
alexcrichton commented on issue #15326:
Already fixed in #15132
Last updated: Nov 19 2026 at 20:05 UTC