More precise error passed to inject_dial_upgrade_error (#771)

* More precise error passed to inject_dial_upgrade_error

* Fix concerns

* Fix panic proof
This commit is contained in:
Pierre Krieger
2018-12-18 11:23:13 +01:00
committed by GitHub
parent af698a1ce7
commit 83320e0347
14 changed files with 194 additions and 52 deletions

View File

@ -19,7 +19,7 @@
// DEALINGS IN THE SOFTWARE.
use crate::{
protocols_handler::{ProtocolsHandler, ProtocolsHandlerEvent},
protocols_handler::{ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr},
upgrade::{
InboundUpgrade,
OutboundUpgrade,
@ -82,7 +82,7 @@ where
fn inject_event(&mut self, _: Self::InEvent) {}
#[inline]
fn inject_dial_upgrade_error(&mut self, _: Self::OutboundOpenInfo, _: io::Error) {}
fn inject_dial_upgrade_error(&mut self, _: Self::OutboundOpenInfo, _: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>) {}
#[inline]
fn inject_inbound_closed(&mut self) {}

View File

@ -19,7 +19,7 @@
// DEALINGS IN THE SOFTWARE.
use crate::{
protocols_handler::{ProtocolsHandler, ProtocolsHandlerEvent},
protocols_handler::{ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr},
upgrade::{
InboundUpgrade,
OutboundUpgrade,
@ -89,7 +89,7 @@ where
}
#[inline]
fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, error: io::Error) {
fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>) {
self.inner.inject_dial_upgrade_error(info, error)
}

View File

@ -19,7 +19,7 @@
// DEALINGS IN THE SOFTWARE.
use crate::{
protocols_handler::{ProtocolsHandler, ProtocolsHandlerEvent},
protocols_handler::{ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr},
upgrade::{
InboundUpgrade,
OutboundUpgrade,
@ -85,7 +85,7 @@ where
}
#[inline]
fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, error: io::Error) {
fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>) {
self.inner.inject_dial_upgrade_error(info, error)
}

View File

@ -21,9 +21,10 @@
use crate::upgrade::{
InboundUpgrade,
OutboundUpgrade,
UpgradeError,
};
use futures::prelude::*;
use std::{io, time::Duration};
use std::{error, fmt, io, time::Duration};
use tokio_io::{AsyncRead, AsyncWrite};
pub use self::dummy::DummyProtocolsHandler;
@ -124,7 +125,7 @@ pub trait ProtocolsHandler {
fn inject_event(&mut self, event: Self::InEvent);
/// Indicates to the handler that upgrading a substream to the given protocol has failed.
fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, error: io::Error);
fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>);
/// Indicates to the handler that the inbound part of the muxer has been closed, and that
/// therefore no more inbound substreams will be produced.
@ -273,3 +274,50 @@ impl<TConnectionUpgrade, TOutboundOpenInfo, TCustom>
}
}
}
/// Error that can happen on an outbound substream opening attempt.
#[derive(Debug)]
pub enum ProtocolsHandlerUpgrErr<TUpgrErr> {
/// The opening attempt timed out before the negotiation was fully completed.
Timeout,
/// There was an error in the timer used.
Timer,
/// The remote muxer denied the attempt to open a substream.
MuxerDeniedSubstream,
/// Error while upgrading the substream to the protocol we want.
Upgrade(UpgradeError<TUpgrErr>),
}
impl<TUpgrErr> fmt::Display for ProtocolsHandlerUpgrErr<TUpgrErr>
where
TUpgrErr: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ProtocolsHandlerUpgrErr::Timeout => {
write!(f, "Timeout error while opening a substream")
},
ProtocolsHandlerUpgrErr::Timer => {
write!(f, "Timer error while opening a substream")
},
ProtocolsHandlerUpgrErr::MuxerDeniedSubstream => {
write!(f, "Remote muxer denied our attempt to open a substream")
},
ProtocolsHandlerUpgrErr::Upgrade(err) => write!(f, "{}", err),
}
}
}
impl<TUpgrErr> error::Error for ProtocolsHandlerUpgrErr<TUpgrErr>
where
TUpgrErr: error::Error + 'static
{
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
ProtocolsHandlerUpgrErr::Timeout => None,
ProtocolsHandlerUpgrErr::Timer => None,
ProtocolsHandlerUpgrErr::MuxerDeniedSubstream => None,
ProtocolsHandlerUpgrErr::Upgrade(err) => Some(err),
}
}
}

View File

@ -20,7 +20,7 @@
use crate::{
nodes::handled_node::{NodeHandler, NodeHandlerEndpoint, NodeHandlerEvent},
protocols_handler::{ProtocolsHandler, ProtocolsHandlerEvent},
protocols_handler::{ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr},
upgrade::{
self,
OutboundUpgrade,
@ -185,7 +185,7 @@ where
self.queued_dial_upgrades.remove(pos);
self.handler
.inject_dial_upgrade_error(user_data.1, io::ErrorKind::ConnectionReset.into());
.inject_dial_upgrade_error(user_data.1, ProtocolsHandlerUpgrErr::MuxerDeniedSubstream);
}
#[inline]
@ -224,8 +224,18 @@ where
self.negotiating_out.push((upgr_info, in_progress));
}
Err(err) => {
let msg = format!("Error while upgrading: {:?}", err);
let err = io::Error::new(io::ErrorKind::Other, msg);
let err = if err.is_elapsed() {
ProtocolsHandlerUpgrErr::Timeout
} else if err.is_timer() {
ProtocolsHandlerUpgrErr::Timer
} else {
debug_assert!(err.is_inner());
let err = err.into_inner().expect("Timeout error is one of {elapsed, \
timer, inner}; is_inner and is_elapsed are both false; error is \
inner; QED");
ProtocolsHandlerUpgrErr::Upgrade(err)
};
self.handler.inject_dial_upgrade_error(upgr_info, err);
}
}

View File

@ -19,13 +19,15 @@
// DEALINGS IN THE SOFTWARE.
use crate::{
either::EitherError,
either::EitherOutput,
protocols_handler::{ProtocolsHandler, ProtocolsHandlerEvent},
protocols_handler::{ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr},
upgrade::{
InboundUpgrade,
OutboundUpgrade,
EitherUpgrade,
SelectUpgrade
SelectUpgrade,
UpgradeError,
}
};
use futures::prelude::*;
@ -112,10 +114,44 @@ where
}
#[inline]
fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, error: io::Error) {
match info {
EitherOutput::First(info) => self.proto1.inject_dial_upgrade_error(info, error),
EitherOutput::Second(info) => self.proto2.inject_dial_upgrade_error(info, error),
fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>) {
match (info, error) {
(EitherOutput::First(info), ProtocolsHandlerUpgrErr::MuxerDeniedSubstream) => {
self.proto1.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::MuxerDeniedSubstream)
},
(EitherOutput::First(info), ProtocolsHandlerUpgrErr::Timer) => {
self.proto1.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Timer)
},
(EitherOutput::First(info), ProtocolsHandlerUpgrErr::Timeout) => {
self.proto1.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Timeout)
},
(EitherOutput::First(info), ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(err))) => {
self.proto1.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(err)))
},
(EitherOutput::First(info), ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::A(err)))) => {
self.proto1.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(err)))
},
(EitherOutput::First(_), ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::B(_)))) => {
panic!("Wrong API usage; the upgrade error doesn't match the outbound open info");
},
(EitherOutput::Second(info), ProtocolsHandlerUpgrErr::MuxerDeniedSubstream) => {
self.proto2.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::MuxerDeniedSubstream)
},
(EitherOutput::Second(info), ProtocolsHandlerUpgrErr::Timeout) => {
self.proto2.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Timeout)
},
(EitherOutput::Second(info), ProtocolsHandlerUpgrErr::Timer) => {
self.proto2.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Timer)
},
(EitherOutput::Second(info), ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(err))) => {
self.proto2.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(err)))
},
(EitherOutput::Second(info), ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::B(err)))) => {
self.proto2.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(err)))
},
(EitherOutput::Second(_), ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::A(_)))) => {
panic!("Wrong API usage; the upgrade error doesn't match the outbound open info");
},
}
}

View File

@ -28,8 +28,6 @@ pub enum UpgradeError<E> {
Select(ProtocolChoiceError),
/// Error during the post-negotiation handshake.
Apply(E),
#[doc(hidden)]
__Nonexhaustive
}
impl<E> UpgradeError<E>
@ -49,7 +47,6 @@ impl<E> UpgradeError<E> {
match self {
UpgradeError::Select(e) => UpgradeError::Select(e),
UpgradeError::Apply(e) => UpgradeError::Apply(f(e)),
UpgradeError::__Nonexhaustive => UpgradeError::__Nonexhaustive
}
}
@ -69,7 +66,6 @@ where
match self {
UpgradeError::Select(e) => write!(f, "select error: {}", e),
UpgradeError::Apply(e) => write!(f, "upgrade apply error: {}", e),
UpgradeError::__Nonexhaustive => f.write_str("__Nonexhaustive")
}
}
}
@ -82,7 +78,6 @@ where
match self {
UpgradeError::Select(e) => Some(e),
UpgradeError::Apply(e) => Some(e),
UpgradeError::__Nonexhaustive => None
}
}
}

View File

@ -22,6 +22,7 @@ use crate::protocol::{FloodsubCodec, FloodsubConfig, FloodsubRpc};
use futures::prelude::*;
use libp2p_core::{
ProtocolsHandler, ProtocolsHandlerEvent,
protocols_handler::ProtocolsHandlerUpgrErr,
upgrade::{InboundUpgrade, OutboundUpgrade}
};
use smallvec::SmallVec;
@ -144,7 +145,7 @@ where
fn inject_inbound_closed(&mut self) {}
#[inline]
fn inject_dial_upgrade_error(&mut self, _: Self::OutboundOpenInfo, _: io::Error) {}
fn inject_dial_upgrade_error(&mut self, _: Self::OutboundOpenInfo, _: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>) {}
#[inline]
fn shutdown(&mut self) {

View File

@ -23,7 +23,7 @@ use crate::periodic_id_handler::{PeriodicIdHandler, PeriodicIdHandlerEvent};
use crate::protocol::{IdentifyInfo, IdentifySender, IdentifySenderFuture};
use crate::topology::IdentifyTopology;
use futures::prelude::*;
use libp2p_core::protocols_handler::{ProtocolsHandler, ProtocolsHandlerSelect};
use libp2p_core::protocols_handler::{ProtocolsHandler, ProtocolsHandlerSelect, ProtocolsHandlerUpgrErr};
use libp2p_core::swarm::{ConnectedPoint, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use libp2p_core::{Multiaddr, PeerId, either::EitherOutput, topology::Topology};
use smallvec::SmallVec;
@ -192,6 +192,6 @@ pub enum IdentifyEvent {
/// Peer that we fail to identify.
peer_id: PeerId,
/// The error that happened.
error: io::Error,
error: ProtocolsHandlerUpgrErr<io::Error>,
},
}

View File

@ -21,8 +21,8 @@
use crate::protocol::{IdentifySender, IdentifyProtocolConfig};
use futures::prelude::*;
use libp2p_core::{
protocols_handler::{ProtocolsHandler, ProtocolsHandlerEvent},
upgrade::{DeniedUpgrade, InboundUpgrade}
protocols_handler::{ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr},
upgrade::{DeniedUpgrade, InboundUpgrade, OutboundUpgrade}
};
use smallvec::SmallVec;
use std::io;
@ -87,7 +87,7 @@ where
fn inject_inbound_closed(&mut self) {}
#[inline]
fn inject_dial_upgrade_error(&mut self, _: Self::OutboundOpenInfo, _: io::Error) {}
fn inject_dial_upgrade_error(&mut self, _: Self::OutboundOpenInfo, _: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>) {}
#[inline]
fn shutdown(&mut self) {

View File

@ -21,7 +21,7 @@
use crate::protocol::{RemoteInfo, IdentifyProtocolConfig};
use futures::prelude::*;
use libp2p_core::{
protocols_handler::{ProtocolsHandler, ProtocolsHandlerEvent},
protocols_handler::{ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr},
upgrade::{DeniedUpgrade, OutboundUpgrade}
};
use std::{io, marker::PhantomData, time::{Duration, Instant}};
@ -59,7 +59,7 @@ pub enum PeriodicIdHandlerEvent {
/// We obtained identification information from the remote
Identified(RemoteInfo),
/// Failed to identify the remote.
IdentificationError(io::Error),
IdentificationError(ProtocolsHandlerUpgrErr<io::Error>),
}
impl<TSubstream> PeriodicIdHandler<TSubstream> {
@ -110,7 +110,7 @@ where
fn inject_inbound_closed(&mut self) {}
#[inline]
fn inject_dial_upgrade_error(&mut self, _: Self::OutboundOpenInfo, err: io::Error) {
fn inject_dial_upgrade_error(&mut self, _: Self::OutboundOpenInfo, err: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>) {
self.pending_result = Some(PeriodicIdHandlerEvent::IdentificationError(err));
if let Some(ref mut next_id) = self.next_id {
next_id.reset(Instant::now() + TRY_AGAIN_ON_ERR);

View File

@ -19,14 +19,14 @@
// DEALINGS IN THE SOFTWARE.
use futures::prelude::*;
use libp2p_core::protocols_handler::{ProtocolsHandler, ProtocolsHandlerEvent};
use libp2p_core::protocols_handler::{ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr};
use libp2p_core::{upgrade, either::EitherOutput, InboundUpgrade, OutboundUpgrade, PeerId};
use multihash::Multihash;
use protocol::{
KadInStreamSink, KadOutStreamSink, KadPeer, KadRequestMsg, KadResponseMsg,
KademliaProtocolConfig,
};
use std::io;
use std::{error, fmt, io};
use tokio_io::{AsyncRead, AsyncWrite};
/// Protocol handler that handles Kademlia communications with the remote.
@ -80,7 +80,7 @@ where
// TODO: add timeout
OutWaitingAnswer(KadOutStreamSink<TSubstream>, TUserData),
/// An error happened on the substream and we should report the error to the user.
OutReportError(io::Error, TUserData),
OutReportError(KademliaHandlerQueryErr, TUserData),
/// The substream is being closed.
OutClosing(KadOutStreamSink<TSubstream>),
/// Waiting for a request from the remote.
@ -168,7 +168,7 @@ pub enum KademliaHandlerEvent<TUserData> {
/// An error happened when performing a query.
QueryError {
/// The error that happened.
error: io::Error,
error: KademliaHandlerQueryErr,
/// The user data passed to the query.
user_data: TUserData,
},
@ -182,6 +182,50 @@ pub enum KademliaHandlerEvent<TUserData> {
},
}
/// Error that can happen when requesting an RPC query.
#[derive(Debug)]
pub enum KademliaHandlerQueryErr {
/// Error while trying to perform the query.
Upgrade(ProtocolsHandlerUpgrErr<io::Error>),
/// Received an answer that doesn't correspond to the request.
UnexpectedMessage,
/// I/O error in the substream.
Io(io::Error),
}
impl fmt::Display for KademliaHandlerQueryErr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
KademliaHandlerQueryErr::Upgrade(err) => {
write!(f, "Error while performing Kademlia query: {}", err)
},
KademliaHandlerQueryErr::UnexpectedMessage => {
write!(f, "Remote answered our Kademlia RPC query with the wrong message type")
},
KademliaHandlerQueryErr::Io(err) => {
write!(f, "I/O error during a Kademlia RPC query: {}", err)
},
}
}
}
impl error::Error for KademliaHandlerQueryErr {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
KademliaHandlerQueryErr::Upgrade(err) => Some(err),
KademliaHandlerQueryErr::UnexpectedMessage => None,
KademliaHandlerQueryErr::Io(err) => Some(err),
}
}
}
impl From<ProtocolsHandlerUpgrErr<io::Error>> for KademliaHandlerQueryErr {
#[inline]
fn from(err: ProtocolsHandlerUpgrErr<io::Error>) -> Self {
KademliaHandlerQueryErr::Upgrade(err)
}
}
/// Event to send to the handler.
pub enum KademliaHandlerIn<TUserData> {
/// Request for the list of nodes whose IDs are the closest to `key`. The number of nodes
@ -434,13 +478,13 @@ where
fn inject_dial_upgrade_error(
&mut self,
(_, user_data): Self::OutboundOpenInfo,
error: io::Error,
error: ProtocolsHandlerUpgrErr<io::Error>,
) {
// TODO: cache the fact that the remote doesn't support kademlia at all, so that we don't
// continue trying
if let Some(user_data) = user_data {
self.substreams
.push(SubstreamState::OutReportError(error, user_data));
.push(SubstreamState::OutReportError(error.into(), user_data));
}
}
@ -553,8 +597,10 @@ where
),
Err(error) => {
let event = if let Some(user_data) = user_data {
let ev = KademliaHandlerEvent::QueryError { error, user_data };
Some(ProtocolsHandlerEvent::Custom(ev))
Some(ProtocolsHandlerEvent::Custom(KademliaHandlerEvent::QueryError {
error: KademliaHandlerQueryErr::Io(error),
user_data
}))
} else {
None
};
@ -583,8 +629,10 @@ where
),
Err(error) => {
let event = if let Some(user_data) = user_data {
let ev = KademliaHandlerEvent::QueryError { error, user_data };
Some(ProtocolsHandlerEvent::Custom(ev))
Some(ProtocolsHandlerEvent::Custom(KademliaHandlerEvent::QueryError {
error: KademliaHandlerQueryErr::Io(error),
user_data,
}))
} else {
None
};
@ -609,12 +657,17 @@ where
false,
),
Err(error) => {
let event = KademliaHandlerEvent::QueryError { error, user_data };
let event = KademliaHandlerEvent::QueryError {
error: KademliaHandlerQueryErr::Io(error),
user_data,
};
(None, Some(ProtocolsHandlerEvent::Custom(event)), false)
}
Ok(Async::Ready(None)) => {
let error = io::Error::new(io::ErrorKind::Other, "unexpected EOF");
let event = KademliaHandlerEvent::QueryError { error, user_data };
let event = KademliaHandlerEvent::QueryError {
error: KademliaHandlerQueryErr::Io(io::ErrorKind::UnexpectedEof.into()),
user_data,
};
(None, Some(ProtocolsHandlerEvent::Custom(event)), false)
}
},
@ -722,12 +775,8 @@ fn process_kad_response<TUserData>(
match event {
KadResponseMsg::Pong => {
// We never send out pings.
let err = io::Error::new(
io::ErrorKind::InvalidData,
"received unexpected PONG message",
);
KademliaHandlerEvent::QueryError {
error: err,
error: KademliaHandlerQueryErr::UnexpectedMessage,
user_data,
}
}

View File

@ -24,6 +24,7 @@ use libp2p_core::{
OutboundUpgrade,
ProtocolsHandler,
ProtocolsHandlerEvent,
protocols_handler::ProtocolsHandlerUpgrErr,
upgrade::DeniedUpgrade
};
use log::warn;
@ -183,7 +184,7 @@ where
fn inject_inbound_closed(&mut self) {}
#[inline]
fn inject_dial_upgrade_error(&mut self, _: Self::OutboundOpenInfo, _: io::Error) {
fn inject_dial_upgrade_error(&mut self, _: Self::OutboundOpenInfo, _: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>) {
// In case of error while upgrading, there's not much we can do except shut down.
// TODO: we assume that the error is about ping not being supported, which is not
// necessarily the case

View File

@ -23,8 +23,10 @@ use arrayvec::ArrayVec;
use futures::prelude::*;
use libp2p_core::{
InboundUpgrade,
OutboundUpgrade,
ProtocolsHandler,
ProtocolsHandlerEvent,
protocols_handler::ProtocolsHandlerUpgrErr,
upgrade::DeniedUpgrade
};
use log::warn;
@ -101,7 +103,7 @@ where
fn inject_inbound_closed(&mut self) {}
#[inline]
fn inject_dial_upgrade_error(&mut self, _: Self::OutboundOpenInfo, _: io::Error) {}
fn inject_dial_upgrade_error(&mut self, _: Self::OutboundOpenInfo, _: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>) {}
#[inline]
fn shutdown(&mut self) {