From 9439c2d807d094a2ab49b4e2d3637ffd9104f8c2 Mon Sep 17 00:00:00 2001 From: ajaysehwal Date: Thu, 26 Nov 2026 19:02:46 +0530 Subject: [PATCH] Don't panic when writing to the log file fails The file logger reopened the log file for each record and `expect`ed both the open and the write to succeed. Any I/O error, e.g. a full disk or a removed log directory, therefore panicked in whatever thread was logging, which could be a background task holding locks. Print the error to stderr instead and drop the record, as we already do when setting up the log file fails. --- src/logger.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/logger.rs b/src/logger.rs index ca65a3d290..657f91cdcc 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -257,10 +257,11 @@ impl LogWriter for Writer { context, ); - open_log_file(file_path) - .expect("Failed to open log file") - .write_all(log.as_bytes()) - .expect("Failed to write to log file") + if let Err(e) = + open_log_file(file_path).and_then(|mut file| file.write_all(log.as_bytes())) + { + eprintln!("ERROR: Failed to write to log file: {}", e); + } }, Writer::LogFacadeWriter => { let level = match record.level { @@ -392,6 +393,19 @@ mod tests { fs::remove_dir_all(log_dir).unwrap(); } + #[cfg(unix)] + #[test] + fn log_file_write_errors_do_not_panic() { + let log_dir = random_storage_path(); + let log_path = log_dir.join("ldk_node.log"); + let logger = + Logger::new_fs_writer(log_path.to_str().unwrap().to_string(), LogLevel::Info).unwrap(); + + fs::remove_dir_all(&log_dir).unwrap(); + log_info!(logger, "Logging after the log directory was removed"); + assert!(!log_path.exists()); + } + /// A minimal log facade logger that captures log output for testing. struct TestLogger { log: Arc>,