Skip to content

Commit 2d7b335

Browse files
committed
Timeout DNS resolution requests after some time
.. as otherwise we might wait indefinitely for a service to respond.
1 parent 127534b commit 2d7b335

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ pub const WALLET_KEYS_SEED_LEN: usize = 64;
9999
// The timeout after which we abort a external scores sync operation.
100100
pub(crate) const EXTERNAL_PATHFINDING_SCORES_SYNC_TIMEOUT_SECS: u64 = 5;
101101

102+
// The timeout after which we abort a parsing/looking up an HRN resolution.
103+
pub(crate) const HRN_RESOLUTION_TIMEOUT_SECS: u64 = 5;
104+
102105
#[derive(Debug, Clone)]
103106
/// Represents the configuration of an [`Node`] instance.
104107
///

src/payment/unified.rs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//! [BOLT 11]: https://github.com/lightning/bolts/blob/master/11-payment-encoding.md
1616
//! [BOLT 12]: https://github.com/lightning/bolts/blob/master/12-offer-encoding.md
1717
use std::sync::Arc;
18+
use std::time::Duration;
1819
use std::vec::IntoIter;
1920

2021
use bip21::de::ParamKind;
@@ -29,6 +30,7 @@ use lightning::onion_message::dns_resolution::HumanReadableName;
2930
use lightning::routing::router::RouteParametersConfig;
3031
use lightning_invoice::{Bolt11Invoice, Bolt11InvoiceDescription, Description};
3132

33+
use crate::config::HRN_RESOLUTION_TIMEOUT_SECS;
3234
use crate::error::Error;
3335
use crate::ffi::maybe_wrap;
3436
use crate::logger::{log_error, LdkLogger, Logger};
@@ -159,17 +161,24 @@ impl UnifiedPayment {
159161
&self, uri_str: &str, amount_msat: Option<u64>,
160162
route_parameters: Option<RouteParametersConfig>,
161163
) -> Result<UnifiedPaymentResult, Error> {
162-
let instructions = PaymentInstructions::parse(
164+
let parse_fut = PaymentInstructions::parse(
163165
uri_str,
164166
self.config.network,
165167
self.hrn_resolver.as_ref(),
166168
false,
167-
)
168-
.await
169-
.map_err(|e| {
170-
log_error!(self.logger, "Failed to parse payment instructions: {:?}", e);
171-
Error::UriParameterParsingFailed
172-
})?;
169+
);
170+
171+
let instructions =
172+
tokio::time::timeout(Duration::from_secs(HRN_RESOLUTION_TIMEOUT_SECS), parse_fut)
173+
.await
174+
.map_err(|e| {
175+
log_error!(self.logger, "Payment instructions resolution timed out: {:?}", e);
176+
Error::UriParameterParsingFailed
177+
})?
178+
.map_err(|e| {
179+
log_error!(self.logger, "Failed to parse payment instructions: {:?}", e);
180+
Error::UriParameterParsingFailed
181+
})?;
173182

174183
let resolved = match instructions {
175184
PaymentInstructions::ConfigurableAmount(instr) => {
@@ -183,10 +192,22 @@ impl UnifiedPayment {
183192
Error::InvalidAmount
184193
})?;
185194

186-
instr.set_amount(amt, self.hrn_resolver.as_ref()).await.map_err(|e| {
187-
log_error!(self.logger, "Failed to set amount: {:?}", e);
188-
Error::InvalidAmount
189-
})?
195+
let fut = instr.set_amount(amt, self.hrn_resolver.as_ref());
196+
197+
tokio::time::timeout(Duration::from_secs(HRN_RESOLUTION_TIMEOUT_SECS), fut)
198+
.await
199+
.map_err(|e| {
200+
log_error!(
201+
self.logger,
202+
"Payment instructions resolution timed out: {:?}",
203+
e
204+
);
205+
Error::UriParameterParsingFailed
206+
})?
207+
.map_err(|e| {
208+
log_error!(self.logger, "Failed to set amount: {:?}", e);
209+
Error::InvalidAmount
210+
})?
190211
},
191212
PaymentInstructions::FixedAmount(instr) => {
192213
if let Some(user_amount) = amount_msat {

0 commit comments

Comments
 (0)