Skip to content

Commit 205d437

Browse files
committed
Add timeout for Lightning syncing
.. even though we don't expect this to block, we're better safe than sorry and start to introduce timeouts for any calls we make to remote servers.
1 parent 610ef9a commit 205d437

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ pub(crate) const WALLET_SYNC_INTERVAL_MINIMUM_SECS: u64 = 10;
4545
// The timeout after which we abort a wallet syncing operation.
4646
pub(crate) const BDK_WALLET_SYNC_TIMEOUT_SECS: u64 = 90;
4747

48+
// The timeout after which we abort a wallet syncing operation.
49+
pub(crate) const LDK_WALLET_SYNC_TIMEOUT_SECS: u64 = 30;
50+
4851
// The length in bytes of our wallets' keys seed.
4952
pub(crate) const WALLET_KEYS_SEED_LEN: usize = 64;
5053

src/lib.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ pub use builder::BuildError;
122122
pub use builder::NodeBuilder as Builder;
123123

124124
use config::{
125-
NODE_ANN_BCAST_INTERVAL, PEER_RECONNECTION_INTERVAL, RGS_SYNC_INTERVAL,
126-
WALLET_SYNC_INTERVAL_MINIMUM_SECS,
125+
LDK_WALLET_SYNC_TIMEOUT_SECS, NODE_ANN_BCAST_INTERVAL, PEER_RECONNECTION_INTERVAL,
126+
RGS_SYNC_INTERVAL, WALLET_SYNC_INTERVAL_MINIMUM_SECS,
127127
};
128128
use connection::ConnectionManager;
129129
use event::{EventHandler, EventQueue};
@@ -366,19 +366,25 @@ impl Node {
366366
&*sync_sweeper as &(dyn Confirm + Sync + Send),
367367
];
368368
let now = Instant::now();
369-
match tx_sync.sync(confirmables).await {
370-
Ok(()) => {
371-
log_trace!(
372-
sync_logger,
373-
"Background sync of Lightning wallet finished in {}ms.",
374-
now.elapsed().as_millis()
375-
);
376-
let unix_time_secs_opt =
377-
SystemTime::now().duration_since(UNIX_EPOCH).ok().map(|d| d.as_secs());
378-
*sync_wallet_timestamp.write().unwrap() = unix_time_secs_opt;
369+
let timeout_fut = tokio::time::timeout(Duration::from_secs(LDK_WALLET_SYNC_TIMEOUT_SECS), tx_sync.sync(confirmables));
370+
match timeout_fut.await {
371+
Ok(res) => match res {
372+
Ok(()) => {
373+
log_trace!(
374+
sync_logger,
375+
"Background sync of Lightning wallet finished in {}ms.",
376+
now.elapsed().as_millis()
377+
);
378+
let unix_time_secs_opt =
379+
SystemTime::now().duration_since(UNIX_EPOCH).ok().map(|d| d.as_secs());
380+
*sync_wallet_timestamp.write().unwrap() = unix_time_secs_opt;
381+
}
382+
Err(e) => {
383+
log_error!(sync_logger, "Background sync of Lightning wallet failed: {}", e)
384+
}
379385
}
380386
Err(e) => {
381-
log_error!(sync_logger, "Background sync of Lightning wallet failed: {}", e)
387+
log_error!(sync_logger, "Background sync of Lightning wallet timed out: {}", e)
382388
}
383389
}
384390
}

0 commit comments

Comments
 (0)