Skip to content

Commit

Permalink
Merge pull request #22 from tmccombs/spawn-handshake-timeouts
Browse files Browse the repository at this point in the history
Abort spawned tasks for SpawningHandshakes on timeout
  • Loading branch information
tmccombs authored Mar 21, 2022
2 parents 5c618e9 + 9693c67 commit 9e80a12
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixed

- Fixed compilation on non-unix environments, where tokio-net doesn't include unix sockets
- `SpawningHandshakes` will abort the tasks for pending connections when the linked futures are dropped. This should allow timeouts to cause the connectionto be closed.


## 0.5.0 - 2022-03-20
Expand Down
26 changes: 12 additions & 14 deletions src/spawning_handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,33 @@ where
type AcceptFuture = HandshakeJoin<T::Stream, T::Error>;

fn accept(&self, stream: C) -> Self::AcceptFuture {
HandshakeJoin {
inner: tokio::spawn(self.0.accept(stream)),
}
HandshakeJoin(tokio::spawn(self.0.accept(stream)))
}
}

/// Future type returned by [`SpawningHandshakeTls::accept`];
#[cfg_attr(docsrs, doc(cfg(feature = "rt")))]
pin_project! {
/// Future type returned by [`SpawningHandshakeTls::accept`];
pub struct HandshakeJoin<Stream, Error>{
#[pin]
inner: JoinHandle<Result<Stream, Error>>
}
}
pub struct HandshakeJoin<Stream, Error>(JoinHandle<Result<Stream, Error>>);

#[cfg_attr(docsrs, doc(cfg(feature = "rt")))]
impl<Stream, Error> Future for HandshakeJoin<Stream, Error> {
type Output = Result<Stream, Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.project().inner.poll(cx) {
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match Pin::new(&mut self.as_mut().0).poll(cx) {
Poll::Ready(Ok(v)) => Poll::Ready(v),
Poll::Pending => Poll::Pending,
Poll::Ready(Err(e)) => {
if e.is_panic() {
std::panic::resume_unwind(e.into_panic());
} else {
panic!("Tls handshake was aborted: {:?}", e);
unreachable!("Tls handshake was aborted: {:?}", e);
}
}
}
}
}

impl<Stream, Error> Drop for HandshakeJoin<Stream, Error> {
fn drop(&mut self) {
self.0.abort();
}
}

0 comments on commit 9e80a12

Please sign in to comment.