Skip to content

Commit 236e976

Browse files
committed
Wrap payment instructions parsing in tokio timeout
1 parent c9aa7bf commit 236e976

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ pub enum Error {
129129
HrnParsingFailed,
130130
/// A HRN resolver was not configured
131131
HrnResolverNotConfigured,
132+
/// A Timeout occurred during an operation.
133+
TimeoutOccurred,
132134
}
133135

134136
impl fmt::Display for Error {
@@ -212,6 +214,9 @@ impl fmt::Display for Error {
212214
Self::HrnResolverNotConfigured => {
213215
write!(f, "A HRN resolver was not configured.")
214216
},
217+
Self::TimeoutOccurred => {
218+
write!(f, "A Timeout occured during an operation.")
219+
},
215220
}
216221
}
217222
}

src/payment/unified.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crate::payment::{Bolt11Payment, Bolt12Payment, OnchainPayment};
2121
use crate::types::HRNResolver;
2222
use crate::Config;
2323
use std::sync::Arc;
24+
use std::time::Duration;
2425
use std::vec::IntoIter;
2526

2627
use lightning::ln::channelmanager::PaymentId;
@@ -35,6 +36,7 @@ use bitcoin::{Amount, Txid};
3536
use bitcoin_payment_instructions::{
3637
amount::Amount as BPIAmount, PaymentInstructions, PaymentMethod,
3738
};
39+
use tokio::time::timeout;
3840

3941
type Uri<'a> = bip21::Uri<'a, NetworkChecked, Extras>;
4042

@@ -160,13 +162,25 @@ impl UnifiedPayment {
160162
Error::HrnResolverNotConfigured
161163
})?;
162164

163-
let instructions =
164-
PaymentInstructions::parse(uri_str, self.config.network, resolver.as_ref(), false)
165-
.await
166-
.map_err(|e| {
167-
log_error!(self.logger, "Failed to parse payment instructions: {:?}", e);
168-
Error::UriParameterParsingFailed
169-
})?;
165+
const TIMEOUT_DURATION: Duration = Duration::from_secs(30);
166+
167+
let instructions = timeout(
168+
TIMEOUT_DURATION,
169+
PaymentInstructions::parse(uri_str, self.config.network, resolver.as_ref(), false),
170+
)
171+
.await
172+
.map_err(|_| {
173+
log_error!(
174+
self.logger,
175+
"Payment instruction parsing timed out after {:?}",
176+
TIMEOUT_DURATION
177+
);
178+
Error::TimeoutOccurred
179+
})?
180+
.map_err(|e| {
181+
log_error!(self.logger, "Failed to parse payment instructions: {:?}", e);
182+
Error::UriParameterParsingFailed
183+
})?;
170184

171185
let resolved = match instructions {
172186
PaymentInstructions::ConfigurableAmount(instr) => {

0 commit comments

Comments
 (0)