Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasi: Handle read(0) with file streams #8611

Merged

Conversation

elliottt
Copy link
Member

The read method on streams should support a size argument of zero, to test if the stream is still open. However, the implementation of read for open files was unconditionally treating a read that would return zero bytes as an indication that the stream was closed.

This PR fixes this bug by handling a read(0) as an explicit test to see if the current position is at the end of the file.

@elliottt elliottt requested a review from a team as a code owner May 14, 2024 00:09
@elliottt elliottt requested review from alexcrichton and removed request for a team May 14, 2024 00:09
@elliottt elliottt force-pushed the trevor/wasi-file-stream-read-zero branch from b31e9a6 to a8bd804 Compare May 14, 2024 00:15
@github-actions github-actions bot added the wasi Issues pertaining to WASI label May 14, 2024
Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little hesitant to provide such a strong guarantee that a read of size 0 is effectively a stat on all files. Would it work to instead pass the read size to read_result and don't return Closed if the read size is zero?

@elliottt
Copy link
Member Author

elliottt commented May 14, 2024

I don't think that works, as cap_std::fs::File::read_at won't signal any error for a read at the end of a file, of 0 bytes. That means that a read(0) will succeed indicating that it read zero bytes, and a subsequent non-zero read will fail because the file is closed. I agree that this is a bit heavyweight, but I think something like it is necessary to satisfy this part of the streams contract:

/// When the caller gives a `len` of 0, it represents a request to
/// read 0 bytes. If the stream is still open, this call should
/// succeed and return an empty list, or otherwise fail with `closed`.

@alexcrichton
Copy link
Member

I think it still satisfies the contract though because the streams interface is supposed to be super general and it's always possible for a generic stream to get disconnected at any time. For example if you do a zero-length read on a socket it may say it's ready only for the next read to say it's closed.

@elliottt
Copy link
Member Author

Fair enough. I've pushed a change to that effect.

@elliottt elliottt added this pull request to the merge queue May 14, 2024
Merged via the queue into bytecodealliance:main with commit aade0c8 May 14, 2024
22 checks passed
@elliottt elliottt deleted the trevor/wasi-file-stream-read-zero branch May 14, 2024 16:45
elliottt added a commit to elliottt/wasmtime that referenced this pull request May 14, 2024
* Test that wasi file streams can handle read(0)

* Zero-sized reads don't fail for file streams

* Accidentally removed the `read(0)` when refactoring the test
alexcrichton pushed a commit that referenced this pull request May 14, 2024
* wasi: Handle read(0) with file streams (#8611)

* Test that wasi file streams can handle read(0)

* Zero-sized reads don't fail for file streams

* Accidentally removed the `read(0)` when refactoring the test

* Update release notes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wasi Issues pertaining to WASI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants