Skip to content

test: Fix TestFlushBehavior when coin has non-zero dynamic memory usage - #33381

Closed
PiRK wants to merge 1 commit into
bitcoin:masterfrom
PiRK:fix_TestFlushBehavior_when_nonzero_DynamicMemoryUsage
Closed

test: Fix TestFlushBehavior when coin has non-zero dynamic memory usage#33381
PiRK wants to merge 1 commit into
bitcoin:masterfrom
PiRK:fix_TestFlushBehavior_when_nonzero_DynamicMemoryUsage

Conversation

@PiRK

@PiRK PiRK commented Sep 12, 2025

Copy link
Copy Markdown
Contributor

If the random coin in this test has a non-null scriptpubkey, cache->SanityCheck(); fails an assertion because the BOOST_CHECK_THROW line leaves CCoinsViewCache::cachedCoinsUsage in a corrupted state: the erroring AddCoin call decrements the value to 0 even though the coin is still in the cache, then the next SpendCoin call decrements the value again causing a size_t underflow.

Move the BOOST_CHECK_THROW test to its own suite, so that other flush tests are not affected by the caches's inconsistent state

@DrahtBot DrahtBot added the Tests label Sep 12, 2025
@DrahtBot

DrahtBot commented Sep 12, 2025

Copy link
Copy Markdown
Contributor

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

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/33381.

Reviews

See the guideline for information on the review process.
A summary of reviews will appear here.

@PiRK
PiRK force-pushed the fix_TestFlushBehavior_when_nonzero_DynamicMemoryUsage branch from e4aea00 to 32bb174 Compare September 12, 2025 19:59
@PiRK

PiRK commented Sep 12, 2025

Copy link
Copy Markdown
Contributor Author

I force pushed to fix a use-after-move bug

@achow101

Copy link
Copy Markdown
Member

This change breaks the test.

@PiRK
PiRK force-pushed the fix_TestFlushBehavior_when_nonzero_DynamicMemoryUsage branch from 32bb174 to 21aee81 Compare September 19, 2025 16:19
@PiRK

PiRK commented Sep 19, 2025

Copy link
Copy Markdown
Contributor Author

let's see if this works. I moved the problematic test line to its own test suite, where it does not affect other flush tests.

The failure I'm trying to solve seems intermittent. It happens about 75% of the time on master if I just add the scriptPubKey to MakeCoin.
After my commit I get no failure in 10 runs of ./build/bin/test_bitcoin --run_test=coins_tests

@PiRK

PiRK commented Sep 19, 2025

Copy link
Copy Markdown
Contributor Author

To clarify the rationale for this PR: the SanityCheck test added to flush_all in a14edad does not have full coverage if cachedCoinsUsage is not exercised.

This PR ensure that the cachedCoinsUsage part of SanityCheck now actually does something useful for the TestFlushBehavior suite.

@@ -56,10 +56,9 @@ if [ -n "$PIP_PACKAGES" ]; then
fi

if [[ -n "${USE_INSTRUMENTED_LIBCPP}" ]]; then
${CI_RETRY_EXE} git clone --depth=1 https://github.com/llvm/llvm-project -b "llvmorg-21.1.1" /llvm-project

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand how this related to the PR. Is this really needed here or can it be split out to a different PR?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this commit was pulled into my PR the last time i rebased on master. This was the top commit from master that had just been merged (#33364) at the time

Some kind of github glitch?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

… memory usage

If the random coin in this test has a non-null scriptpubkey, `cache->SanityCheck();` fails an assertion because the `BOOST_CHECK_THROW` line leaves `CCoinsViewCache::cachedCoinsUsage` in a corrupted state: the erroring `AddCoin` call decrements the value to 0 even though the coin is still in the cache, then the next `SpendCoin` call decrements the value again causing a `size_t` underflow.
```
$  ./build/bin/test_bitcoin  --run_test=coins_tests
Running 8 test cases...
test_bitcoin: ./coins.cpp:345: void CCoinsViewCache::SanityCheck() const: Assertion `recomputed_usage == cachedCoinsUsage' failed.
unknown location(0): fatal error: in "coins_tests/ccoins_flush_behavior": signal: SIGABRT (application abort requested)
./test/coins_tests.cpp(987): last checkpoint
test_bitcoin: ./common/args.cpp:578: void ArgsManager::AddArg(const std::string&, const std::string&, unsigned int, const OptionsCategory&): Assertion `ret.second' failed.
unknown location(0): fatal error: in "coins_tests/coins_resource_is_used": signal: SIGABRT (application abort requested)
./test/coins_tests.cpp(1064): last checkpoint: "coins_resource_is_used" fixture ctor
```

Move the `BOOST_CHECK_THROW` line to its own suite so it does not corrupt the coins view for further flush tests. .
@PiRK
PiRK force-pushed the fix_TestFlushBehavior_when_nonzero_DynamicMemoryUsage branch from 21aee81 to c9ad7e5 Compare September 24, 2025 06:50
@PiRK

PiRK commented Sep 24, 2025

Copy link
Copy Markdown
Contributor Author

Rebased, the phantom commit is gone now

@l0rinc

l0rinc commented Oct 20, 2025

Copy link
Copy Markdown
Contributor

@PiRK, can you please check if this was already fixed by #32313?

@PiRK

PiRK commented Oct 20, 2025

Copy link
Copy Markdown
Contributor Author

@l0rinc Yes, #32313 fixes it.

Tested with this patch:

diff --git a/src/test/coins_tests.cpp b/src/test/coins_tests.cpp
index a1152d245f..a3ee441c42 100644
--- a/src/test/coins_tests.cpp
+++ b/src/test/coins_tests.cpp
@@ -880,6 +880,7 @@ Coin MakeCoin()
 {
     Coin coin;
     coin.out.nValue = m_rng.rand32();
+    coin.out.scriptPubKey.assign(m_rng.randbits(6), 0);
     coin.nHeight = m_rng.randrange(4096);
     coin.fCoinBase = 0;
     return coin;

coins_tests fails as expected on the commit just before #32313 was merged

./test/coins_tests.cpp(1050): Entering test case "ccoins_flush_behavior"
test_bitcoin: ./coins.cpp:345: void CCoinsViewCache::SanityCheck() const: Assertion `recomputed_usage == cachedCoinsUsage' failed.

And the test now passes on master.

Not sure if setting the scriptPubkey in ccoins_flush_behavior now adds any useful coverage after you've added a dedicated unit test and extra assertions in the other PR. I'll let you judge, and close this PR accordingly.

@PiRK

PiRK commented Oct 20, 2025

Copy link
Copy Markdown
Contributor Author

AFAICT the issue my patch raised was only reachable after suppressing a std::logic_error, which shouldn't happen in production code.

@l0rinc

l0rinc commented Oct 20, 2025

Copy link
Copy Markdown
Contributor

Was a good find, thanks, I agree that it doesn't make a lot of sense anymore to keep this around, if you agree, please consider closing the PR

@PiRK PiRK closed this Oct 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants