Skip to content

Commit 8647a56

Browse files
committed
node: retain peer after local force-close
When we initiate a force-close via `Node::force_close_channel`, the remote peer may be unreachable or, in the case of certain LND versions, mishandle the force-close error message. Keeping the peer in the store lets the background reconnection task continue firing and gives `channel_reestablish` a chance to drive recovery once the peer is reachable again. The peer is now only dropped from `close_channel_internal` on cooperative close; users who want to forget a force-closed peer must call `Node::disconnect`. Extend `do_channel_full_cycle` to assert the new retention behavior in both the force-close and cooperative-close branches.
1 parent 96bdc06 commit 8647a56

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

‎src/lib.rs‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,8 +1859,16 @@ impl Node {
18591859
})?;
18601860
}
18611861

1862-
// Check if this was the last open channel, if so, forget the peer.
1863-
if open_channels.len() == 1 {
1862+
// If this was the last open channel and we're closing cooperatively, forget the peer
1863+
// since we have no further reason to reconnect.
1864+
1865+
// For force-closes we intentionally keep the peer in the store so the background reconnection
1866+
// task keeps firing and can drive the channel_reestablish recovery flow.
1867+
// This is especially important against LND peers, which don't always handle force-closure error messages correctly.
1868+
1869+
// Note that this means a force-closed peer is retained until the user explicitly calls Node::disconnect.
1870+
1871+
if open_channels.len() == 1 && !force {
18641872
self.peer_store.remove_peer(&counterparty_node_id)?;
18651873
}
18661874
}

‎tests/common/mod.rs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,6 +1592,20 @@ pub(crate) async fn do_channel_full_cycle<E: ElectrumApi>(
15921592
assert!(node_b.list_balances().pending_balances_from_channel_closures.is_empty());
15931593
}
15941594

1595+
if force_close {
1596+
// Peer retained after local force-close to allow channel_reestablish recovery.
1597+
assert!(
1598+
node_a.list_peers().iter().any(|p| p.node_id == node_b.node_id() && p.is_persisted),
1599+
"node_b should remain persisted in node_a peer store after locally-initiated force-close"
1600+
);
1601+
} else {
1602+
// Peer removed after cooperative close — no further reason to reconnect.
1603+
assert!(
1604+
!node_a.list_peers().iter().any(|p| p.node_id == node_b.node_id() && p.is_persisted),
1605+
"node_b should be removed from node_a peer store after cooperative close"
1606+
);
1607+
}
1608+
15951609
let sum_of_all_payments_sat = (push_msat
15961610
+ invoice_amount_1_msat
15971611
+ overpaid_amount_msat

0 commit comments

Comments
 (0)