Skip to content

Commit b7d2556

Browse files
committed
fix(p2p/webrtc): failed connections leaking
1 parent 1170171 commit b7d2556

File tree

6 files changed

+20
-2
lines changed

6 files changed

+20
-2
lines changed

node/src/action_kind.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ pub enum ActionKind {
339339
P2pConnectionOutgoingEffectfulInit,
340340
P2pConnectionOutgoingEffectfulOfferSend,
341341
P2pConnectionOutgoingEffectfulRandomInit,
342+
P2pDisconnectionFailedCleanup,
342343
P2pDisconnectionFinish,
343344
P2pDisconnectionInit,
344345
P2pDisconnectionPeerClosed,
@@ -704,7 +705,7 @@ pub enum ActionKind {
704705
}
705706

706707
impl ActionKind {
707-
pub const COUNT: u16 = 594;
708+
pub const COUNT: u16 = 595;
708709
}
709710

710711
impl std::fmt::Display for ActionKind {
@@ -1213,6 +1214,7 @@ impl ActionKindGet for P2pDisconnectionAction {
12131214
match self {
12141215
Self::Init { .. } => ActionKind::P2pDisconnectionInit,
12151216
Self::PeerClosed { .. } => ActionKind::P2pDisconnectionPeerClosed,
1217+
Self::FailedCleanup { .. } => ActionKind::P2pDisconnectionFailedCleanup,
12161218
Self::Finish { .. } => ActionKind::P2pDisconnectionFinish,
12171219
}
12181220
}

p2p/src/connection/incoming/p2p_connection_incoming_reducer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ impl P2pConnectionIncomingState {
363363
dispatcher.push_callback(callback.clone(), (rpc_id, str_error));
364364
}
365365
}
366+
dispatcher.push(P2pDisconnectionAction::FailedCleanup { peer_id });
366367

367368
Ok(())
368369
}

p2p/src/connection/outgoing/p2p_connection_outgoing_reducer.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::{
99
outgoing_effectful::P2pConnectionOutgoingEffectfulAction, P2pConnectionErrorResponse,
1010
P2pConnectionState,
1111
},
12+
disconnection::P2pDisconnectionAction,
1213
webrtc::Host,
1314
P2pNetworkKadRequestAction, P2pNetworkSchedulerAction, P2pPeerAction, P2pPeerState,
1415
P2pPeerStatus, P2pState,
@@ -535,6 +536,8 @@ impl P2pConnectionOutgoingState {
535536
dispatcher.push_callback(callback.clone(), (rpc_id, error));
536537
}
537538
}
539+
dispatcher.push(P2pDisconnectionAction::FailedCleanup { peer_id });
540+
538541
Ok(())
539542
}
540543
P2pConnectionOutgoingAction::Success { peer_id } => {

p2p/src/disconnection/p2p_disconnection_actions.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ pub enum P2pDisconnectionAction {
1818
/// Peer disconnection.
1919
#[action_event(fields(display(peer_id)), level = info)]
2020
PeerClosed { peer_id: PeerId },
21+
#[action_event(fields(display(peer_id)), level = info)]
22+
FailedCleanup { peer_id: PeerId },
2123
/// Finish disconnecting from a peer.
2224
#[action_event(fields(display(peer_id)), level = debug)]
2325
Finish { peer_id: PeerId },
@@ -31,8 +33,13 @@ impl redux::EnablingCondition<P2pState> for P2pDisconnectionAction {
3133
| P2pDisconnectionAction::Finish { peer_id } => {
3234
state.peers.get(peer_id).map_or(false, |peer| {
3335
!matches!(peer.status, P2pPeerStatus::Disconnected { .. })
36+
&& !peer.status.is_error()
3437
})
3538
}
39+
P2pDisconnectionAction::FailedCleanup { peer_id } => state
40+
.peers
41+
.get(peer_id)
42+
.map_or(false, |peer| !peer.is_libp2p() && peer.status.is_error()),
3643
}
3744
}
3845
}

p2p/src/disconnection/p2p_disconnection_reducer.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ impl P2pDisconnectedState {
6060
dispatcher.push(P2pDisconnectionEffectfulAction::Init { peer_id });
6161
Ok(())
6262
}
63+
P2pDisconnectionAction::FailedCleanup { peer_id } => {
64+
let dispatcher = state_context.into_dispatcher();
65+
dispatcher.push(P2pDisconnectionEffectfulAction::Init { peer_id });
66+
Ok(())
67+
}
6368
P2pDisconnectionAction::Finish { peer_id } => {
6469
let Some(peer) = p2p_state.peers.get_mut(&peer_id) else {
6570
bug_condition!("Invalid state for: `P2pDisconnectionAction::Finish`");

p2p/src/disconnection_effectful/p2p_disconnection_effectful_actions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
44
use crate::{P2pPeerStatus, P2pState, PeerId};
55

66
#[derive(Serialize, Deserialize, Debug, Clone, ActionEvent)]
7-
#[action_event(fields(display(peer_id), display(reason)), level = debug)]
7+
#[action_event(fields(display(peer_id)), level = debug)]
88
pub enum P2pDisconnectionEffectfulAction {
99
/// Initialize disconnection.
1010
Init { peer_id: PeerId },

0 commit comments

Comments
 (0)