Skip to content

fix: handshake does not fully flush writes - #112

Merged
quininer merged 2 commits into
rustls:mainfrom
conradludgate:ensure-flush-is-completed
Jun 6, 2025
Merged

quininer merged 2 commits into
rustls:mainfrom
conradludgate:ensure-flush-is-completed

Conversation

@conradludgate

@conradludgate conradludgate commented May 27, 2025 •

Copy link
Copy Markdown
Contributor

@almindor pointed out a potential issue with poll_flush within handshake. If write returns Ready, but flush returns Pending, it's guaranteed that the handshake() function returns and loses the need_flush state. If there's no more data to write, then it will never attempt to flush or write again.

Given the nature of TLS, it could be the client hello still needs flushing before a server hello would ever be readable.

This normally wouldn't be observable against a TcpStream, given that TcpStream has no flush impl.

The first commit creates a test that currently times out. The second commit introduces a potential fix which caches the need_flush state. I'm not sure what to do about early-data handling.

@quininer

Copy link
Copy Markdown
Member

Thank you! That makes sense.

about early data, I think you can put the need_flush flag directly on TlsStream instead of MidHandshake state. because our layout is not that compact.

@conradludgate

Copy link
Copy Markdown
Contributor Author

about early data, I think you can put the need_flush flag directly on TlsStream instead of MidHandshake state. because our layout is not that compact.

Thanks. I've changed to this and added a test for early_data, and checked that it timed-out before and works after the change.

@djc

djc commented Jun 2, 2025

Copy link
Copy Markdown
Member

Can you clean this up into atomic commits so that it becomes a bit easier to review?

@conradludgate
conradludgate force-pushed the ensure-flush-is-completed branch from ec49b6b to 5003c89 Compare June 2, 2025 07:41
@conradludgate

Copy link
Copy Markdown
Contributor Author

@djc done.

Comment thread src/common/mod.rs Resolved

@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/common/mod.rs Outdated
pub fn handshake(
&mut self,
cx: &mut Context,
mut need_flush: bool,

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.

I was thinking of make need_flush a field of Stream struct. but this is good enough.

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.

Changed to that. Wdyt?

@djc

djc commented Jun 6, 2025

Copy link
Copy Markdown
Member

@conradludgate this needs a rebase. If you want to turn need_flush into a field on Stream, that would be nice too.

@conradludgate
conradludgate force-pushed the ensure-flush-is-completed branch from 5003c89 to d8fed93 Compare June 6, 2025 11:23
@quininer
quininer merged commit dd5f27d into rustls:main Jun 6, 2025
@quininer

quininer commented Jun 6, 2025

Copy link
Copy Markdown
Member

Thank you!

@djc djc mentioned this pull request Sep 17, 2025
takumi-earth pushed a commit to earthlings-dev/tokio-rustls that referenced this pull request Jan 27, 2026
* write test to verify that flush does not get resumed

* attempt to persist the need_flush state during handshake
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.

3 participants