Repository navigation
Don't panic when writing to the log file fails - #1265
ajaysehwal wants to merge 1 commit into
Conversation
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.
|
I've assigned @tnull as a reviewer! |
| 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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Yeah, currently we assume everyone to run this with panic = "abort".
The file logger reopens the log file for each record and
expects both the open and the write to succeed. Any I/O error (a full disk, a removed log directory, changed permissions) therefore panics in whatever thread is logging, which can be a background task holding locks. Withpanic = "abort"this kills the host process.This prints the error to stderr and drops the record instead, as we already do when setting up the log file fails in
Logger::new_fs_writer. The file is still reopened per record, so external log rotation keeps working.Testing
Added
log_file_write_errors_do_not_panic, which removes the log directory after creating the logger and then logs a line. It panics without this change and passes with it.