Skip to content

Commit

Permalink
fix: catch panic of reading scorer
Browse files Browse the repository at this point in the history
On iOS it seems like we can run into a problem where we can't read the scorer file anymore. Reading will then just panic. Here we catch the panic and delete the broken file which allows us to continue.
  • Loading branch information
bonomat authored and holzeis committed Jul 13, 2023
1 parent 6dba5e1 commit 79f7601
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions crates/ln-dlc-node/src/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use bitcoin::Network;
use lightning::routing::scoring::ProbabilisticScorer;
use lightning::routing::scoring::ProbabilisticScoringParameters;
use lightning::util::ser::ReadableArgs;
use std::fs;
use std::fs::File;
use std::io::BufReader;
use std::panic;
use std::path::Path;
use std::sync::Arc;

Expand All @@ -17,9 +19,21 @@ pub(crate) fn read_scorer(
let params = ProbabilisticScoringParameters::default();
if let Ok(file) = File::open(path) {
let args = (params.clone(), graph.clone(), logger.clone());
match ProbabilisticScorer::read(&mut BufReader::new(file), args) {
Ok(scorer) => return scorer,
Err(e) => tracing::error!("Failed to read scorer from disk: {e}"),
let result =
panic::catch_unwind(|| ProbabilisticScorer::read(&mut BufReader::new(file), args));
match result {
Ok(Ok(scorer)) => {
return scorer;
}
Ok(Err(err)) => {
tracing::error!("Could not decode scorer {err:#}");
}
Err(_) => {
tracing::error!("Reading scorer panicked");
if let Err(err) = fs::remove_file(path) {
tracing::error!("Could not even delete file #{err}");
}
}
}
}
ProbabilisticScorer::new(params, graph, logger)
Expand Down

0 comments on commit 79f7601

Please sign in to comment.