Review report

bitcoin/bitcoin: PR #36768

3 minor findings · 3 suggestions

Earlier concerns neither republished nor resolved: 2 suggestions. See the Run archive for their assessments.

Findings

Link

[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).

Details and suggested fix

While one thread registers a connection, signal pruning calls connected() under m_mutex. That check temporarily owns an existing callback. Another thread can disconnect that callback and clear its atomic owner before the temporary is released, leaving the temporary to destroy the lambda, its captured data, and the application's destroy callback under the mutex. Cleanup that reconnects tries to acquire the same non-recursive mutex directly. Cleanup that logs does so through signal dispatch if the entry passes the level filter and another subscriber remains; Info is sufficient. The local data reference here protects only the new subscriber. Retain every callback reference loaded during pruning until after unlocking, and add a concurrent connect/disconnect test whose cleanup logs. The race window is narrow, and no current consumer was found combining concurrent create/destroy with reentrant cleanup, so production impact is unconfirmed.


Link

[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).

Details and suggested fix

btck_LoggingConnection::create() releases its unique_ptr, so only a raw pointer remains across LogDebug("Logger connected."). The new registration already increments m_callback_count. With Debug or Trace delivery enabled, formatting the diagnostic or allocating the signal's connection snapshot can throw outside the per-callback catch. The outer catch then returns null without disconnecting the subscriber, decrementing its count, or destroying its user data. The orphan can also outlive state borrowed by the callback. Base kept an owning CallbackHandle across this diagnostic. Retain a direct-delete owning guard, such as a unique_ptr<KernelLogger::Connection>, until logging succeeds, then release it into the returned handle. This preserves delivery to the new subscriber without logging again during cleanup. Default Info filters out the diagnostic; no current caller encountering the required allocation failure was identified, so production impact is unconfirmed.


Link

[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).

Details and suggested fix

Only the logging dispatch lock is released. During block processing, ActivateBestChain holds m_chainstate_mutex across ConnectTip and the Info entry from UpdateTipLog. Processing another accepted block on the same manager from that callback reaches the same non-recursive lock again and can hang without lock-order checking. At Debug or Trace, LevelDB also forwards compaction diagnostics while holding DBImpl::mutex_; accessing that database or destroying its chainstate manager can reacquire that lock on the same thread. These constraints existed before, but the old contract prohibited other kernel calls, whereas this wording permits them. Qualify the promise as applying only to logging dispatch locks and explicitly exclude same-manager block processing and same-database access or manager destruction during the relevant callbacks. Bindings should defer those actions until the callback returns.


Optional improvements

Link

[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).

Details and suggested fix

MaybeLog calls the signal directly, and the signal invokes every connected callback without a reentry guard. If a callback, a kernel call it makes, or a finalizer it triggers logs an enabled entry, the running callback receives that entry on the same thread before its outer invocation returns. logging_callback_reentry_tests intentionally requires this behavior. State this explicitly in both the Log concept and btck_LogCallback documentation: concurrent thread safety alone is insufficient. Advise callbacks not to hold non-recursive locks across work that can log, or to establish a reentry guard before such work. Also warn against logging unconditionally on every invocation, which produces unbounded recursion.


Link

[suggestion] · Concurrency test misses destruction between the two returns

logging_callback_concurrency_tests never 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).

Details and suggested fix

The test establishes two overlapping invocations, resets the logger, and checks !destroyed. It then releases both invocations with one flag and checks destroyed only after both threads join. These assertions cannot distinguish destruction after the first return from destruction after the last, and BlockingLog::LogMessage has no member access after its wait. Give the invocations separate release gates. Release one, join its thread, and assert !destroyed while the other remains blocked. Then release and join the second and assert destroyed. This preserves the existing concurrency check and deterministically tests the documented last-return lifetime.


Link

[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).

Details and suggested fix

CMAKE_CXX_FLAGS applies -Wno-error=maybe-uninitialized throughout both configured C++ builds. Since 03_test_script.sh enables warnings-as-errors, a genuine warning in validation or consensus code will now appear in the output without failing either job. The inline comments and commit message attribute the workaround to the new optional logger fixtures, and test_kernel is a separate target. Scope the downgrade to that target instead of adding a global exception. The existing armhf workaround follows the same broad pattern, but a target-local exception here would preserve warning enforcement elsewhere without changing the callback tests.


Coverage details

Earlier concerns

Earlier reports raised these, with the titles and severities shown there. This review neither republished nor resolved them.