mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:17:37 +00:00
kernel: Dispatch log callbacks with btcsignals #36768
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!36768
Loading…
Reference in a new issue
No description provided.
Delete branch "pr/logsig"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem: A kernel application whose log callback logs, or destroys a kernel object, hangs. The common way to hit this is a garbage-collected language binding: Python's garbage collector can run finalizers inside the callback, and a finalizer that destroys a chainstate manager flushes and logs (https://github.com/bitcoin/bitcoin/pull/34775#issuecomment-6278257737, https://github.com/bitcoin/bitcoin/pull/34374#discussion_r4030551866). The cause is that the kernel calls log callbacks while holding the logger's mutex, so any logging from inside a callback blocks on a mutex its own thread already holds. The problem is not new and has existed since logging callbacks were introduced in #30595.
Solution: This PR delivers log entries through
btcsignals::signal, which calls callbacks without holding any lock, replacing the kernel's own callback list and custom dispatching mechanism.The change is a usability improvement and a bugfix because with it applications no longer need to reason about deadlocks and blocking behavior from a mutex they can't see. However, they may need to be updated to deal with log callbacks that are no longer serialized and connection functions that no longer block. Compatibility details are in the commit message.
The main code change is in
src/kernel/bitcoinkernel.cpp(+42/-77) accompanied by header docs and new test_kernel cases, which hang without the fix. There is also a commit switchingbtcsignals.hto useStdMutexinstead ofMutexso it is safe to use in logging callbacks.This is an alternative to the last commit of #34775, which fixes the same deadlock but only after limiting the process to one logging connection, and still hangs if a callback destroys the connection.
1 additional verified finding in the full report.
Earlier concerns remain unresolved; see the full report for their current assessments.
🟠 Minor · Concurrent pruning can destroy logging user data under the signal mutex
Concurrent connection creation and destruction can run an existing subscriber's cleanup under the signal mutex, hanging if that cleanup reconnects or emits an enabled log entry while another subscriber remains.
Location:
src/kernel/bitcoinkernel.cpp:519(head).🟠 Minor · A throwing connection diagnostic leaks the registered subscriber
With Debug or Trace delivery enabled, an allocation failure in the post-registration diagnostic returns null without deleting the connection, leaving its callback registered and violating the user-data cleanup guarantee.
Location:
src/kernel/bitcoinkernel.cpp:955(head).🟠 Minor · Callback contract overlooks same-thread kernel lock reentry
Callbacks still run while non-recursive kernel locks are held, so the new permission to call kernel functions needs same-thread restrictions as well as the cross-thread warning.
Location:
src/kernel/bitcoinkernel.h:411(head).🔵 Suggestion · Thread-safety guidance omits same-thread callback reentry
An enabled entry emitted inside a callback synchronously invokes that callback again, so a thread-safe implementation can still deadlock on its own non-recursive lock.
Location:
src/kernel/bitcoinkernel_wrapper.h:1060(head).🔵 Suggestion · Concurrency test misses destruction between the two returns
logging_callback_concurrency_testsnever checks that user data remains alive after one disconnected invocation returns while the other is still running.Location:
src/test/kernel/test_kernel.cpp:940(head).🔵 Suggestion · Test-only warning downgrade applies to all C++ targets
The i686 and Alpine additions downgrade maybe-uninitialized warnings for all configured C++ targets, although the stated trigger is confined to
test_kernel.cpp.Location:
ci/test/00_setup_env_i686.sh:19(head).Review coverage was incomplete.
Reviewed:
a9f467e8dfea· Full review report · Previous reportspermalink
Updated
7a5e5b0a49->25eff90bfd(pr/logsig.1->pr/logsig.2, compare) Add a commit disabling -Werror for a GCC 15 -Wmaybe-uninitialized false positive in the new test_kernel test, as the armhf task already does https://github.com/bitcoin/bitcoin/actions/runs/41960869850/job/127026663042 https://github.com/bitcoin/bitcoin/actions/runs/41960869850/job/127026663160Approach ACK
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
External sites
Reviews
See the guideline and AI policy for information on the review process.
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:
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.
nit: could use a trailing comment
# for std::optional::emplace in test_kernel.cpp(or so), and put this in the first line to minimize the diff churn and conflicts with other ci pulls?unrelated-nit: Obviously doesn't matter, but in theory this should use a hard-coded type in TicksSinceEpoch, not a silently fluent type.
Updated
25eff90bfd->a9f467e8df(pr/logsig.2->pr/logsig.3, compare) with maflcko's suggestions: put the CI flag first with a comment saying what it is for, and print the log timestamp with an explicit nanoseconds unitre: https://github.com/bitcoin/bitcoin/pull/36768#discussion_r4490493347
Thanks, moved it to the first line with a trailing comment, in the Alpine musl task too.
re: https://github.com/bitcoin/bitcoin/pull/36768#discussion_r4490500702
Good point, changed it to
std::chrono::nanoseconds{entry.Timestamp().time_since_epoch()}.count(), so a change to the timestamp type would fail to compile instead of silently changing the unit. I usedstd::chronoinstead ofTicksSinceEpochbecause bitcoin-chainstate is there to show what an external c++ program using the kernel library needs, so it shouldn't depend on internal util/ helpers.I've tested and reviewed the code and it lgtm. I've left a suggestion to improve a unit test
a9f467e8df: kernel: Dispatch log callbacks with btcsignals so they run without the logger mutexThis test does not yet test that after multiple threads enter the callback and after 1 of them (but not all of them) returns, the object persists. Below is one way to test the desired logic.
Diff