I am wondering if Tokio’s AsyncReadExt::read
method is cancel safe.
More specifically, say I invoke, e.g., socket.read(&mut my_buf)
from within a tokio::select
, but my invocation gets cancelled before returning. Can I trust that no bytes were read from socket
, i.e., that socket
is in the same state as before I called socket.read
?
I have seen a similar question that involves reading a primitive such as u64
. In that case, it is very clear to me why the call is not cancel safe: a buffer is instantiated under the hood and filled, possibly with repeated calls to AsyncReadExt::read
. If the fixed-size read is cancelled before it returns but after having pulled some bytes into the buffer, of course some bytes could be lost.
It is unclear to me, however, whether or not AsyncReadExt::read
should have the same problem. Common sense would suggest no – a single call to AsyncReadExt::read
should wait until at least one byte can be pulled, pull however many bytes it can (up to the buffer capacity), then return. In that case, I would imagine that either some bytes are read and written to the buffer, or no bytes are read at all. I could imagine, however, some implementation of AsyncReadExt::read
might employ buffers after all, so I cannot be certain, and I could not find anything explicit on the documentation.