Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, silently failing is strictly worse than panicking here. Panicking if we can't write to disk is actually acceptable behavior, as there's no way to recover really.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My concern was mainly the unwinding case: logging happens on arbitrary threads, often with a lock held (LDK logs under its internal locks), so a panic there can leave poisoned mutexes and a half-working node, unlike with panic = "abort" where it is fail-stop.

That said, I understand silently losing logs isn't great either, and the remaining persistence-failure handling is already being discussed in #381. Happy to close this one if you'd rather keep the current behavior.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, currently we assume everyone to run this with panic = "abort".

}
},
Writer::LogFacadeWriter => {
let level = match record.level {
Expand Down Expand Up @@ -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<Mutex<String>>,
Expand Down
Loading