Delete process internal bitcoind locks in case of panic

This commit is contained in:
edouardparis 2025-05-06 10:56:23 +02:00
parent 0488ed37a3
commit 40dc9ae8c0
2 changed files with 39 additions and 2 deletions

View File

@ -30,6 +30,7 @@ use liana_gui::{
launcher::{self, Launcher},
loader::{self, Loader},
logger::Logger,
node::bitcoind::delete_all_bitcoind_locks_for_process,
services::connect::{
client::backend::{api, BackendWalletClient},
login,
@ -537,7 +538,7 @@ fn main() -> Result<(), Box<dyn Error>> {
None
};
setup_panic_hook();
setup_panic_hook(&config.liana_directory);
let settings = Settings {
id: Some("Liana".to_string()),
@ -584,8 +585,13 @@ fn main() -> Result<(), Box<dyn Error>> {
}
// A panic in any thread should stop the main thread, and print the panic.
fn setup_panic_hook() {
fn setup_panic_hook(liana_directory: &LianaDirectory) {
let bitcoind_dir = liana_directory.bitcoind_directory();
std::panic::set_hook(Box::new(move |panic_info| {
error!("Panic occured");
if let Err(e) = delete_all_bitcoind_locks_for_process(bitcoind_dir.clone()) {
error!("Failed to delete internal bitcoind locks: {}", e);
}
let file = panic_info
.location()
.map(|l| l.file())

View File

@ -616,6 +616,37 @@ impl LockFile {
}
}
// In case of panic, we remove all the bitcoind locks created by the process.
pub fn delete_all_bitcoind_locks_for_process(
directory: BitcoindDirectory,
) -> Result<(), Box<dyn std::error::Error>> {
let locks_directory = directory.path().join(LOCK_DIRECTORY_NAME);
if !locks_directory.exists() {
tracing::debug!("No internal bitcoind locks for the current process");
return Ok(());
}
tracing::info!("Deleting all internal bitcoind locks for the current process");
let process_prefix = format!("{}-", std::process::id());
for network_dir in std::fs::read_dir(&locks_directory)? {
let dir = network_dir?.path();
for lock_file in std::fs::read_dir(&dir)? {
let file = lock_file?.path();
if let Some(name) = file.file_name().and_then(|n| n.to_str()) {
if name.starts_with(&process_prefix) {
std::fs::remove_file(file)?;
}
}
}
if std::fs::read_dir(&dir)?.next().is_none() {
std::fs::remove_dir(dir)?;
}
}
if std::fs::read_dir(&locks_directory)?.next().is_none() {
std::fs::remove_dir(locks_directory)?;
}
Ok(())
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum RpcAuthType {
CookieFile,