Repository navigation
Conversation
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>
|
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. |
|
It shows up when a file is read while something else is still writing to it, like a |
|
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? |
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:
eofcallback ofwasi_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.file_read_eofnow also closes the read state, unless another thread is still waiting to read from it.file_seekis unchanged: since the stream is now dropped at end of file, seeking back to the same offset reads the new data as well.Testing
read-after-eof.c, reads after the file grew, after seeking to the current offset, and after seeking to the start. It fails onmainfor wasip2 and wasip3 and passes with this change.CHECK_SYMBOLS=ON): wasip1 225/225, wasip2 524/524, wasip3 528/528, and wasip3 withENABLE_COOP_THREADS=ON966/966.clang-format --dry-run -Werrorwith clang-format 23.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_seekor elsewhere.🤖 Generated with Claude Code