Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Reopen a file's read stream after it reaches end of file - #890

Open
libsmol wants to merge 1 commit into
WebAssembly:mainfrom
libsmol:read-after-eof
Open

libsmol wants to merge 1 commit into
WebAssembly:mainfrom
libsmol:read-after-eof

Conversation

@libsmol

@libsmol libsmol commented Oct 25, 2026

Copy link
Copy Markdown

Fixes #889.

On wasip2 and wasip3, reads from a file go through a cached input stream. Once that stream reaches the end of the file it stays closed, so data appended to the file later is never seen, even after seeking back to the same offset (details and a reproducer in the issue).

This change drops the stream when it reaches the end of the file, so the next read opens a new one at the current offset:

  • The eof callback of wasi_read_t, previously wasip3-only, is now also used on wasip2, where it's optional. Files set it to drop the read stream and its pollable. TCP sockets and stdin leave it unset, so their behavior doesn't change.
  • On wasip3, file_read_eof now also closes the read state, unless another thread is still waiting to read from it.

file_seek is unchanged: since the stream is now dropped at end of file, seeking back to the same offset reads the new data as well.

Testing

  • A new test, read-after-eof.c, reads after the file grew, after seeking to the current offset, and after seeking to the start. It fails on main for wasip2 and wasip3 and passes with this change.
  • The full test suite passes locally (clang 23, CHECK_SYMBOLS=ON): wasip1 225/225, wasip2 524/524, wasip3 528/528, and wasip3 with ENABLE_COOP_THREADS=ON 966/966.
  • The changed files pass clang-format --dry-run -Werror with clang-format 23.
  • With RustPython linked against the patched wasip2 sysroot, Python file objects see data appended by another descriptor, and two SQLite connections to the same database work.

This pull request was prepared by an AI coding assistant (Claude Code, running Anthropic's Claude Opus 5.5) on behalf of the libsmol project. We've tested it as described above, but we're happy to rework it if you'd prefer a different approach, for example handling this in file_seek or elsewhere.

🤖 Generated with Claude Code

On wasip2 and wasip3, reads from a file go through an input stream that
is created lazily and kept until a seek moves the offset. Once that
stream reaches the end of the file it stays closed, so after a read
returns 0, data appended to the file later is never seen, not even
after an `lseek` back to the same offset. A program following a
growing log file never sees new lines, and SQLite reports "file is not
a database" when a second connection reads a page that another
connection wrote after the first one had found the file empty.

On wasip2 the `eof` callback of `wasi_read_t`, previously wasip3-only,
is now available too and optional there. Files use it to drop the read
stream and its pollable when the stream reports that it is closed, so
the next read opens a new stream at the same offset. On wasip3 the
existing callback now also closes the read state, unless another thread
is still waiting to read from it.

The new `read-after-eof` test reads after the file grew, after seeking
to the current offset, and after seeking to the start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@alexcrichton

Copy link
Copy Markdown
Collaborator

If there's a human at the other end of this I'm happy to engage, but otherwise I'd rather not indirectly review AI output with no human in the loop.

@libsmol

libsmol commented Oct 26, 2026

Copy link
Copy Markdown
Author

If there's a human at the other end of this I'm happy to engage, but otherwise I'd rather not indirectly review AI output with no human in the loop.

Sorry, I should have made this clear from the start: I'm a real person, and libsmol is my project.

I'm working on smol, a coding agent that does everything by running Python (RustPython, embedded). I wanted to sandbox it, so I tried building RustPython for wasm32-wasip2 and running it under wasmtime. That's where I ran into this. Once a Python file object had read to the end, it never saw anything another fd appended later. SQLite broke the same way: opening a second connection to the same db gave me "file is not a database".

I reproduced it outside Python with the small C program in #889 and tracked it down to file.c: the read stream stays cached after it reports closed at EOF, and file_seek only resets it when the offset changes. Once I knew where the problem was, I used an AI assistant (Claude Code) to write the patch, the test and the PR text. That's why it reads the way it does.

@libsmol

libsmol commented Oct 26, 2026

Copy link
Copy Markdown
Author

It shows up when a file is read while something else is still writing to it, like a tail -f loop or a SQLite database that another connection keeps adding to.

@alexcrichton

Copy link
Copy Markdown
Collaborator

To me this seems like it's likely a bug in Wasmtime itself rather than something for wasi-libc to paper over. Would you be up for looking into the implementation over there to see if it's fixable there?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wasip2/wasip3: reads never see data appended to a file after reaching EOF

2 participants