test: Misc fixes for pending validation events races #36447

Manually merged
maflcko merged 3 commits from 2610-test-more-deterministic-TestChain100Setup into master 2026-10-07 12:56:49 +00:00 AGit
maflcko commented 2026-10-06 16:01:43 +00:00 (Migrated from github.com)

TestChain100Setup enqueues 100 validation events from blocks. Unit tests using that setup and their own validation events may run into non-determinism and even intermittent test failures.

Fix all issues by draining the pending validation events in the ctor.

This can be tested with a diff like:

diff --git a/src/validationinterface.cpp b/src/validationinterface.cpp
index 128f14a6d5..2ea3ec8d4c 100644
--- a/src/validationinterface.cpp
+++ b/src/validationinterface.cpp
@@ -14,2 +14,3 @@
 #include <primitives/transaction.h>
+#include <random.h>
 #include <util/check.h>
@@ -156,2 +157,4 @@ void ValidationSignals::SyncWithValidationInterfaceQueue()
 
+static FastRandomContext g_rnd{};
+
 // Use a macro instead of a function for conditional logging to prevent
@@ -168,2 +171,3 @@ void ValidationSignals::SyncWithValidationInterfaceQueue()
             LOG_EVENT("%s", local_log_msg);                                                                      \
+            UninterruptibleSleep(1ms * g_rnd.randrange(105));   \
             local_event();                                                                                       \

and then running the test scan_for_wallet_transactions_attach_chain a few times.

Or master (or the first commit), it should fail with: error: scan_for_wallet_transactions_attach_chain": check wallet->mapWallet.size() == static_cast<size_t>(NEW_BLOCKS + 1) has failed [7 != 6].

(Also included is a commit to move-only the many rescan tests to a separate file)

(Also included is a commit to fix unsafe index shutdowns in tests)

`TestChain100Setup` enqueues 100 validation events from blocks. Unit tests using that setup and their own validation events may run into non-determinism and even intermittent test failures. Fix all issues by draining the pending validation events in the ctor. This can be tested with a diff like: ```diff diff --git a/src/validationinterface.cpp b/src/validationinterface.cpp index 128f14a6d5..2ea3ec8d4c 100644 --- a/src/validationinterface.cpp +++ b/src/validationinterface.cpp @@ -14,2 +14,3 @@ #include <primitives/transaction.h> +#include <random.h> #include <util/check.h> @@ -156,2 +157,4 @@ void ValidationSignals::SyncWithValidationInterfaceQueue() +static FastRandomContext g_rnd{}; + // Use a macro instead of a function for conditional logging to prevent @@ -168,2 +171,3 @@ void ValidationSignals::SyncWithValidationInterfaceQueue() LOG_EVENT("%s", local_log_msg); \ + UninterruptibleSleep(1ms * g_rnd.randrange(105)); \ local_event(); \ ``` and then running the test `scan_for_wallet_transactions_attach_chain` a few times. Or master (or the first commit), it should fail with: `error: scan_for_wallet_transactions_attach_chain": check wallet->mapWallet.size() == static_cast<size_t>(NEW_BLOCKS + 1) has failed [7 != 6]`. (Also included is a commit to move-only the many rescan tests to a separate file) (Also included is a commit to fix unsafe index shutdowns in tests)
DrahtBot commented 2026-10-06 16:01:55 +00:00 (Migrated from github.com)

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK sedited, willcl-ark
Concept ACK StephenChi-hi
Stale ACK ismaelsadeeq

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36444 (wallet: Store information about created and imported multipath descriptors by achow101)
  • #36443 (descriptor: Reconstruct a multipath descriptor from its expansion by achow101)
  • #36442 (descriptor: Represent multipath descriptors in a single Descriptor object by achow101)
  • #36337 (fuzz: Cover block filter P2P messages by maflcko)
  • #35474 (node: move index ownership to NodeContext by w0xlt)
  • #24230 (indexes: Stop using node internal types and locking cs_main, improve sync logic by ryanofsky)

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.

LLM Linter (✨ experimental)

Possible typos and grammar issues:

  • src/wallet/test/rescan_tests.cpp: the one of the recorded best block → the coinbase of the recorded best block [the original phrasing is unclear]

2026-10-07 10:42:25

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36447 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36447.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36447#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [sedited](https://github.com/bitcoin/bitcoin/pull/36447#pullrequestreview-5441437290), [willcl-ark](https://github.com/bitcoin/bitcoin/pull/36447#issuecomment-6036942281) | | Concept ACK | [StephenChi-hi](https://github.com/bitcoin/bitcoin/pull/36447#pullrequestreview-5441985558) | | Stale ACK | [ismaelsadeeq](https://github.com/bitcoin/bitcoin/pull/36447#pullrequestreview-5439436476) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36444](https://github.com/bitcoin/bitcoin/pull/36444) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36444.svg"></sub> (wallet: Store information about created and imported multipath descriptors by achow101) * [#36443](https://github.com/bitcoin/bitcoin/pull/36443) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36443.svg"></sub> (descriptor: Reconstruct a multipath descriptor from its expansion by achow101) * [#36442](https://github.com/bitcoin/bitcoin/pull/36442) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36442.svg"></sub> (descriptor: Represent multipath descriptors in a single Descriptor object by achow101) * [#36337](https://github.com/bitcoin/bitcoin/pull/36337) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36337.svg"></sub> (fuzz: Cover block filter P2P messages by maflcko) * [#35474](https://github.com/bitcoin/bitcoin/pull/35474) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35474.svg"></sub> (node: move index ownership to NodeContext by w0xlt) * [#24230](https://github.com/bitcoin/bitcoin/pull/24230) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/24230.svg"></sub> (indexes: Stop using node internal types and locking cs_main, improve sync logic by ryanofsky) 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. <!--5faf32d7da4f0f540f40219e4f7537a3--> ### LLM Linter (✨ experimental) Possible typos and grammar issues: - `src/wallet/test/rescan_tests.cpp`: `the one of the recorded best block` → `the coinbase of the recorded best block` [the original phrasing is unclear] <sup>2026-10-07 10:42:25</sup>
First-time contributor

Base: 9523d2b4a185c704994ef44d2229c3d1604cb9e5
Head: fa466dea808dedfc281cdff842cbefafae574d8a

No verified findings are available from this partial review.

Review coverage was incomplete. The archaeology review had incomplete evidence. The adversarial review did not complete. The adversarial_glm review did not complete. The design review had incomplete evidence.

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> Base: `9523d2b4a185c704994ef44d2229c3d1604cb9e5` Head: `fa466dea808dedfc281cdff842cbefafae574d8a` No verified findings are available from this partial review. Review coverage was incomplete. The archaeology review had incomplete evidence. The adversarial review did not complete. The adversarial_glm review did not complete. The design review had incomplete evidence. [Full review report](<https://review.fish.foo/traces/36447-fa466dea808dedfc281cdff842cbefafae574d8a-522-6.html>)
sedited (Migrated from github.com) approved these changes 2026-10-06 18:41:06 +00:00
sedited (Migrated from github.com) left a comment
ACK 9999557b77224be0f6d079af53fa90042bfd01b9
willcl-ark (Migrated from github.com) approved these changes 2026-10-06 19:47:56 +00:00
willcl-ark (Migrated from github.com) left a comment

ACK 9999557b77

Nice!

ACK 9999557b77224be0f6d079af53fa90042bfd01b9 Nice!
maflcko commented 2026-10-06 20:05:32 +00:00 (Migrated from github.com)

Hmm, looks like Ralph Review found another race. To repo:

diff --git a/src/wallet/test/rescan_tests.cpp b/src/wallet/test/rescan_tests.cpp
index 4c5cf76ced..d838e1fc70 100644
--- a/src/wallet/test/rescan_tests.cpp
+++ b/src/wallet/test/rescan_tests.cpp
@@ -29,2 +29,3 @@
 #include <string>
+#include <thread>
 #include <utility>
@@ -567,4 +568,12 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions_attach_chain, TestChain100S
     constexpr int NEW_BLOCKS{5};
+    const auto delayed_blocks_start = std::chrono::steady_clock::now();
     for (int i = 0; i < NEW_BLOCKS; ++i) {
+        if (i >= 3) {
+            m_node.validation_signals->CallFunctionInValidationInterfaceQueue(
+                [] { std::this_thread::sleep_for(std::chrono::milliseconds{500}); });
+        }
         CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
+        if (i < 3) {
+            m_node.validation_signals->SyncWithValidationInterfaceQueue();
+        }
     }
@@ -582,4 +591,9 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions_attach_chain, TestChain100S
     wallet = TestLoadWallet(context);
+    BOOST_TEST_MESSAGE("Wallet load returned " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - delayed_blocks_start).count() << " ms after starting delayed blocks");
+
+    // Allow some delayed callbacks to run before checking the wallet tip.
+    std::this_thread::sleep_for(std::chrono::milliseconds{500});
     {
         LOCK(wallet->cs_wallet);
+        BOOST_TEST_MESSAGE("Assertions at " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - delayed_blocks_start).count() << " ms, wallet height " << wallet->GetLastBlockHeight());
         BOOST_CHECK_EQUAL(wallet->GetLastBlockHeight(), tip_height);

Let me push a fix for that as well ...

Hmm, looks like Ralph Review found another race. To repo: ```diff diff --git a/src/wallet/test/rescan_tests.cpp b/src/wallet/test/rescan_tests.cpp index 4c5cf76ced..d838e1fc70 100644 --- a/src/wallet/test/rescan_tests.cpp +++ b/src/wallet/test/rescan_tests.cpp @@ -29,2 +29,3 @@ #include <string> +#include <thread> #include <utility> @@ -567,4 +568,12 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions_attach_chain, TestChain100S constexpr int NEW_BLOCKS{5}; + const auto delayed_blocks_start = std::chrono::steady_clock::now(); for (int i = 0; i < NEW_BLOCKS; ++i) { + if (i >= 3) { + m_node.validation_signals->CallFunctionInValidationInterfaceQueue( + [] { std::this_thread::sleep_for(std::chrono::milliseconds{500}); }); + } CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())); + if (i < 3) { + m_node.validation_signals->SyncWithValidationInterfaceQueue(); + } } @@ -582,4 +591,9 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions_attach_chain, TestChain100S wallet = TestLoadWallet(context); + BOOST_TEST_MESSAGE("Wallet load returned " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - delayed_blocks_start).count() << " ms after starting delayed blocks"); + + // Allow some delayed callbacks to run before checking the wallet tip. + std::this_thread::sleep_for(std::chrono::milliseconds{500}); { LOCK(wallet->cs_wallet); + BOOST_TEST_MESSAGE("Assertions at " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - delayed_blocks_start).count() << " ms, wallet height " << wallet->GetLastBlockHeight()); BOOST_CHECK_EQUAL(wallet->GetLastBlockHeight(), tip_height); ``` Let me push a fix for that as well ...
maflcko commented 2026-10-06 20:34:55 +00:00 (Migrated from github.com)

ok, now the CI is failing because there is another UB race :(

Let's fix that here as well, I guess ...

ok, now the CI is failing because there is another UB race :( Let's fix that here as well, I guess ...
DrahtBot commented 2026-10-06 20:55:57 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task TSan: https://github.com/bitcoin/bitcoin/actions/runs/37524113100/job/112476629653
LLM reason (✨ experimental): CI failed because ThreadSanitizer detected a data race on a vptr in BaseIndex::~BaseIndex() during coinstatsindex_tests.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `TSan`: https://github.com/bitcoin/bitcoin/actions/runs/37524113100/job/112476629653</sub> <sub>LLM reason (✨ experimental): CI failed because ThreadSanitizer detected a data race on a vptr in `BaseIndex::~BaseIndex()` during `coinstatsindex_tests`.</sub> <details><summary>Hints</summary> Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example: * Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch. * A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test. * An intermittent issue. Leave a comment here, if you need help tracking down a confusing failure. </details>
ismaelsadeeq (Migrated from github.com) approved these changes 2026-10-07 05:41:06 +00:00
ismaelsadeeq (Migrated from github.com) left a comment

Code review ACK fa77f27f98

Minor comments below

Code review ACK fa77f27f98211167d9dfeba8933e6866df5fce8b Minor comments below
ismaelsadeeq (Migrated from github.com) commented 2026-10-07 05:38:42 +00:00

Perhaps it may be better to just drain in mineBlock? That way other callers that will mine a block get the notification emitted for free, without them having to drain themselves?

Perhaps it may be better to just drain in mineBlock? That way other callers that will mine a block get the notification emitted for free, without them having to drain themselves?
ismaelsadeeq (Migrated from github.com) commented 2026-10-07 05:40:08 +00:00

Same comment here: can we move the drain to CreateAndProcessBlock?

Same comment here: can we move the drain to CreateAndProcessBlock?
maflcko (Migrated from github.com) reviewed 2026-10-07 06:37:41 +00:00
maflcko (Migrated from github.com) commented 2026-10-07 06:37:41 +00:00

I was thinking that it could be useful coverage for the unit tests to be async here. But then, I don't know if they have ever found an issue here. Of course, we still need the option to be async, but maybe it is fine to sync by default.

I'll push that ...

I was thinking that it could be useful coverage for the unit tests to be async here. But then, I don't know if they have ever found an issue here. Of course, we still need the option to be async, but maybe it is fine to sync by default. I'll push that ...
ismaelsadeeq (Migrated from github.com) approved these changes 2026-10-07 08:11:11 +00:00
ismaelsadeeq (Migrated from github.com) left a comment

reACK fa10ab5fdb

reACK fa10ab5fdbb8ca3fede08dd7c93c7359a5e25433
sedited (Migrated from github.com) reviewed 2026-10-07 10:04:07 +00:00
sedited (Migrated from github.com) commented 2026-10-07 10:04:08 +00:00

What about the other cases of txindex.Stop() in this file? I guess most don't produce more blocks, but the last test for example does, so maybe we should also sync there?

What about the other cases of `txindex.Stop()` in this file? I guess most don't produce more blocks, but the last test for example does, so maybe we should also sync there?
maflcko (Migrated from github.com) reviewed 2026-10-07 10:43:46 +00:00
maflcko (Migrated from github.com) commented 2026-10-07 10:43:46 +00:00

ah, I only grepped for the comment. Fixed the others as well now.

ah, I only grepped for the comment. Fixed the others as well now.
sedited (Migrated from github.com) approved these changes 2026-10-07 11:13:07 +00:00
sedited (Migrated from github.com) left a comment
ACK fa466dea808dedfc281cdff842cbefafae574d8a
willcl-ark commented 2026-10-07 11:27:25 +00:00 (Migrated from github.com)

reACK fa466dea80

reACK fa466dea808dedfc281cdff842cbefafae574d8a
StephenChi-hi (Migrated from github.com) reviewed 2026-10-07 12:03:52 +00:00
StephenChi-hi (Migrated from github.com) left a comment

Tested ACK

I agree, this will sure help to prevents flaky test failures.
built and verified changes, all 862 unit test cases in test_bitcoin passed cleanly.

Tested ACK I agree, this will sure help to prevents flaky test failures. built and verified changes, all 862 unit test cases in test_bitcoin passed cleanly.
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36447
No description provided.