You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
potentially questionable overloading of ErrorKind::WouldBlock #777
PR #629 changed the behavior of the std::io::Read implementation for reading plaintext bytes from a TLS connection. Ok(0) (EOF) is only returned when a CloseNotify was received from the remote peer. When no bytes are available to read but the remote peer has not sent a CloseNotify message, an Error with ErrorKind::WouldBlock is returned instead.
What I'm wondering here is whether WouldBlock is, in fact, the correct ErrorKind to return in this case. Per the standard library documentation, WouldBlock indicates that
The operation needs to block to complete, but the blocking operation was requested to not occur.
Generally, this is used when an IO operation is performed asynchronously: for example, on Linux, when a socket is opened with the O_NONBLOCK flag and a read or write call returned EWOULDBLOCK. Async IO libraries such as tokio and mio assign special meaning to this ErrorKind, as it is assumed to indicate that the operating system will somehow notify the program when that socket is once again readable (e.g. via an epoll event).
Therefore, I wonder if it's really correct to overload the meaning of WouldBlock here. My understanding is that rustls will return it when the connection was closed abruptly by the peer (without sending a CloseNotify), or when the connection has not been closed, but additional TLS bytes must be read from IO before more plaintext bytes are available to read. Returning WouldBlock in both cases seems potentially confusing.
I wonder if it would be more correct to return an error with ErrorKind::UnexpectedEof in the case where an EOF was encountered without receiving a CloseNotify?
To be sure, we're talking about Connection::read() (on current main), right?
fnread(&mutself,buf:&mut[u8]) -> io::Result<usize>{let len = self.received_plaintext.read(buf)?;if len == 0 && !buf.is_empty(){// no bytes available:// - if we received a close_notify, this is a genuine permanent EOF// - otherwise say EWOULDBLOCKif !self.connection_at_eof(){returnErr(io::ErrorKind::WouldBlock.into());}}Ok(len)}
The way I understand it is that self.received_plaintext contains application data bytes that are buffered in the Connection, after having been decrypted from the bytes read from the io::Read passed to read_tls(). As long as we haven't determined that the connection is at EOF (by way of a CloseNotify alert from the peer), we could at any time get new application data traffic being passed into the connection.
As such, I think the situation here is analogous to the async I/O sockets at the OS level or in tokio, in that rustls is indicating that the caller should notify the Connection when new TLS data is available, by calling read_tls().
To be sure, we're talking about Connection::read() (on current main), right?
Yes, that's correct.
It's possible I've misinterpreted the behavior here --- in which case, thanks for clearing this up!
I agree that returning WouldBlock is correct when additional read_tls() calls are necessary before more plaintext bytes can be read. The case I was thinking of, specifically, is one where the reader passed to read_tls returns Ok(0), indicating that the IO stream EOFed without a CloseNotify. Does Connection::read return WouldBlock in that case? If it doesn't, I may have misunderstood something.
It looks like Connection does not keep track of whether the Read passed to read_tls() has returned Ok(0), so in that case it seems like read() would still end up yielding WouldBlock because received_plaintext would run empty and we would not keep track of the fact that we'd received a non-CloseNotify EOF. That seems like something we ought to fix?
Activity
djc commentedon Jul 8, 2021
To be sure, we're talking about
Connection::read()(on current main), right?The way I understand it is that
self.received_plaintextcontains application data bytes that are buffered in theConnection, after having been decrypted from the bytes read from theio::Readpassed toread_tls(). As long as we haven't determined that the connection is at EOF (by way of aCloseNotifyalert from the peer), we could at any time get new application data traffic being passed into the connection.As such, I think the situation here is analogous to the async I/O sockets at the OS level or in tokio, in that rustls is indicating that the caller should notify the
Connectionwhen new TLS data is available, by callingread_tls().hawkw commentedon Jul 8, 2021
Yes, that's correct.
It's possible I've misinterpreted the behavior here --- in which case, thanks for clearing this up!
I agree that returning
WouldBlockis correct when additionalread_tls()calls are necessary before more plaintext bytes can be read. The case I was thinking of, specifically, is one where the reader passed toread_tlsreturnsOk(0), indicating that the IO stream EOFed without aCloseNotify. DoesConnection::readreturnWouldBlockin that case? If it doesn't, I may have misunderstood something.djc commentedon Jul 8, 2021
It looks like
Connectiondoes not keep track of whether theReadpassed toread_tls()has returnedOk(0), so in that case it seems likeread()would still end up yieldingWouldBlockbecausereceived_plaintextwould run empty and we would not keep track of the fact that we'd received a non-CloseNotifyEOF. That seems like something we ought to fix?hawkw commentedon Jul 8, 2021
@djc Yeah...i ended up just having
tokio-rustlstrack this itself so that it can handle this correctly. It would be nice ifrustlsdid that instead!djc commentedon Jul 8, 2021
If you want to review #779, that would be great!
hawkw commentedon Jul 9, 2021
yup, i'll take a look!
djc commentedon Jul 27, 2021
This should be fixed by b84721e.