Repository navigation
Conversation
Release KernelLogger::m_mutex while a log callback runs, so a callback can log, or destroy kernel objects that log, without deadlocking. Previously Log() held m_mutex across every callback, so an entry logged from inside a callback waited for the mutex its own thread held. A garbage collector running finalizers inside a callback triggers this: a finalizer that destroys a chainstate manager flushes and logs, and the process hangs. See bitcoin#34775 (comment) and bitcoin#34374 (comment). Creating or destroying another connection from inside a callback hung the same way. Multiple connections and the API are unchanged. Log() walks the list of callbacks holding m_mutex only to step to the next one, and each callback has a count of running invocations. Destroying a connection marks its callback removed so no new invocations start, waits for the count to reach zero, and then destroys the callback and its user_data outside m_mutex, so it still waits for invocations on other threads. The count is only decremented and notified under m_mutex, and destroy takes m_mutex again before freeing the callback, so Log() never touches a freed callback. Destroying a connection from inside its own callback would wait for itself, so it asserts instead. Behavior change: callbacks can run on several threads at the same time and must be thread-safe, and entries from different threads have no global order. The header documents this and which kernel calls are safe from inside a callback. bitcoin-chainstate and the test logger now write each line in one call so concurrent lines don't interleave. New test_kernel cases, which all hang without this change: a callback that logs and destroys a chainstate manager and context, a callback that destroys another connection, and destroying a connection while invocations run on other threads. This change was written with Claude Opus 5.5.
Contributor
Author
|
Closing in favor of #36768, which fixes the same deadlock by delivering log entries through Both PRs keep the current API, including multiple connections, and run callbacks without holding the logger mutex, so a callback can log and destroy kernel objects. Both have tests showing it no longer hangs. The difference is what destroying a connection does while its callback is running on another thread:
#36768 also needs less kernel code ( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 (#34775 (comment), #34374 (comment)). 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.
Solution: This PR calls log callbacks without holding the mutex. The kernel API is unchanged, including support for multiple logging connections.
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. Fixing the deadlock separately lets it merge without waiting on that API question. If #34775 is going to merge soon with its own fix, this PR can be closed.
Behavior changes for callers:
The change is in
src/kernel/bitcoinkernel.cpp(about 45 lines) plus header docs and new test_kernel cases, which hang without the fix. Details are in the commit message.