Skip to content

Commit 86e3672

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 ca6933d commit 86e3672

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

src/config.rs

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

51+
// The timeout after which we abort a wallet syncing operation.
52+
pub(crate) const LDK_WALLET_SYNC_TIMEOUT_SECS: u64 = 30;
53+
5154
// The length in bytes of our wallets' keys seed.
5255
pub(crate) const WALLET_KEYS_SEED_LEN: usize = 64;
5356

src/lib.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ pub use builder::BuildError;
123123
pub use builder::NodeBuilder as Builder;
124124

125125
use config::{
126-
NODE_ANN_BCAST_INTERVAL, PEER_RECONNECTION_INTERVAL,
126+
LDK_WALLET_SYNC_TIMEOUT_SECS, NODE_ANN_BCAST_INTERVAL, PEER_RECONNECTION_INTERVAL,
127127
RESOLVED_CHANNEL_MONITOR_ARCHIVAL_INTERVAL, RGS_SYNC_INTERVAL,
128128
WALLET_SYNC_INTERVAL_MINIMUM_SECS,
129129
};
@@ -373,25 +373,31 @@ impl Node {
373373
&*sync_sweeper as &(dyn Confirm + Sync + Send),
374374
];
375375
let now = Instant::now();
376-
match tx_sync.sync(confirmables).await {
377-
Ok(()) => {
378-
log_trace!(
379-
sync_logger,
380-
"Background sync of Lightning wallet finished in {}ms.",
381-
now.elapsed().as_millis()
382-
);
383-
let unix_time_secs_opt =
384-
SystemTime::now().duration_since(UNIX_EPOCH).ok().map(|d| d.as_secs());
385-
*sync_wallet_timestamp.write().unwrap() = unix_time_secs_opt;
386-
387-
periodically_archive_fully_resolved_monitors(
388-
Arc::clone(&archive_cman),
389-
Arc::clone(&archive_cmon),
390-
Arc::clone(&sync_monitor_archival_height)
391-
);
376+
let timeout_fut = tokio::time::timeout(Duration::from_secs(LDK_WALLET_SYNC_TIMEOUT_SECS), tx_sync.sync(confirmables));
377+
match timeout_fut.await {
378+
Ok(res) => match res {
379+
Ok(()) => {
380+
log_trace!(
381+
sync_logger,
382+
"Background sync of Lightning wallet finished in {}ms.",
383+
now.elapsed().as_millis()
384+
);
385+
let unix_time_secs_opt =
386+
SystemTime::now().duration_since(UNIX_EPOCH).ok().map(|d| d.as_secs());
387+
*sync_wallet_timestamp.write().unwrap() = unix_time_secs_opt;
388+
389+
periodically_archive_fully_resolved_monitors(
390+
Arc::clone(&archive_cman),
391+
Arc::clone(&archive_cmon),
392+
Arc::clone(&sync_monitor_archival_height)
393+
);
394+
}
395+
Err(e) => {
396+
log_error!(sync_logger, "Background sync of Lightning wallet failed: {}", e)
397+
}
392398
}
393399
Err(e) => {
394-
log_error!(sync_logger, "Background sync of Lightning wallet failed: {}", e)
400+
log_error!(sync_logger, "Background sync of Lightning wallet timed out: {}", e)
395401
}
396402
}
397403
}

0 commit comments

Comments
 (0)