Skip to content

Commit 402212c

Browse files
authored
fix(swarm): implement ConnectionHandler::poll_close for combinators
Follow-up to #4076. This is especially relevant since `libp2p-swarm-derive` uses `SelectConnectionHandler`. Pull-Request: #4794.
1 parent 32c945c commit 402212c

File tree

7 files changed

+58
-2
lines changed

7 files changed

+58
-2
lines changed

swarm/src/behaviour/toggle.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,4 +363,12 @@ where
363363
}
364364
}
365365
}
366+
367+
fn poll_close(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::ToBehaviour>> {
368+
let Some(inner) = self.inner.as_mut() else {
369+
return Poll::Ready(None);
370+
};
371+
372+
inner.poll_close(cx)
373+
}
366374
}

swarm/src/handler.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ pub trait ConnectionHandler: Send + 'static {
160160
/// We therefore cannot guarantee that performing IO within here will succeed.
161161
///
162162
/// To signal completion, [`Poll::Ready(None)`] should be returned.
163+
///
164+
/// Implementations MUST have a [`fuse`](futures::StreamExt::fuse)-like behaviour.
165+
/// That is, [`Poll::Ready(None)`] MUST be returned on repeated calls to [`ConnectionHandler::poll_close`].
163166
fn poll_close(&mut self, _: &mut Context<'_>) -> Poll<Option<Self::ToBehaviour>> {
164167
Poll::Ready(None)
165168
}

swarm/src/handler/either.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ where
134134
Poll::Ready(event)
135135
}
136136

137+
fn poll_close(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::ToBehaviour>> {
138+
let event = match self {
139+
Either::Left(handler) => futures::ready!(handler.poll_close(cx)).map(Either::Left),
140+
Either::Right(handler) => futures::ready!(handler.poll_close(cx)).map(Either::Right),
141+
};
142+
143+
Poll::Ready(event)
144+
}
145+
137146
fn on_connection_event(
138147
&mut self,
139148
event: ConnectionEvent<

swarm/src/handler/map_in.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ where
8080
self.inner.poll(cx)
8181
}
8282

83+
fn poll_close(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::ToBehaviour>> {
84+
self.inner.poll_close(cx)
85+
}
86+
8387
fn on_connection_event(
8488
&mut self,
8589
event: ConnectionEvent<

swarm/src/handler/map_out.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use crate::handler::{
2222
ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, SubstreamProtocol,
2323
};
24+
use futures::ready;
2425
use std::fmt::Debug;
2526
use std::task::{Context, Poll};
2627

@@ -83,6 +84,14 @@ where
8384
})
8485
}
8586

87+
fn poll_close(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::ToBehaviour>> {
88+
let Some(e) = ready!(self.inner.poll_close(cx)) else {
89+
return Poll::Ready(None);
90+
};
91+
92+
Poll::Ready(Some((self.map)(e)))
93+
}
94+
8695
fn on_connection_event(
8796
&mut self,
8897
event: ConnectionEvent<

swarm/src/handler/multi.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::handler::{
2727
};
2828
use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend, UpgradeInfoSend};
2929
use crate::Stream;
30-
use futures::{future::BoxFuture, prelude::*};
30+
use futures::{future::BoxFuture, prelude::*, ready};
3131
use rand::Rng;
3232
use std::{
3333
cmp,
@@ -271,6 +271,17 @@ where
271271

272272
Poll::Pending
273273
}
274+
275+
fn poll_close(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::ToBehaviour>> {
276+
for (k, h) in self.handlers.iter_mut() {
277+
let Some(e) = ready!(h.poll_close(cx)) else {
278+
continue;
279+
};
280+
return Poll::Ready(Some((k.clone(), e)));
281+
}
282+
283+
Poll::Ready(None)
284+
}
274285
}
275286

276287
/// Split [`MultiHandler`] into parts.

swarm/src/handler/select.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::handler::{
2525
};
2626
use crate::upgrade::SendWrapper;
2727
use either::Either;
28-
use futures::future;
28+
use futures::{future, ready};
2929
use libp2p_core::upgrade::SelectUpgrade;
3030
use std::{cmp, task::Context, task::Poll};
3131

@@ -259,6 +259,18 @@ where
259259
Poll::Pending
260260
}
261261

262+
fn poll_close(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::ToBehaviour>> {
263+
if let Some(e) = ready!(self.proto1.poll_close(cx)) {
264+
return Poll::Ready(Some(Either::Left(e)));
265+
}
266+
267+
if let Some(e) = ready!(self.proto2.poll_close(cx)) {
268+
return Poll::Ready(Some(Either::Right(e)));
269+
}
270+
271+
Poll::Ready(None)
272+
}
273+
262274
fn on_connection_event(
263275
&mut self,
264276
event: ConnectionEvent<

0 commit comments

Comments
 (0)