3 minor findings · 3 suggestions
Findings
[minor] · Connection pruning can destroy user data under the signal mutex
Concurrent connection creation and destruction can run an existing subscriber's destroy callback under the signal mutex, hanging if it re-enters logging or registration.
Location: src/kernel/bitcoinkernel.cpp:519 (head).
Details and suggested fix
connect() calls connected() while holding m_mutex, and that check temporarily owns the existing callable through a shared_ptr. If another thread disconnects that connection after the load, the temporary can become the last owner. Releasing it inside the pruning predicate then destroys the lambda's captured data and runs user_data_destroy_callback under the mutex. The extra reference here protects only the connection being created. A destroy callback that creates a connection tries to acquire the same non-recursive mutex; logging an accepted entry does so too when another subscriber remains registered, even at the default Info level. Retain the callback references used by liveness checks until after unlocking, including in empty(), and add a concurrent connect/disconnect test with a logging destroy callback. The race window is narrow, and no current consumer was found that combines concurrent connection creation/destruction with a re-entrant destroy callback, so production impact is unconfirmed.
[minor] · Registration-log allocation failure leaks a live logging connection
With Debug or Trace enabled, allocation failure in "Logger connected." can make creation return null while leaving its callback registered and its user data owned by an unreachable connection.
Location: src/kernel/bitcoinkernel.cpp:955 (head).
Details and suggested fix
btck_LoggingConnection::create() releases its owning unique_ptr before returning the raw pointer held across LogDebug. Dispatching that diagnostic allocates a connection-vector snapshot outside the per-callback exception handler. If allocation fails, the outer catch returns nullptr without deleting the connection. Its callback remains registered, its count stays incremented, and the supplied user-data destroy callback never runs. In the C++ wrapper, the orphaned T can also retain references to caller-scoped state that expires after construction fails, making later delivery access expired state. Keep a direct RAII owner for KernelLogger::Connection until the diagnostic succeeds, then release it into the returned handle. This preserves delivery of the creation message while restoring cleanup on failure. This path requires allocation failure with Debug or Trace enabled; no current caller triggering it was identified, so production impact is unconfirmed.
[minor] · Callback contract permits unsafe same-thread kernel re-entry
Removing the logging mutex does not make all kernel calls safe from callbacks: block processing and Debug-level LevelDB logging can invoke them while the same thread holds non-recursive locks.
Location: src/kernel/bitcoinkernel.h:411 (head).
Details and suggested fix
ActivateBestChain holds m_chainstate_mutex across ConnectTip and UpdateTip's Info log. A callback that calls btck_chainstate_manager_process_block on the same manager can reach ActivateBestChain again and attempt to acquire that non-recursive mutex. With Debug or Trace delivery enabled, LevelDB also forwards compaction logs synchronously while holding DBImpl::mutex_; callback access to the same database, or destruction of its chainstate manager, can attempt the same lock or wait for its own background work. These restrictions predate this PR, but the previous contract prohibited other kernel API calls. The new warning about waiting for another thread does not cover same-thread re-entry. Restrict "No lock is held" to the logging dispatch mutex and explicitly document the unsafe operations, including same-manager block processing and database access or manager destruction during Debug/Trace LevelDB callbacks.
Optional improvements
[suggestion] · Self-destruction docs omit Debug-level recursive delivery
At Debug or Trace level, destroying a connection from its own callback re-invokes that callback before disconnection, so an unguarded self-destroy action can recurse indefinitely.
Location: src/kernel/bitcoinkernel.h:962 (head).
Details and suggested fix
btck_logging_connection_destroy emits "Logger disconnecting." before deleting the connection, so the retiring callback still receives that entry synchronously. If it destroys its own handle again, each nested call logs before reaching disconnection. The same action works at Info because the diagnostic is filtered out. The new self-destruction test also guards its action to the first invocation, so it does not establish unguarded Debug-level behavior. If retaining delivery of this diagnostic to the retiring connection, as logging_connection_tests expects, document here that destroy can re-invoke the callback and that a self-destroy action must disarm itself first. Add a Debug-level self-destruction test to make that contract explicit.
[suggestion] · Thread-safety wording omits same-thread callback re-entry
A callback protected by a non-recursive mutex can still deadlock when work inside it logs and synchronously re-enters that callback.
Location: src/kernel/bitcoinkernel_wrapper.h:1060 (head).
Details and suggested fix
MaybeLog emits synchronously, and the signal directly invokes callbacks without a recursion guard. When a callback, a function it calls, or a finalizer triggered inside it logs, an accepted nested entry can reach that same callback before its outer invocation returns. logging_callback_reentry_tests deliberately asserts this behavior. Document same-thread re-entry explicitly in both the C callback documentation and the Log concept, and warn against holding non-recursive locks across work that can log. A callback that logs on every invocation also needs to prevent unbounded recursion. For Logger<T>, also state that T may be destroyed on the last invocation's thread after the handle is gone, so any referenced external state must outlive the callback object, not merely the handle.
[suggestion] · Concurrency test misses the lifetime check between callback returns
Releasing both invocations together leaves premature user-data destruction after the first return unchecked.
Location: src/test/kernel/test_kernel.cpp:940 (head).
Details and suggested fix
The test checks !destroyed before either invocation is released, then checks destroyed only after both logging threads have joined. Both invocations wait on the same flag, and BlockingLog::LogMessage does not deliberately access object state after that wait. An implementation that destroys user data after the first completed invocation can therefore satisfy both lifetime assertions. Give the invocations separate release gates, release one, wait for its logging thread to finish, and assert !destroyed while the other remains blocked. Then release the second, join it, and assert destroyed. This checks the stated last-invocation lifetime property deterministically.