Repository navigation
Implement AsyncBufRead - #100
Conversation
@quininer If you had time to take a look at this branch it'd be nice to hear your thoughts before we merge rustls/rustls#870 Thanks! |
|
Sorry delay, I'm on Spring Festival. I'll take a look at it later :) |
quininer
left a comment
There was a problem hiding this comment.
This looks great. It's what I've always wanted. :D
| } | ||
| } | ||
|
|
||
| impl<IO: AsyncRead + AsyncWrite + Unpin, C, SD> AsyncRead for Stream<'_, IO, C> |
There was a problem hiding this comment.
If possible, poll_read should be based on poll_fill_buf.
There was a problem hiding this comment.
I've added this as a second commit. There is a downside though: a single poll_read call will only read up to one fill_buf()'s worth of data (i.e. one TLS packet) into the read buffer, rather than reading as much as it can. I don't think it's valid to call poll_fill_buf in a loop because once we have read some data into the output buffer, we should not be doing anything that can result in an error.
goffrie@d8e61ab offers an alternative that loops the internal reader call (but only reads once from the underlying reader) which allows draining all queued data in a single poll_read call, at the cost of having more duplicated code. This still lets us avoid using initialize_unfilled() which IMO is the main benefit. Maybe you have opinions on which you'd prefer.
FWIW you can ignore this failure. I'll open a separate PR to fix. Edit: #101 |
| } | ||
| } | ||
|
|
||
| pub(crate) fn poll_fill_buf(mut self, cx: &mut Context<'_>) -> Poll<io::Result<&'a [u8]>> |
There was a problem hiding this comment.
Should implement AsyncBufRead for common::Stream and test it in common/test_stream.rs?
|
|
||
| Err(err) => Poll::Ready(Err(err)), | ||
| let this = self.get_mut(); | ||
| let data = ready!(Stream { |
There was a problem hiding this comment.
if poll_fill_buf accepts &mut, then it seems unnecessary to reborrow?
There was a problem hiding this comment.
It is not possible to make poll_fill_buf accept &mut self, because it needs to return a reference that is tied to self.session's lifetime - which outlives self, as the Stream is only created temporarily. This is the same reason why into_first_chunk is needed in rustls. See this comment: rustls/rustls#2303 (comment)
| let mut buf2 = buf.initialize_unfilled(); | ||
|
|
||
| Poll::Ready(match self.0.write_tls(buf2.by_ref()) { | ||
| Ok(0) => return Poll::Pending, |
There was a problem hiding this comment.
Without this, when the writing Connection has nothing left to send, Good returns a 0-byte read. Rustls then interprets it as EOF and triggers an error later on for the connection getting closed. This causes some stream tests to fail with the new poll_read implementation. I'm not entirely sure why this didn't happen before, but it is probably related to the particular ordering of calls to read_io?
There was a problem hiding this comment.
This doesn't make sense, maybe we need to keep the read_io behavior as is.
There was a problem hiding this comment.
I'll remove the AsyncRead changes from this PR and save them for another one in that case.
There was a problem hiding this comment.
Actually you are right, something here doesn't make sense - the server should be sending a close notify so returning EOF ought to be OK. I'll look into it more carefully.
There was a problem hiding this comment.
Ok, I see. The problem is that today (without this Poll::Pending case), test_stream::do_handshake actually puts the connection into a somewhat invalid state where the connection has already read EOF (i.e. CommonState::has_seen_eof is true). However, since the connection hasn't received a CloseNotify, session.wants_read() continues to be true. With the current implementation of AsyncRead, which always calls Stream::read_io() prior to session.reader().read(), that means we get more chances to read more data into the input buffer (even though that doesn't make sense since we've already "seen EOF") and thereby delay receiving the peer closed connection without sending TLS close_notify error. However, my implementation of poll_fill_buf always calls into_first_chunk first before calling read_io() which means that it will hit the error case first.
None of this really matters since in the real world we should never see this "data-after-EOF" case so I'm happy to adjust poll_fill_buf to keep the status quo if you'd prefer.
|
i just realized that I only changed |
ef3ef6e to
642a1c4
Compare
642a1c4 to
5dba60d
Compare
|
Thank you! |
|
@quininer Would you mind cutting a release to get this change on crates.io? No rush though 🙂 |
* Implement AsyncBufRead for TlsStream types & Stream
* Implement AsyncRead using AsyncBufRead
* Reimplement AsyncRead for {server,client}::TlsStream in terms of AsyncBufRead
Now that rustls/rustls#2303 is merged, we can make use of it to implement AsyncBufRead.
This also reimplements AsyncRead in terms of AsyncBufRead, which indirectly fixes #55.