From 86598c430d8277c5f910914103e66e9ae46b6cdc Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 19 May 2022 17:27:13 +0200 Subject: [PATCH 1/2] Refactor `Yamux::close` to use `?` --- muxers/yamux/src/lib.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/muxers/yamux/src/lib.rs b/muxers/yamux/src/lib.rs index 2ce0b065345..ea0ee24ee7a 100644 --- a/muxers/yamux/src/lib.rs +++ b/muxers/yamux/src/lib.rs @@ -179,16 +179,23 @@ where fn close(&self, c: &mut Context<'_>) -> Poll> { let mut inner = self.0.lock(); - if let std::task::Poll::Ready(x) = Pin::new(&mut inner.control).poll_close(c) { - return Poll::Ready(x.map_err(YamuxError)); + + if let Poll::Ready(()) = Pin::new(&mut inner.control) + .poll_close(c) + .map_err(YamuxError)? + { + return Poll::Ready(Ok(())); } - while let std::task::Poll::Ready(x) = inner.incoming.poll_next_unpin(c) { - match x { - Some(Ok(_)) => {} // drop inbound stream - Some(Err(e)) => return Poll::Ready(Err(e)), + + while let Poll::Ready(_inbound_stream) = + inner.incoming.poll_next_unpin(c)? + { + match _inbound_stream { + Some(_) => {} // drop inbound stream None => return Poll::Ready(Ok(())), } } + Poll::Pending } From 6eb64fe8155a6a18f7d67333fb9c43c3a50885cc Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Sun, 29 May 2022 20:49:47 +0200 Subject: [PATCH 2/2] Make dropping of inbound streams more obvious --- muxers/yamux/src/lib.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/muxers/yamux/src/lib.rs b/muxers/yamux/src/lib.rs index ea0ee24ee7a..4d573ab8e0e 100644 --- a/muxers/yamux/src/lib.rs +++ b/muxers/yamux/src/lib.rs @@ -31,7 +31,7 @@ use libp2p_core::muxing::{StreamMuxer, StreamMuxerEvent}; use libp2p_core::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}; use parking_lot::Mutex; use std::{ - fmt, io, iter, + fmt, io, iter, mem, pin::Pin, task::{Context, Poll}, }; @@ -187,11 +187,9 @@ where return Poll::Ready(Ok(())); } - while let Poll::Ready(_inbound_stream) = - inner.incoming.poll_next_unpin(c)? - { - match _inbound_stream { - Some(_) => {} // drop inbound stream + while let Poll::Ready(maybe_inbound_stream) = inner.incoming.poll_next_unpin(c)? { + match maybe_inbound_stream { + Some(inbound_stream) => mem::drop(inbound_stream), None => return Poll::Ready(Ok(())), } }