From 8c938af579e83e995934b568f5198d1a6368a948 Mon Sep 17 00:00:00 2001 From: Henry de Valence Date: Fri, 14 Feb 2020 16:01:43 -0800 Subject: [PATCH] Spawn tasks for handshake futures. Previously, we relied on the owner of the handshake future to drive it to completion. This meant that there were cases where handshakes might never be completed, just because nothing was actively polling them. --- zebra-network/src/peer/connector.rs | 4 ++-- zebra-network/src/peer/handshake.rs | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/zebra-network/src/peer/connector.rs b/zebra-network/src/peer/connector.rs index 1bf58be5f..7c1633c90 100644 --- a/zebra-network/src/peer/connector.rs +++ b/zebra-network/src/peer/connector.rs @@ -11,7 +11,7 @@ use tower::{discover::Change, Service, ServiceExt}; use crate::{BoxedStdError, Request, Response}; -use super::{Client, Handshake, HandshakeError}; +use super::{Client, Handshake}; /// A wrapper around [`peer::Handshake`] that opens a TCP connection before /// forwarding to the inner handshake service. Writing this as its own @@ -40,7 +40,7 @@ where S::Future: Send, { type Response = Change; - type Error = HandshakeError; + type Error = BoxedStdError; type Future = Pin> + Send + 'static>>; diff --git a/zebra-network/src/peer/handshake.rs b/zebra-network/src/peer/handshake.rs index c917aa3b3..4894977d8 100644 --- a/zebra-network/src/peer/handshake.rs +++ b/zebra-network/src/peer/handshake.rs @@ -80,7 +80,7 @@ where S::Future: Send, { type Response = Client; - type Error = HandshakeError; + type Error = BoxedStdError; type Future = Pin> + Send + 'static>>; @@ -257,6 +257,16 @@ where Ok(client) }; - fut.instrument(connector_span).boxed() + + // Spawn a new task to drive this handshake. + tokio::spawn(fut.instrument(connector_span)) + // This is required to get error types to line up. + // Probably there's a nicer way to express this using combinators. + .map(|x| match x { + Ok(Ok(client)) => Ok(client), + Ok(Err(handshake_err)) => Err(handshake_err.into()), + Err(join_err) => Err(join_err.into()), + }) + .boxed() } }