1 minor finding · 5 suggestions
Findings
[minor] · New connections receive entries already being delivered
Creating and retaining a logging connection inside an INFO callback delivers the original entry to the new subscriber, contrary to the documented subscription boundary.
Location: src/kernel/bitcoinkernel.cpp:621 (head).
Details and suggested fix
At the default INFO level, callback A can receive entry E, create and retain connection B, and return. Registration appends B to the live callback list, and its DEBUG “Logger connected.” message is filtered out. The outer Log() traversal then increments its iterator and delivers E to B, although E predates B's creation. The base held the mutex throughout delivery, preventing this mutation. Give registrations a monotonically increasing sequence and capture a cutoff under Log()'s first lock, excluding later registrations while retaining the removed/in_flight handling. Do not mark every pending callback in-flight up front, since that would prevent A from destroying another connection. Add a test that B misses E but receives the next entry. No in-tree caller creates a connection during delivery, so production impact is unconfirmed.
Optional improvements
[suggestion] · Callback permissions include non-reentrant block processing
A callback handling an active-chain UpdateTip INFO entry can re-enter process_block on the same manager and re-lock a non-recursive mutex, despite the header's permission to call kernel APIs.
Location: src/kernel/bitcoinkernel.h:416 (head).
Details and suggested fix
Removing the logger mutex does not make validation re-entrant. ActivateBestChain holds m_chainstate_mutex through ConnectTip and the UpdateTip INFO callback. Calling btck_chainstate_manager_process_block on that manager reaches ActivateBestChain again even for a valid, already-stored block, because AcceptBlock returns success and ProcessNewBlock still activates the chain. Without DEBUG_LOCKORDER, this re-locks a std::mutex already held by the same thread, which is undefined behavior and can hang. With DEBUG_LOCKORDER, ProcessNewBlock aborts earlier on its cs_main assertion. Neither the destruction restriction nor the warning about waiting for another thread excludes this same-thread call. Narrow the callback contract to forbid re-entering block processing and mutating the originating object, while preserving recursive logging and the intended inactive-object-finalizer case. Only this activation path is established here; an exhaustive safe-function list needs a broader lock audit.
[suggestion] · Connection destruction omits the validation-callback lock restriction
A validation callback holding cs_main can deadlock while destroying a connection whose concurrent log callback queries chain height; the destruction documentation does not warn about this interaction.
Location: src/kernel/bitcoinkernel.h:971 (head).
Details and suggested fix
BlockConnected runs inline through ImmediateTaskRunner while ActivateBestChain holds cs_main. If thread T destroys logging connection A there, destruction waits for A's running invocations. Meanwhile, thread U can be inside A's log callback and call btck_chain_get_height, which waits for cs_main. T then waits for U, and U waits for T. The new log-callback permission allows the height query; the base contract prohibited it. The warning against cross-thread waits covers log callbacks, but not the validation callback initiating this destruction. Document here and in the validation-callback contract that destruction must occur outside lock-holding kernel callbacks when the connection's callback may call kernel APIs. Preserve synchronous user-data destruction rather than replacing it with deferred destruction without an API discussion.
[suggestion] · Nested outer-connection destruction bypasses the fail-fast check
A nested callback that destroys an outer callback's connection waits for its own thread indefinitely, although direct self-destruction aborts; both operations are already documented as unsupported.
Location: src/kernel/bitcoinkernel.cpp:593 (head).
Details and suggested fix
t_running records only the innermost invocation. If A logs recursively and nested callback B destroys A's connection, the assertion passes because t_running points to B. UnregisterCallback then waits for A's in_flight count, which cannot reach zero until B returns and the outer A invocation finishes. Extend the diagnostic to every callback active on this thread by linking stack-local invocation nodes into a thread-local chain and checking that chain before waiting. This adds a previous-node pointer per nesting level and preserves synchronous destruction and waits for other-thread invocations. It turns documented misuse into an abort like the direct case, rather than changing supported behavior. Whether a language binding can garbage-collect a connection wrapper while its callback is active remains unconfirmed.
[suggestion] · Concurrency test can miss the unregister lifetime boundary
The 100 ms sleep and shared release gate let the lifetime assertions pass on schedules that never exercise unregistration with callbacks still running.
Location: src/test/kernel/test_kernel.cpp:850 (head).
Details and suggested fix
The test proves that both BlockingLog invocations have entered, but sleeping does not prove that logger.reset() has started. If the destroyer runs only after both logging threads finish, both destruction assertions can pass even with the in_flight wait removed. Releasing both invocations together also omits the state where one has returned and the other remains blocked. Strengthen the test with independent release gates and a bounded, nonblocking logging probe that observes removal before checking !destroyed. Let additional probe invocations return immediately. Release and join one logging thread, check !destroyed again, then release the second and check completion. Record the callback-exit count when BlockingLog is destroyed and assert that both invocations had exited, so the test checks the lifetime invariant directly.
[suggestion] · One cout insertion does not guarantee whole-line output
Concurrent demo and test log callbacks can still produce interleaved characters under the standard's guarantees, despite the new comments promising that lines will not mix.
Location: src/bitcoin-chainstate.cpp:57 (head).
Details and suggested fix
KernelLog::LogMessage and TestLog::LogMessage now format locally and issue one std::cout << line.str(), but a single insertion is not portably indivisible. Synchronized standard streams guarantee freedom from data races, not whole-record output. Common implementations typically serialize this insertion, so this is a portability limitation rather than observed mixed output. Protect the final write with a function-local static mutex in each writer to serialize lines from those callbacks. Scope the comment to those participating writers, since unrelated direct std::cout writes in the demo and tests remain unsynchronized.