Skip to content

Don't panic when writing to the log file fails - #1265

Closed
ajaysehwal wants to merge 1 commit into
lightningdevkit:mainfrom
ajaysehwal:fix-logger-io-panic
Closed

ajaysehwal wants to merge 1 commit into
lightningdevkit:mainfrom
ajaysehwal:fix-logger-io-panic

Conversation

@ajaysehwal

Copy link
Copy Markdown
Contributor

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. With panic = "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.

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.
@ldk-reviews-bot

ldk-reviews-bot commented Nov 26, 2026 •

Copy link
Copy Markdown

I've assigned @tnull as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

Comment thread src/logger.rs
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".

@ajaysehwal ajaysehwal closed this Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants