Skip to content

Commit

Permalink
Abort spawned tasks for SpawningHandshakes on timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
tmccombs committed Mar 21, 2022
1 parent 2753ead commit 9693c67
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Changelog

## 0.5.1
## Upcoming 0.5.1

### 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
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 9693c67

Please sign in to comment.