WebAssembly/wasi-sockets

Querying the number of bytes ready to be read.

sunfishcode opened this issue · 2 comments

Many platforms have a FIONREAD ioctl which returns a number of bytes which may be read from a socket without blocking.
FIONREAD, including Linux, Windows, macOS and others. I'd be useful to add a function which does this to wasi-sockets.

My specific motivation for this is that Preview1 happened to have similar functionality, provided as a result from poll_oneofff, and I'd like to be able to polyfill it.

Is your question for TCP only?
In the preview1 docs I read "The number of bytes available for reading or writing.". How does that last part work?


The reason it isn't included is because I couldn't find correct use cases for it. When Googling "TCP FIONREAD", it seems that by far most people use it for two reasons:

  • Determining upfront whether or not read will block or not.
  • Determining the buffer size to pass to read.

Both of these generally are a bad practice at best, and cause race conditions at worst. Also, both of these use cases have "proper" solutions in WASI in the form of streams & futures.

But I might be overlooking something. Please let me know if I'm wrong.

As for polyfilling, you might be able to fake it and just set it to an arbitrary number (eg 8192 bytes) when there is data ready. The actual read call might then return a different number of bytes, but that was always a possibility.

Sounds reasonable. I'll go with a fixed number in the polyfill for now and we'll see how it goes.