Skip to content

Implement AsyncBufRead - #100

Merged
quininer merged 3 commits into
rustls:mainfrom
goffrie:asyncbufread
Feb 11, 2025
Merged

quininer merged 3 commits into
rustls:mainfrom
goffrie:asyncbufread

Conversation

@goffrie

@goffrie goffrie commented Jan 21, 2025 •

Copy link
Copy Markdown
Contributor

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.

@cpu

cpu commented Jan 25, 2025

Copy link
Copy Markdown
Member

cpu requested a review from quininer now

@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!

@quininer

quininer commented Feb 4, 2025

Copy link
Copy Markdown
Member

Sorry delay, I'm on Spring Festival. I'll take a look at it later :)

@quininer quininer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great. It's what I've always wanted. :D

Comment thread src/common/mod.rs Outdated
}
}

impl<IO: AsyncRead + AsyncWrite + Unpin, C, SD> AsyncRead for Stream<'_, IO, C>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, poll_read should be based on poll_fill_buf.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@cpu

cpu commented Feb 6, 2025 •

Copy link
Copy Markdown
Member

CI / MSRV (pull_request) Failing after 13s

FWIW you can ignore this failure. I'll open a separate PR to fix. Edit: #101

@goffrie
goffrie marked this pull request as ready for review February 7, 2025 22:47
Comment thread src/common/mod.rs
}
}

pub(crate) fn poll_fill_buf(mut self, cx: &mut Context<'_>) -> Poll<io::Result<&'a [u8]>>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should implement AsyncBufRead for common::Stream and test it in common/test_stream.rs?

Comment thread src/common/mod.rs Outdated

Err(err) => Poll::Ready(Err(err)),
let this = self.get_mut();
let data = ready!(Stream {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if poll_fill_buf accepts &mut, then it seems unnecessary to reborrow?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Comment thread src/common/test_stream.rs Outdated
let mut buf2 = buf.initialize_unfilled();

Poll::Ready(match self.0.write_tls(buf2.by_ref()) {
Ok(0) => return Poll::Pending,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't make sense, maybe we need to keep the read_io behavior as is.

@goffrie goffrie Feb 10, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll remove the AsyncRead changes from this PR and save them for another one in that case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@goffrie

goffrie commented Feb 9, 2025 •

Copy link
Copy Markdown
Contributor Author

i just realized that I only changed client::TlsStream and the changes need to be replicated to the server stream & the wrapper enum. (edit: done)

@goffrie
goffrie force-pushed the asyncbufread branch 2 times, most recently from ef3ef6e to 642a1c4 Compare February 10, 2025 22:31

@quininer quininer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread src/client.rs Resolved
@quininer
quininer merged commit 710cf25 into rustls:main Feb 11, 2025
@quininer

Copy link
Copy Markdown
Member

Thank you!

@goffrie
goffrie deleted the asyncbufread branch February 11, 2025 08:06
@goffrie

goffrie commented Feb 26, 2025

Copy link
Copy Markdown
Contributor Author

@quininer Would you mind cutting a release to get this change on crates.io? No rush though 🙂

@quininer

quininer commented Mar 1, 2025

Copy link
Copy Markdown
Member

@goffrie I opened this. :)

takumi-earth pushed a commit to earthlings-dev/tokio-rustls that referenced this pull request Jan 27, 2026
* Implement AsyncBufRead for TlsStream types & Stream

* Implement AsyncRead using AsyncBufRead

* Reimplement AsyncRead for {server,client}::TlsStream in terms of AsyncBufRead
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Large percentage of cpu time in memset when reading with a larger buffer

3 participants