Skip to content

test: fix intermittent failure in p2p_tx_download.py - #36891

Open
Crypt-iQ wants to merge 2 commits into
bitcoin:masterfrom
Crypt-iQ:2026-11/txdownload-flake
Open

Crypt-iQ wants to merge 2 commits into
bitcoin:masterfrom
Crypt-iQ:2026-11/txdownload-flake

Conversation

@Crypt-iQ

Copy link
Copy Markdown
Contributor

Because nodes[1] time was not mocked in test_block_inv, it was possible for wall clock to advance 2s by the time nodes[0] had delivered a tx announcement to it. Then, nodes[1] would request the tx from a test peer, never receive the tx, and fail in sync_mempools().

@DrahtBot DrahtBot added the Tests label Nov 16, 2026
@DrahtBot

DrahtBot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

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
Concept ACK Waridi318

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:

  • #36257 (qa: assert_equal -> assert_true/assert_false by hodlinator)

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.

@Crypt-iQ

Copy link
Copy Markdown
Contributor Author

It's possible to simulate nodes[1] not running with the following diff on 8130b3b:

diff --git a/test/functional/p2p_tx_download.py b/test/functional/p2p_tx_download.py
index aa43cc18e2..4a83e44b91 100755
--- a/test/functional/p2p_tx_download.py
+++ b/test/functional/p2p_tx_download.py
@@ -116,6 +116,8 @@ class TxDownloadTest(BitcoinTestFramework):
         self.log.info("Put the tx in node 0's mempool")
         self.nodes[0].sendrawtransaction(tx['hex'])
 
+        time.sleep(2)
+
         # Since node 1 is connected outbound to an honest peer (node 0), it
         # should get the tx within a timeout.
         # However, node 0 sees the same connection as inbound, so it may not

@Pranav-error

Copy link
Copy Markdown

I tested the master and PR versions using the same binary and Crypt-iQ's reproducer, adding a delay after sendrawtransaction.

On master, with a 2-second delay, 2/5 runs failed due to mempool sync timeouts. With the PR, both runs passed. Increasing the delay to 5 seconds made master pass, but runs took around 83 seconds instead of 26, while the PR remained around 26 seconds.

Looking at node1's debug log, master requests the wtx from peer 4, which never responds, and retries node0 only after 60 seconds. With the PR, it requests from peer 0 almost immediately after receiving the inventory. So this isn't just fixing a flaky test; it also avoids a silent 60-second slowdown when the race occurs.

@maflcko

maflcko commented Nov 20, 2026

Copy link
Copy Markdown
Member

@Pranav-error Sharing LLM output will get you blocked, see https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md. If you have something relevant to say, this is great, but then please write it by hand or if it must be LLM output, clearly mark it as such.

@Pranav-error

Pranav-error commented Nov 20, 2026 •

Copy link
Copy Markdown

@maflcko i had used ai to pharse my words so there is no gramtical misatke so i woudn't be doing that also from next time

@maflcko maflcko left a comment

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.

thx, but I am not sure if this is correct

Comment thread test/functional/p2p_tx_download.py Outdated
Comment on lines +141 to +142
self.nodes[0].setmocktime(0)
self.nodes[1].setmocktime(0)

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.

you can remove both, the outer loop already takes care of this.

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.

I did this in 761ec7e which also removed another location

Comment thread test/functional/p2p_tx_download.py Outdated

@maflcko maflcko Nov 20, 2026 •

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.

I think if you select self.nodes[1].setmocktime(now) the test will break, because NONPREF_PEER_TX_DELAY can never hit? Sure, it never hit in practise before either, but it seems a bit odd for the test to imply it can be hit when now it can deterministically never hit.

Also, what does "races" refer to when both nodes use mocktime? The patch here doesn't seem right, or at least incomplete.

I think the issue you are seeing was introduced in 2706c5b (I haven't tried this).

So the possible fixes are:

  • Revert 2706c5b (and go back to rough "best-effort" test coverage)
  • Properly use mocktime to keep the test coverage (and ideally exercise the exact code triggers mentioned here)

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.

I should have spent a bit more time before opening the PR. I do think there's a flake here though.

I think currently the test is broken on master:

  • self.nodes[0] uses mocktime and self.nodes[1] does not. So when self.nodes[0].bumpmocktime(timeout) is used, it doesn't make much sense because that timeout is actually meant for self.nodes[1] since it doesn't have the tx and may request the tx from the test peers and then fallback to requesting it from self.nodes[0]. I think self.nodes[0] mocktime does need to be bumped, but only around the order of INBOUND_INVENTORY_BROADCAST_INTERVAL so it can actually announce to self.nodes[1].
  • the second thing is that because self.nodes[1] does not use mock time, its real clock can advance before self.nodes[0] sends the inv. If time advances on self.nodes[1] by NONPREF_PEER_TX_DELAY before self.nodes[0] can announce, self.nodes[1] will request from a test peer first. It will expire after ~60s (GETDATA_TX_INTERVAL) and while this is happening, the sync_mempools timeout of 60s may be hit.

Is this too much of an edge case?

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.

Sorry, I pasted the wrong commit id :(

I've edited my comment with the correct id.

I agree that there is a flake here, and it could make sense to fix it. However, I think the correct fix is going to look different.

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.

Properly use mocktime to keep the test coverage (and ideally exercise the exact code triggers mentioned here)

I think it makes more sense to do this and split into two subtests where one subtest checks behavior when self.nodes[1] requests from a non-preferred peer first and the other checks behavior when self.nodes[1] requests from self.nodes[0] first.

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.

sgtm. Happy to review a pull doing that :)

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.

I'm a little confused what test_inv_block is testing. Is it testing that even peers that inv the tx first and never provide it ("block" delivery of the tx) can't prevent the node from getting the tx? Because if so, it seems a little odd because in the real world, self.nodes[1] would request the tx from its other peers after ~2s as self.nodes[0] would only announce after ~5s. But the test immediately bumps the mocktime of self.nodes[0] so it ~instantly announces to self.nodes[1] and usually self.nodes[1] will request from self.nodes[0] instead of from its other peers. This doesn't match reality, so I'm confused.

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.

Yeah, I think this was another bug introduced in 2706c5b? It dropped the NONPREF_PEER_TX_DELAY sleep.

Not sure if there is another test case already for the outbound announcing first, but I think it would be good to write two test cases, or at a minimum fix this test case to add back the NONPREF_PEER_TX_DELAY properly.

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.

I tested that commit and it did introduce the bug. I modified the test to use mocktime and request from a blocking inbound peer first in 4cc74b2. I decided against adding a subtest for an outbound announcing first since test_preferred_tiebreaker_inv covers this case and, to me, it doesn't really test a peer blocking relay. It mostly seems like an artifact of 2706c5b. Also, the whole p2p_tx_download.py suite takes ~20s to run on my machine, compared to the commit before 2706c5b which took ~90s to run.

@Waridi318

Copy link
Copy Markdown
Contributor

Concept ACK on splitting this into two subtests. But one thing to watch for in the rework is that INBOUND_INVENTORY_BROADCAST_INTERVAL is a mean that is passed to rand_exp_duration, and it's not an upper bound. And so bumping node 0 by roughly that interval only covers the draw about 2/3 of the time.

So I tried this and applied this branch locally. I changed node 0's bump from timeout to 5 seconds, and then I ran test_inv_block 20 times. I got 6 failures, all at the inv_to_send assertion. With the existing bump, I got 20 out of 20 passed.

There was #35324, which introduced this assertion with a comment that it may fail rarely and retry the test. So that means that sizing that bump to the interval would turn that rare failure into an actual routine one.

There's a function, test_tx_requests, in this file that handles the same problem by bumping in a loop until the expected getdata arrives. That approach I think would work here and would let the caveat be dropped.

@Crypt-iQ

Crypt-iQ commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

But one thing to watch for in the rework is that INBOUND_INVENTORY_BROADCAST_INTERVAL is a mean that is passed to rand_exp_duration

I think bumping to a reasonable amount over is fine to make the chance of this happening low. Looping also works, though maybe we could just cap the timers in net_processing.cpp

Because nodes[1] time was not mocked in test_inv_block, it was
possible for wall clock to advance 2s by the time nodes[0] had
delivered a tx announcement to it. Then, nodes[1] would request
the tx from a test peer, never receive the tx, and fail in
sync_mempools(). Fix this by using mocktime for nodes[1] as well.

Another issue is that nodes[0] would immediately announce the
tx which usually made nodes[1] request it from them instead
of from a blocking inbound peer. Fix this by ensuring that
nodes[1] requests from a blocking inbound peer first.
@Crypt-iQ
Crypt-iQ force-pushed the 2026-11/txdownload-flake branch from e556dc1 to 4cc74b2 Compare November 25, 2026 19:17
@Crypt-iQ

Copy link
Copy Markdown
Contributor Author

Latest push to 4cc74b2 uses mocktime and ensures that self.nodes[1] requests from a blocking inbound peer first. It is also slightly faster. I will run the patch under rr and see if I've introduced any flakes.

@Waridi318

Copy link
Copy Markdown
Contributor

But one thing to watch for in the rework is that INBOUND_INVENTORY_BROADCAST_INTERVAL is a mean that is passed to rand_exp_duration

I think bumping to a reasonable amount over is fine to make the chance of this happening low. Looping also works, though maybe we could just cap the timers in net_processing.cpp

Yeap. Agreed. Would also be interested to see whether a cap would apply to some of the other tests on #19732.

@Pranav-error

Copy link
Copy Markdown

Tested on macOS arm64. The PR passed 20/20 runs, and Crypt-iQ's reproducer (2s delay after sendrawtransaction) passed 10/10 runs.

For comparison, my Nov 20 runs on master with the same delay failed 2/5 times due to mempool sync timeouts.

Didn't test on other OSes or with rr though, but looks good from my testing too.

@Crypt-iQ

Copy link
Copy Markdown
Contributor Author

rr says I've introduced yet another flake in the latest patch. Will debug next week.

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