Skip to content

Commit 0758bd7

Browse files
Fix outbound payments memory leak on buggy router
Prior to this patch, if we attempted to send a payment or probe to a buggy route, we would error but continue storing the pending outbound payment forever. Attempts to retry would result in a “duplicate payment” error. In the case of ChannelManager::send_payment, we would also fail to generate a PaymentFailed event, even if the user manually called abandon_payment. This bug is unlikely to have ever been hit in the wild as most users use LDK’s router. Discovered in the course of adding a new send_to_route API. Now, we’ll properly generate events and remove the outbound from storage.
1 parent 490777c commit 0758bd7

File tree

5 files changed

+127
-13
lines changed

5 files changed

+127
-13
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14863,6 +14863,7 @@ mod tests {
1486314863
},
1486414864
_ => panic!("unexpected error")
1486514865
}
14866+
assert!(nodes[0].node.list_recent_payments().is_empty());
1486614867
}
1486714868

1486814869
#[test]

lightning/src/ln/functional_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6482,6 +6482,7 @@ fn test_payment_route_reaching_same_channel_twice() {
64826482
RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
64836483
), false, APIError::InvalidRoute { ref err },
64846484
assert_eq!(err, &"Path went through the same channel twice"));
6485+
assert!(nodes[0].node.list_recent_payments().is_empty());
64856486
}
64866487

64876488
// BOLT 2 Requirements for the Sender when constructing and sending an update_add_htlc message.

lightning/src/ln/outbound_payment.rs

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,9 +1000,9 @@ impl OutboundPayments {
10001000
);
10011001
if let Err(e) = result {
10021002
self.handle_pay_route_err(
1003-
e, payment_id, payment_hash, route, route_params, router, first_hops,
1004-
&inflight_htlcs, entropy_source, node_signer, best_block_height, logger,
1005-
pending_events, &send_payment_along_path
1003+
e, payment_id, payment_hash, route, route_params, onion_session_privs, router, first_hops,
1004+
&inflight_htlcs, entropy_source, node_signer, best_block_height, logger, pending_events,
1005+
&send_payment_along_path
10061006
);
10071007
}
10081008
Ok(())
@@ -1281,7 +1281,11 @@ impl OutboundPayments {
12811281
log_info!(logger, "Sending payment with id {} and hash {} returned {:?}",
12821282
payment_id, payment_hash, res);
12831283
if let Err(e) = res {
1284-
self.handle_pay_route_err(e, payment_id, payment_hash, route, route_params, router, first_hops, &inflight_htlcs, entropy_source, node_signer, best_block_height, logger, pending_events, &send_payment_along_path);
1284+
self.handle_pay_route_err(
1285+
e, payment_id, payment_hash, route, route_params, onion_session_privs, router, first_hops,
1286+
&inflight_htlcs, entropy_source, node_signer, best_block_height, logger, pending_events,
1287+
&send_payment_along_path
1288+
);
12851289
}
12861290
Ok(())
12871291
}
@@ -1437,15 +1441,21 @@ impl OutboundPayments {
14371441
best_block_height, &send_payment_along_path);
14381442
log_info!(logger, "Result retrying payment id {}: {:?}", &payment_id, res);
14391443
if let Err(e) = res {
1440-
self.handle_pay_route_err(e, payment_id, payment_hash, route, route_params, router, first_hops, inflight_htlcs, entropy_source, node_signer, best_block_height, logger, pending_events, send_payment_along_path);
1444+
self.handle_pay_route_err(
1445+
e, payment_id, payment_hash, route, route_params, onion_session_privs, router, first_hops,
1446+
inflight_htlcs, entropy_source, node_signer, best_block_height, logger, pending_events,
1447+
send_payment_along_path
1448+
);
14411449
}
14421450
}
14431451

14441452
fn handle_pay_route_err<R: Deref, NS: Deref, ES: Deref, IH, SP, L: Deref>(
14451453
&self, err: PaymentSendFailure, payment_id: PaymentId, payment_hash: PaymentHash, route: Route,
1446-
mut route_params: RouteParameters, router: &R, first_hops: Vec<ChannelDetails>,
1447-
inflight_htlcs: &IH, entropy_source: &ES, node_signer: &NS, best_block_height: u32, logger: &L,
1448-
pending_events: &Mutex<VecDeque<(events::Event, Option<EventCompletionAction>)>>, send_payment_along_path: &SP,
1454+
mut route_params: RouteParameters, onion_session_privs: Vec<[u8; 32]>, router: &R,
1455+
first_hops: Vec<ChannelDetails>, inflight_htlcs: &IH, entropy_source: &ES, node_signer: &NS,
1456+
best_block_height: u32, logger: &L,
1457+
pending_events: &Mutex<VecDeque<(events::Event, Option<EventCompletionAction>)>>,
1458+
send_payment_along_path: &SP,
14491459
)
14501460
where
14511461
R::Target: Router,
@@ -1474,11 +1484,13 @@ impl OutboundPayments {
14741484
},
14751485
PaymentSendFailure::PathParameterError(results) => {
14761486
log_error!(logger, "Failed to send to route due to parameter error in a single path. Your router is buggy");
1487+
self.remove_session_privs(payment_id, &route, onion_session_privs);
14771488
Self::push_path_failed_evs_and_scids(payment_id, payment_hash, &mut route_params, route.paths, results.into_iter(), logger, pending_events);
14781489
self.abandon_payment(payment_id, PaymentFailureReason::UnexpectedError, pending_events);
14791490
},
14801491
PaymentSendFailure::ParameterError(e) => {
14811492
log_error!(logger, "Failed to send to route due to parameter error: {:?}. Your router is buggy", e);
1493+
self.remove_session_privs(payment_id, &route, onion_session_privs);
14821494
self.abandon_payment(payment_id, PaymentFailureReason::UnexpectedError, pending_events);
14831495
},
14841496
PaymentSendFailure::DuplicatePayment => debug_assert!(false), // unreachable
@@ -1518,6 +1530,21 @@ impl OutboundPayments {
15181530
}
15191531
}
15201532

1533+
// If a payment fails after adding the pending payment but before any HTLCs are locked into
1534+
// channels, we need to clear the session_privs in order for abandoning the payment to succeed.
1535+
fn remove_session_privs(
1536+
&self, payment_id: PaymentId, route: &Route, onion_session_privs: Vec<[u8; 32]>
1537+
) {
1538+
if let Some(payment) = self.pending_outbound_payments.lock().unwrap().get_mut(&payment_id) {
1539+
for (path, session_priv_bytes) in route.paths.iter().zip(onion_session_privs.into_iter()) {
1540+
let removed = payment.remove(&session_priv_bytes, Some(path));
1541+
debug_assert!(removed, "This can't happen as the payment has an entry for this path added by callers");
1542+
}
1543+
} else {
1544+
debug_assert!(false, "This can't happen as the payment was added by callers");
1545+
}
1546+
}
1547+
15211548
pub(super) fn send_probe<ES: Deref, NS: Deref, F>(
15221549
&self, path: Path, probing_cookie_secret: [u8; 32], entropy_source: &ES, node_signer: &NS,
15231550
best_block_height: u32, send_payment_along_path: F
@@ -1791,7 +1818,7 @@ impl OutboundPayments {
17911818
let cur_height = best_block_height + 1;
17921819
let mut results = Vec::new();
17931820
debug_assert_eq!(route.paths.len(), onion_session_privs.len());
1794-
for (path, session_priv_bytes) in route.paths.iter().zip(onion_session_privs.into_iter()) {
1821+
for (path, session_priv_bytes) in route.paths.iter().zip(onion_session_privs.iter()) {
17951822
let mut path_res = send_payment_along_path(SendAlongPathArgs {
17961823
path: &path, payment_hash: &payment_hash, recipient_onion, total_value,
17971824
cur_height, payment_id, keysend_preimage: &keysend_preimage, invoice_request,
@@ -1887,9 +1914,15 @@ impl OutboundPayments {
18871914
// If we failed to send any paths, remove the new PaymentId from the `pending_outbound_payments`
18881915
// map as the payment is free to be resent.
18891916
fn remove_outbound_if_all_failed(&self, payment_id: PaymentId, err: &PaymentSendFailure) {
1890-
if let &PaymentSendFailure::AllFailedResendSafe(_) = err {
1891-
let removed = self.pending_outbound_payments.lock().unwrap().remove(&payment_id).is_some();
1892-
debug_assert!(removed, "We should always have a pending payment to remove here");
1917+
match err {
1918+
PaymentSendFailure::AllFailedResendSafe(_)
1919+
| PaymentSendFailure::ParameterError(_)
1920+
| PaymentSendFailure::PathParameterError(_) =>
1921+
{
1922+
let removed = self.pending_outbound_payments.lock().unwrap().remove(&payment_id).is_some();
1923+
debug_assert!(removed, "We should always have a pending payment to remove here");
1924+
},
1925+
PaymentSendFailure::DuplicatePayment | PaymentSendFailure::PartialFailure { .. } => {}
18931926
}
18941927
}
18951928

lightning/src/ln/payment_tests.rs

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::types::payment::{PaymentHash, PaymentSecret, PaymentPreimage};
2424
use crate::ln::chan_utils;
2525
use crate::ln::msgs::ChannelMessageHandler;
2626
use crate::ln::onion_utils;
27-
use crate::ln::outbound_payment::{IDEMPOTENCY_TIMEOUT_TICKS, Retry, RetryableSendFailure};
27+
use crate::ln::outbound_payment::{IDEMPOTENCY_TIMEOUT_TICKS, ProbeSendFailure, Retry, RetryableSendFailure};
2828
use crate::routing::gossip::{EffectiveCapacity, RoutingFees};
2929
use crate::routing::router::{get_route, Path, PaymentParameters, Route, Router, RouteHint, RouteHintHop, RouteHop, RouteParameters};
3030
use crate::routing::scoring::ChannelUsage;
@@ -1249,6 +1249,7 @@ fn sent_probe_is_probe_of_sending_node() {
12491249
// First check we refuse to build a single-hop probe
12501250
let (route, _, _, _) = get_route_and_payment_hash!(&nodes[0], nodes[1], 100_000);
12511251
assert!(nodes[0].node.send_probe(route.paths[0].clone()).is_err());
1252+
assert!(nodes[0].node.list_recent_payments().is_empty());
12521253

12531254
// Then build an actual two-hop probing path
12541255
let (route, _, _, _) = get_route_and_payment_hash!(&nodes[0], nodes[2], 100_000);
@@ -4375,3 +4376,77 @@ fn test_non_strict_forwarding() {
43754376
let events = nodes[0].node.get_and_clear_pending_events();
43764377
expect_payment_failed_conditions_event(events, payment_hash, false, PaymentFailedConditions::new().blamed_scid(routed_scid));
43774378
}
4379+
4380+
#[test]
4381+
fn remove_pending_outbounds_on_buggy_router() {
4382+
// Ensure that if a payment errors due to a bogus route, we'll abandon the payment and remove the
4383+
// pending outbound from storage.
4384+
let chanmon_cfgs = create_chanmon_cfgs(2);
4385+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4386+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4387+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4388+
create_announced_chan_between_nodes(&nodes, 0, 1);
4389+
4390+
let amt_msat = 10_000;
4391+
let payment_id = PaymentId([42; 32]);
4392+
let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), 0)
4393+
.with_bolt11_features(nodes[1].node.bolt11_invoice_features()).unwrap();
4394+
let (mut route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], payment_params, amt_msat);
4395+
4396+
// Extend the path by itself, essentially simulating route going through same channel twice
4397+
let cloned_hops = route.paths[0].hops.clone();
4398+
route.paths[0].hops.extend_from_slice(&cloned_hops);
4399+
let route_params = route.route_params.clone().unwrap();
4400+
nodes[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
4401+
4402+
nodes[0].node.send_payment(
4403+
payment_hash, RecipientOnionFields::secret_only(payment_secret), payment_id, route_params,
4404+
Retry::Attempts(1) // Even though another attempt is allowed, the payment should fail
4405+
).unwrap();
4406+
let events = nodes[0].node.get_and_clear_pending_events();
4407+
assert_eq!(events.len(), 2);
4408+
match &events[0] {
4409+
Event::PaymentPathFailed { failure, payment_failed_permanently, .. } => {
4410+
assert_eq!(failure, &PathFailure::InitialSend {
4411+
err: APIError::InvalidRoute { err: "Path went through the same channel twice".to_string() }
4412+
});
4413+
assert!(!payment_failed_permanently);
4414+
},
4415+
_ => panic!()
4416+
}
4417+
match events[1] {
4418+
Event::PaymentFailed { reason, .. } => {
4419+
assert_eq!(reason.unwrap(), PaymentFailureReason::UnexpectedError);
4420+
},
4421+
_ => panic!()
4422+
}
4423+
assert!(nodes[0].node.list_recent_payments().is_empty());
4424+
}
4425+
4426+
#[test]
4427+
fn remove_pending_outbound_probe_on_buggy_path() {
4428+
// Ensure that if a probe errors due to a bogus route, we'll return an error and remove the
4429+
// pending outbound from storage.
4430+
let chanmon_cfgs = create_chanmon_cfgs(2);
4431+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4432+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4433+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4434+
create_announced_chan_between_nodes(&nodes, 0, 1);
4435+
4436+
let amt_msat = 10_000;
4437+
let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), 0)
4438+
.with_bolt11_features(nodes[1].node.bolt11_invoice_features()).unwrap();
4439+
let (mut route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[1], payment_params, amt_msat);
4440+
4441+
// Extend the path by itself, essentially simulating route going through same channel twice
4442+
let cloned_hops = route.paths[0].hops.clone();
4443+
route.paths[0].hops.extend_from_slice(&cloned_hops);
4444+
4445+
assert_eq!(
4446+
nodes[0].node.send_probe(route.paths.pop().unwrap()).unwrap_err(),
4447+
ProbeSendFailure::ParameterError(
4448+
APIError::InvalidRoute { err: "Path went through the same channel twice".to_string() }
4449+
)
4450+
);
4451+
assert!(nodes[0].node.list_recent_payments().is_empty());
4452+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## Bug Fixes
2+
3+
* Fixed a rare case where a custom router returning a buggy route could result in holding onto a
4+
pending payment forever and in some cases failing to generate a PaymentFailed event (#3531).

0 commit comments

Comments
 (0)