Skip to content

Commit 5151975

Browse files
authored
notification: Unregister dropped protocols (#391)
Part of the investigation from: - paritytech/polkadot-sdk#8474 --------- Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
1 parent 8a5420a commit 5151975

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

src/protocol/notification/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,9 @@ impl NotificationProtocol {
17951795
protocol = %self.protocol,
17961796
"user protocol has exited, exiting"
17971797
);
1798+
1799+
self.service.unregister_protocol();
1800+
17981801
return true;
17991802
}
18001803
Some(command) => match command {

src/protocol/transport_service.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,14 @@ impl TransportService {
544544
pub fn local_peer_id(&self) -> PeerId {
545545
self.local_peer_id
546546
}
547+
548+
/// Dynamically unregister a protocol.
549+
///
550+
/// This must be called when a protocol is no longer needed (e.g. user dropped the protocol
551+
/// handle).
552+
pub fn unregister_protocol(&self) {
553+
self.transport_handle.unregister_protocol(self.protocol.clone());
554+
}
547555
}
548556

549557
impl Stream for TransportService {

src/transport/manager/handle.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ pub enum InnerTransportManagerCommand {
6060
/// Remote address.
6161
address: Multiaddr,
6262
},
63+
64+
UnregisterProtocol {
65+
/// Protocol name.
66+
protocol: ProtocolName,
67+
},
6368
}
6469

6570
/// Handle for communicating with [`crate::transport::manager::TransportManager`].
@@ -285,6 +290,29 @@ impl TransportManagerHandle {
285290
TrySendError::Closed(_) => ImmediateDialError::TaskClosed,
286291
})
287292
}
293+
294+
/// Dynamically unregister a protocol.
295+
///
296+
/// This must be called when a protocol is no longer needed (e.g. user dropped the protocol
297+
/// handle).
298+
pub fn unregister_protocol(&self, protocol: ProtocolName) {
299+
tracing::info!(
300+
target: LOG_TARGET,
301+
?protocol,
302+
"Unregistering user protocol on handle drop"
303+
);
304+
305+
if let Err(err) = self
306+
.cmd_tx
307+
.try_send(InnerTransportManagerCommand::UnregisterProtocol { protocol })
308+
{
309+
tracing::error!(
310+
target: LOG_TARGET,
311+
?err,
312+
"Failed to unregister protocol"
313+
);
314+
}
315+
}
288316
}
289317

290318
pub struct TransportHandle {

src/transport/manager/mod.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,26 @@ impl TransportManager {
360360
service
361361
}
362362

363+
/// Unregister a protocol in response of the user dropping the protocol handle.
364+
fn unregister_protocol(&mut self, protocol: ProtocolName) {
365+
let Some(context) = self.protocols.remove(&protocol) else {
366+
tracing::error!(target: LOG_TARGET, ?protocol, "Cannot unregister protocol, not registered");
367+
return;
368+
};
369+
370+
for fallback in &context.fallback_names {
371+
if !self.protocol_names.remove(fallback) {
372+
tracing::error!(target: LOG_TARGET, ?fallback, ?protocol, "Cannot unregister fallback protocol, not registered");
373+
}
374+
}
375+
376+
tracing::info!(
377+
target: LOG_TARGET,
378+
?protocol,
379+
"Protocol fully unregistered"
380+
);
381+
}
382+
363383
/// Acquire `TransportHandle`.
364384
pub fn transport_handle(&self, executor: Arc<dyn Executor>) -> TransportHandle {
365385
TransportHandle {
@@ -1042,6 +1062,9 @@ impl TransportManager {
10421062
tracing::debug!(target: LOG_TARGET, ?error, "failed to dial peer")
10431063
}
10441064
}
1065+
InnerTransportManagerCommand::UnregisterProtocol { protocol } => {
1066+
self.unregister_protocol(protocol);
1067+
}
10451068
}
10461069
},
10471070

@@ -1256,7 +1279,7 @@ impl TransportManager {
12561279
?peer,
12571280
%protocol,
12581281
?connection_id,
1259-
"call to protocol would, block try sending in a blocking way",
1282+
"call to protocol would block try sending in a blocking way",
12601283
);
12611284

12621285
context

0 commit comments

Comments
 (0)