Skip to content

Conversation

@cedwies
Copy link

@cedwies cedwies commented Oct 17, 2025

fclose() can report write errors (for example if the disk is full or the filesystem has a problem). Right now, some fclose() calls in src/logging.cpp ignore the return value. This means errors might go unnoticed and log lines could be lost without warning.

What this PR does:

  • Add a small helper that prints fclose() errors to stderr (with path and errno).
  • In shutdown: close m_fileout safely and report errors.
  • In reopen: open the new file, swap it in, close the old one, and report errors if closing fails.
  • In shrink/rotate: check all fclose(file) calls and report failures.

No other behavior changes. Normal logging, rotation, and console output remain unchanged.

@DrahtBot
Copy link
Contributor

DrahtBot commented Oct 17, 2025

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/33646.

Reviews

See the guideline for information on the review process.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #34038 (logging: API improvements by ajtowns)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

Copy link
Contributor

@ajtowns ajtowns left a comment

Choose a reason for hiding this comment

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

Should rebase on top of master rather than including a merge commit.

m_msgs_before_open.clear();
}
if (f && fclose(f) != 0) {
ReportLogIOError("fclose", m_file_path);
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the value in having this outside the m_cs guard?

Copy link
Author

Choose a reason for hiding this comment

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

I put the fclose outside the lock to avoid holding the mutex during potentially slow I/O (like flushing the file). We swap m_fileout inside, so the state is safe, and then close the old file without blocking other threads. ReportLogIOError is designed to be lock-free anyway, if I am not missing something.

Some buffered I/O errors surface only on fclose(). Several fclose() calls in
src/logging.cpp ignored the return value which can risk silent loss of log lines
or misleading success during rotate/truncate.

Check return of fclose() and include errno in diagnostics. For the active log sink,
detach under lock and close outside the lock. On failure, report directly
to stderr (which avoids recursive logging) and flush. No behavior change on success.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants