wasmtime: wasmtime-wasi: Unexpected tcp stream break (Returning 0 for `std::io::ErrorKind::WouldBlock`)
Returning 0 for std::io::ErrorKind::WouldBlock causes downstream to interpret it as a closed stream, see:
https://github.com/yoshuawuyts/wstd/issues/25#issuecomment-2493294087.
https://github.com/yoshuawuyts/wstd/blob/5ce367add5e7bcb569b6487453cb9ba94468dc99/src/io/copy.rs#L12
This is also found in: https://github.com/bytecodealliance/wasmtime/blob/67674881db5fbdbba8594feb52655aaa351a5f77/crates/test-programs/src/bin/preview2_tcp_streams.rs#L17-L22
Given that the wit files already include many would-block errors, would it make sense to extend stream-error to include a would-block?
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 16 (16 by maintainers)
I apologize I never got back to this after saying I would. I started a new job and have a whole host of new problems to work on, so I dropped some of the old ones.
Thanks Alex for digging into this and helping me understand why the existing behavior is there. My conclusion is that my specification of “a ready pollable implies input-stream.read will succeed” is too strong of a guarantee to implement efficiently on top of POSIX. In discussion with Alex, he suggested that only the behavior of read shows whether input was ready or not, and the pollable’s readiness is a best-effort indicator that provides an optimization over busy-looping on read. I expect that we can easily accommodate the weakening of that guarantee in existing code (at least in wstd), and any existing binaries can’t possibly be relying on that guarantee since its never been upheld by implementations. We will have to take that discussion out to the spec repo in order to nail it all down.
I apologize it’s taken me quite some time to take a look at this. Now that I’ve read over this and https://github.com/bytecodealliance/wasmtime/pull/9691 I personally don’t think there’s a bug in the wasmtime-wasi implementation here. I don’t think WASI can reasonably provide super strict guarantees about exactly when things become ready and exactly the states before/after reads. Attempting to do this might not even be feasible in the face of an implementation across multiple platforms.
For example the test in https://github.com/bytecodealliance/wasmtime/pull/9691 looks like this:
I don’t believe that this can be made to work reliably. Here after the initial “Hi!” message is ready it’s not actually known whether the stream is readable or not. Determining this would involve actually consulting the kernel with some sort of syscall or something like that. It’s a pretty important perf optimization that in async servers you don’t do anything with the kernel until absolutely necessary.
For example here the kernel was consulted during the first
server.input.subscribe().block(). At this point the stream is considered readable. The issuedreadthen again consults the kernel and reads out"Hi!". At this point though the stream is notably considered still readable by Tokio and Wasmtime. It’s not known whether this was a “short read” or whether more data is coming soon. This is why the nextsubscribe()immediately says “yes this is readable”. Doing otherwise would require unnecessarily consulting the kernel.Working robustly with async is certainly an art but personally I don’t think that there’s a bug here that needs fixing. I would personally think we can close https://github.com/bytecodealliance/wasmtime/pull/9691 and this issue as “working as intended”.
Thanks, it sounds like this is indeed a bug in wasmtime-wasi then. Can you create a minimal reproduction so I can work on a fix and use it as a test once we have the fix in? Something along the lines of https://github.com/bytecodealliance/wasmtime/blob/main/crates/test-programs/src/bin/preview2_tcp_streams.rs
When the stream is EOF, it returns Err(StreamError::Closed) as seen in the above snippet line 21. Maybe this isn’t so much an “Error” as the “Err” constructor indicates, but the alternative was returning a custom enum there of Ok/Closed/Error, which we rejected on fairly arbitrary stylistic grounds.