wasi-preview1-component-adapter: fd_write does not invalidate cached InputStream, causing stale reads

Open Beginner friendly
#15,296 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@dpfens is already working on this.

Since Nov 20, 2026.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust, wasm
Domain
backend

Research direction

Start with crates/wasi-preview1-component-adapter/src/descriptors.rs and the get_read_stream implementation, then trace sequential writes in crates/wasi-preview1-component-adapter/src/lib.rs around fd_write. Reproduce the stale second read using the standalone C program and adapter commands in the issue. Done means the second read returns c after the write, while direct P1 behavior remains as expected.

Written by the indexing model from the issue text.

Description

wasi

wasi-preview1-component-adapter: fd_write does not invalidate cached InputStream, causing stale reads

Summary

After read → write → read on one O_RDWR descriptor, the second read returns the byte just written at offset 1 ('A') rather than the byte at the new shared offset 2 ('c'). The process exits normally and the on-disk file is correct; only the returned buffer is stale.

Confirmed with the standalone C program below, without any custom test driver or trace format. Each lane reproduced the same output 5/5 times in fresh directories on Wasmtime 46.0.0 (Release and Debug), 47.0.2, and 50.0.1. Wasmtime 50.0.1 was the latest upstream release at verification time.

Minimal reproducer (repro.c)

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main(void) {
    char first = 0, second = 0;
    int fd = open("file.txt", O_CREAT | O_TRUNC | O_WRONLY, 0644);
    write(fd, "abcdefghijklmnop", 16);
    close(fd);
    fd = open("file.txt", O_RDWR);
    errno = 0;
    ssize_t read1 = read(fd, &first, 1);
    printf("read #1: rv=%zd errno=%d byte=%c\n", read1, errno, first);
    errno = 0;
    ssize_t written = write(fd, "A", 1);
    printf("write: rv=%zd errno=%d (%s)\n", written, errno, strerror(errno));
    errno = 0;
    ssize_t read2 = read(fd, &second, 1);
    printf("read #2: rv=%zd errno=%d byte=%c (expected c, stale value A)\n",
           read2, errno, second);
    close(fd);
    return 0;
}

Exact commands

Run in a new empty directory. The first command downloads the official v50.0.1 command adapter; clang, wasm-tools, and wasmtime must be on the stated paths or adjusted to local installations.

curl -fLO https://github.com/bytecodealliance/wasmtime/releases/download/v50.0.1/wasi_snapshot_preview1.command.wasm
/opt/wasi-sdk/bin/clang --target=wasm32-wasip1 -std=c17 -O2 -Wall -Wextra -Werror repro_rc4_stale_read_after_write.c -o repro_p1.wasm
wasm-tools component new repro_p1.wasm --adapt wasi_snapshot_preview1=wasi_snapshot_preview1.command.wasm -o repro_adapter.wasm
wasmtime run --dir . repro_p1.wasm; wasmtime run --dir . repro_adapter.wasm

Expected vs. actual output (Wasmtime 50.0.1)

Direct P1 (expected):

read #1: rv=1 errno=0 byte=a
write: rv=1 errno=0 (Success)
read #2: rv=1 errno=0 byte=c (expected c, stale value A)

Adapter P2 (actual):

read #1: rv=1 errno=0 byte=a
write: rv=1 errno=0 (Success)
read #2: rv=1 errno=0 byte=A (expected c, stale value A)

Root cause

get_read_stream creates an InputStream at the then-current file.position and caches it (descriptors.rs:44-66). fd_write later advances file.position after a successful sequential write (lib.rs:1747-1762) but does not invalidate streams.input. The next read therefore reuses the host stream's old offset. The same code remains in v50.0.1.

Suggested fix and causal evidence

After a successful sequential file write advances the P1 position, invalidate the cached input stream (for example, drop(streams.input.take())) so a later read is created at the updated position. A minimal v46 patch changed the second read from 'A' to 'c' 3/3; reverting restored 'A' 3/3.

Dominant language
Rust
Stars
18.9k
Forks
2k
Avg merge
1d 6h
Merged PRs (30d)
238

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from bytecodealliance/wasmtime

All issues in bytecodealliance/wasmtime

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.