Skip to content

Commit 87c939b

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 45ecf43 commit 87c939b

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

src/config.rs

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

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

src/lib.rs

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

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

0 commit comments

Comments
 (0)