Skip to content

kernel: Replace application log callbacks with file logging - #36197

Closed
w0xlt wants to merge 3 commits into
bitcoin:masterfrom
w0xlt:kernel/file-logging
Closed

w0xlt wants to merge 3 commits into
bitcoin:masterfrom
w0xlt:kernel/file-logging

Conversation

@w0xlt

@w0xlt w0xlt commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Kernel logging currently invokes application handlers while holding the logger mutex and, in some cases, validation locks. Slow handlers delay the caller, and handlers that call back into the kernel or acquire conflicting locks can deadlock.

This PR replaces those callbacks with the file writer already used by the node. Applications provide a file path through the C API or C++ wrapper, and the existing logger appends messages to that file. A single logging connection owns the file and closes it when destroyed.

This keeps application log handling out of kernel execution while reusing Core’s existing file writer and bounded startup buffer. It also avoids adding a separate delivery queue with its own capacity, overflow, and consumer lifecycle concerns. Applications can read the file independently and forward messages elsewhere.

File output keeps the node’s existing logging behavior: synchronous, best-effort writes with bounded startup buffering.

Reuse the shared append backend and mutex for owned file output. Preserve other outputs on close and recover coherently from failed startup replay.
Accept an owned file path through the C API and C++ wrapper, and migrate the example and in-tree callers together. File output retains the shared logger synchronous, best-effort semantics.
Exercise public path, ownership, replay, filtering, and concurrent-close boundaries. Check appended logging and early log-open failure in the chainstate example.
@DrahtBot

DrahtBot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

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/36197.

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Concept NACK stickies-v

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #35322 (logging: streamline Logger state and drop redundant methods by ryanofsky)
  • #34778 (logging: rewrite macros to enforce restrictions at compile-time, improve efficiency and usability by ryanofsky)
  • #34775 (kernel: make logging callback global by stickies-v)
  • #34374 (kernel: use struct-based logging and simplify logging interface by stickies-v)
  • #33847 (kernel: Improve logging API by ryanofsky)
  • #33646 (log: check fclose() results and report safely in logging.cpp by cedwies)
  • #30342 (kernel, logging: Pass Logger instances to kernel objects by ryanofsky)
  • #29256 (log, refactor: Allow log macros to accept context arguments by ryanofsky)

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.

LLM Linter (✨ experimental)

Possible places where comparison-specific test macros should replace generic comparisons:

  • [src/test/kernel/test_kernel.cpp] BOOST_CHECK_THROW(Logger{PathToString(rejected_path)}, std::runtime_error); -> use BOOST_CHECK_EXCEPTION with a predicate that checks the failure reason, if the exact rejection is important.
  • [src/test/kernel/test_kernel.cpp] BOOST_CHECK_THROW(Logger{PathToString(rejected_path)}, std::runtime_error); -> use BOOST_CHECK_EXCEPTION with a predicate that checks the failure reason, if the exact rejection is important.
  • [src/test/kernel/test_kernel.cpp] BOOST_CHECK_THROW(Logger{std::string_view{}}, std::runtime_error); -> use BOOST_CHECK_EXCEPTION with a predicate that checks the failure reason, if the exact rejection is important.
  • [src/test/kernel/test_kernel.cpp] BOOST_CHECK_THROW(Block{invalid_block}, std::runtime_error); -> use BOOST_CHECK_EXCEPTION with a predicate that checks the failure reason, if the exact rejection is important.
  • [src/test/logging_tests.cpp] BOOST_CHECK_THROW(logger.StartFileLogging(log_path), std::runtime_error); -> use BOOST_CHECK_EXCEPTION with a predicate that checks the failure reason, if the exact rejection is important.

No Python comparison macro suggestions were found.

2026-09-09 07:29:03

@stickies-v

Copy link
Copy Markdown
Contributor

Concept NACK. This increases coupling with node, and increases dependency on filesystem, both of which go against the direction of kernel. I think it's already pretty easy for consumers to not have slow or deadlocking callbacks, so this doesn't seem like an actual problem at the moment, unless you have data / use cases that point to the contrary?

Additionally, both concerns can be addressed by improving the implementation, e.g. less locking, logging on separate threads, etc.

@sedited

sedited commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

I agree with stickies-v here. It seems fine to give the responsibility of "block the logging callback for as little time as possible" to the consuming developer.

@w0xlt

w0xlt commented Sep 15, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review @stickies-v @sedited

I opened this PR while working on asynchronous block processing, but found another approach that does not require this logging API change, so I’m closing this PR.

I still think that the invoking application callbacks while holding internal locks can be a design hazard (Ex: CP.22: Never call unknown code while holding a lock (e.g., a callback). But this can be discussed separately.

@w0xlt w0xlt closed this Sep 15, 2026
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.

4 participants