Repository navigation
Conversation
GCC 15 at -O1 reports a false -Wmaybe-uninitialized for a std::optional<btck::Logger<...>> in test_kernel.cpp, in the destructor of a logger that is always emplaced before use. Don't fail the build on it, as the armhf task already does. This change was written with Claude Opus 5.5.
Locking a sync.h Mutex can log: with DEBUG_LOCKCONTENTION it logs lock contention, and with DEBUG_LOCKORDER it logs lock order errors. A signal that delivers kernel log entries, as the next commit adds, would then lock its own mutex again on the same thread while delivering that entry, and deadlock. Use a StdMutex, as Logger in logging.h does for the same reason. The mutex only guards the connection list and is never held while callbacks run, so lock order checking adds little here. This change was written with Claude Opus 5.5.
…e logger mutex KernelLogger called log callbacks while holding its mutex, so a callback that logged, or destroyed a kernel object that logs, deadlocked. A garbage collector running finalizers inside a callback can trigger this (bitcoin#34775 (comment)). Deliver entries through a btcsignals::signal instead, which holds no lock while callbacks run. The API is unchanged. Behavior changes: - Callbacks can run on several threads at the same time, so they must be thread-safe. - Callbacks may call kernel functions, including creating and destroying logging connections, their own included. - Destroying a connection no longer waits for invocations already running on other threads. The user data is destroyed when the last one returns, as with other btcsignals users. This is the follow-up suggested in bitcoin#34374 (comment). This change was written with Claude Opus 5.5.
|
Updated 7a5e5b0 -> 25eff90 ( |
|
Approach ACK |
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, 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. |
ryanofsky
left a comment
There was a problem hiding this comment.
Updated 25eff90 -> a9f467e (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 unit
nervana21
left a comment
There was a problem hiding this comment.
I've tested and reviewed the code and it lgtm. I've left a suggestion to improve a unit test
| BOOST_CHECK(!destroyed); | ||
|
|
||
| released = true; | ||
| released.notify_all(); |
There was a problem hiding this comment.
a9f467e: kernel: Dispatch log callbacks with btcsignals so they run without the logger mutex
This 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
diff --git a/src/test/kernel/test_kernel.cpp b/src/test/kernel/test_kernel.cpp
--- a/src/test/kernel/test_kernel.cpp
+++ b/src/test/kernel/test_kernel.cpp
@@ -896,24 +896,32 @@ BOOST_AUTO_TEST_CASE(logging_callback_destroy_outer_tests)
BOOST_CHECK_EQUAL(inner_entries, 2);
}
-//! Counts invocations of LogMessage(), and blocks each one until released, so that a test controls
-//! when invocations return.
+//! Counts invocations of LogMessage(), and blocks each one on its own release gate, so that a test
+//! can let them return one at a time.
class BlockingLog
{
std::atomic<int>& m_entered;
- std::atomic<bool>& m_released;
+ std::atomic<bool>& m_released_a;
+ std::atomic<bool>& m_released_b;
+ std::atomic<int>& m_returned;
std::atomic<bool>& m_destroyed;
+ std::atomic<int> m_next{0};
public:
- BlockingLog(std::atomic<int>& entered, std::atomic<bool>& released, std::atomic<bool>& destroyed)
- : m_entered{entered}, m_released{released}, m_destroyed{destroyed} {}
+ BlockingLog(std::atomic<int>& entered, std::atomic<bool>& released_a, std::atomic<bool>& released_b,
+ std::atomic<int>& returned, std::atomic<bool>& destroyed)
+ : m_entered{entered}, m_released_a{released_a}, m_released_b{released_b}, m_returned{returned},
+ m_destroyed{destroyed} {}
~BlockingLog() { m_destroyed = true; }
void LogMessage(const LogEntry&)
{
+ const int slot{m_next.fetch_add(1)};
++m_entered;
m_entered.notify_all();
- m_released.wait(false);
+ (slot == 0 ? m_released_a : m_released_b).wait(false);
+ ++m_returned;
+ m_returned.notify_all();
}
};
@@ -922,10 +930,12 @@ public:
BOOST_AUTO_TEST_CASE(logging_callback_concurrency_tests)
{
std::atomic<int> entered{0};
- std::atomic<bool> released{false};
+ std::atomic<bool> released_a{false};
+ std::atomic<bool> released_b{false};
+ std::atomic<int> returned{0};
std::atomic<bool> destroyed{false};
std::optional<Logger<BlockingLog>> logger;
- logger.emplace(std::make_unique<BlockingLog>(entered, released, destroyed));
+ logger.emplace(std::make_unique<BlockingLog>(entered, released_a, released_b, returned, destroyed));
const auto log_info{[] { ChainParams params{hex_string_to_byte_vec("51")}; }};
std::thread log_1{log_info};
@@ -937,8 +947,15 @@ BOOST_AUTO_TEST_CASE(logging_callback_concurrency_tests)
logger.reset();
BOOST_CHECK(!destroyed);
- released = true;
- released.notify_all();
+ released_a = true;
+ released_a.notify_all();
+ for (int n; (n = returned) < 1;) {
+ returned.wait(n);
+ }
+ BOOST_CHECK(!destroyed);
+
+ released_b = true;
+ released_b.notify_all();
log_1.join();
log_2.join();
BOOST_CHECK(destroyed);
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. 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.