Skip to content

Commit

Permalink
fix: Check for ErrorKind::WouldBlock in LazyConfigAcceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
jbr committed Mar 5, 2024
1 parent fd3724e commit 2143357
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ use std::sync::Arc;
use std::task::{Context, Poll};

pub use rustls;
use rustls::server::AcceptedAlert;
use rustls::{ClientConfig, ClientConnection, CommonState, ServerConfig, ServerConnection};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};

Expand Down Expand Up @@ -195,6 +196,7 @@ impl TlsAcceptor {
pub struct LazyConfigAcceptor<IO> {
acceptor: rustls::server::Acceptor,
io: Option<IO>,
alert: Option<(rustls::Error, AcceptedAlert)>,
}

impl<IO> LazyConfigAcceptor<IO>
Expand All @@ -206,6 +208,7 @@ where
Self {
acceptor,
io: Some(io),
alert: None,
}
}

Expand Down Expand Up @@ -274,6 +277,16 @@ where
}
};

if let Some((err, mut alert)) = this.alert.take() {
return match alert.write(&mut common::SyncWriteAdapter { io, cx }) {
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
this.alert = Some((err, alert));
Poll::Pending
}
_ => Poll::Ready(Err(io::Error::new(io::ErrorKind::InvalidInput, err))),
};
}

let mut reader = common::SyncReadAdapter { io, cx };
match this.acceptor.read_tls(&mut reader) {
Ok(0) => return Err(io::ErrorKind::UnexpectedEof.into()).into(),
Expand All @@ -287,11 +300,9 @@ where
let io = this.io.take().unwrap();
return Poll::Ready(Ok(StartHandshake { accepted, io }));
}
Ok(None) => continue,
Err((err, mut alert)) => {
let mut writer = common::SyncWriteAdapter { io, cx };
let _ = alert.write(&mut writer); // best effort
return Poll::Ready(Err(io::Error::new(io::ErrorKind::InvalidInput, err)));
Ok(None) => {}
Err((err, alert)) => {
this.alert = Some((err, alert));
}
}
}
Expand Down

0 comments on commit 2143357

Please sign in to comment.