From b5db6df80ef1fcae931642c1134fe4889d562530 Mon Sep 17 00:00:00 2001 From: Philipp Hoenisch Date: Wed, 12 Jul 2023 17:47:48 +0200 Subject: [PATCH] fix: catch panic of reading scorer 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. --- crates/ln-dlc-node/src/disk.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/crates/ln-dlc-node/src/disk.rs b/crates/ln-dlc-node/src/disk.rs index 030e16e56..14262a02a 100644 --- a/crates/ln-dlc-node/src/disk.rs +++ b/crates/ln-dlc-node/src/disk.rs @@ -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; @@ -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)