Stream: git-wasmtime

Topic: wasmtime / issue #15326 wasi: zero-length blocking file r...


view this post on Zulip Wasmtime GitHub notifications bot (Nov 17 2026 at 10:09):

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 FileInputStream as ReadState::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 f containing ABCD in 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.wasm

Expected 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=A

Adapter path:

zero=0 next1=0 next2=0 pread=1 after_seek=1 byte=A

The 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 specialized FileInputStream::blocking_read calls the helper blocking_read(file, offset, size) while the stream is idle. The helper interprets Ok(0) from sys::read_at_cursor_unspecified as ReadState::Closed, including when size == 0.

The adapter's blocking mode in crates/wasi-preview1-component-adapter/src/lib.rs converts StreamError::Closed into an empty vector and therefore a successful zero-byte P1 read. Its cached OnceCell<InputStream> is retained, so the next positive read sees the same closed state.

fd_seek discards the cached input stream and therefore masks the problem by creating a fresh stream. fd_pread uses the descriptor's offset-read path and is unaffected.

Suggested fix

For a live stream, return an empty byte sequence immediately when size == 0 instead of issuing the underlying zero-length file read. Preserve the existing Closed and Error behavior for streams that are already in those states.

A minimal patch-and-revert experiment confirmed the mechanism:

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.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 17 2026 at 16:54):

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 FileInputStream as ReadState::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 f containing ABCD in 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.wasm

Expected 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=A

Adapter path:

zero=0 next1=0 next2=0 pread=1 after_seek=1 byte=A

The 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 specialized FileInputStream::blocking_read calls the helper blocking_read(file, offset, size) while the stream is idle. The helper interprets Ok(0) from sys::read_at_cursor_unspecified as ReadState::Closed, including when size == 0.

The adapter's blocking mode in crates/wasi-preview1-component-adapter/src/lib.rs converts StreamError::Closed into an empty vector and therefore a successful zero-byte P1 read. Its cached OnceCell<InputStream> is retained, so the next positive read sees the same closed state.

fd_seek discards the cached input stream and therefore masks the problem by creating a fresh stream. fd_pread uses the descriptor's offset-read path and is unaffected.

Suggested fix

For a live stream, return an empty byte sequence immediately when size == 0 instead of issuing the underlying zero-length file read. Preserve the existing Closed and Error behavior for streams that are already in those states.

A minimal patch-and-revert experiment confirmed the mechanism:

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.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 17 2026 at 16:54):

alexcrichton commented on issue #15326:

Already fixed in #15132


Last updated: Nov 19 2026 at 20:05 UTC