test: Fix intermittent issue in p2p_orphan_handling.py #36617

Manually merged
maflcko merged 1 commit from 2610-test-fix-int-orphan into master 2026-11-04 19:45:14 +00:00 AGit
maflcko commented 2026-10-22 21:08:08 +00:00 (Migrated from github.com)

Turns out https://github.com/bitcoin/bitcoin/pull/32092 was incomplete and the issue can still happen:

https://github.com/bitcoin/bitcoin/actions/runs/39809026827/job/119929911875#step:14:14454

The basic idea is that peer1 disconnects to clear its orphan state. This happens in DisconnectNodes->DeleteNode->FinalizeNode->TxDownloadManager::DisconnectedPeer.

In my prior fix, I assumed that DisconnectNodes in the net thread would be possible to sync on via a ping-pong. However, the msghand thread may hold a reference to the node, preventing deletion and only moving the node to m_nodes_disconnected. Afterward, the net thread may hand the pings to the msghand thread and go to sleep for a bit.

Thus, the test proceed and cause a split getdata, meaning that peer2.wait_for_parent_requests([int(parent_peekaboo_AB["txid"], 16), int(parent_missing["txid"], 16)]) fails.

Fix this by just waiting for TxDownloadManager::DisconnectedPeer to have happened via self.wait_until(lambda: len(node.getorphantxs(verbosity=2)[0]["from"]) == 1).

Edit: Possible diff to reproduce with ~10% failure rate:

diff --git a/src/net.cpp b/src/net.cpp
index 1201d3bcb4..e81623484b 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -2229,2 +2229,3 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
                     WakeMessageHandler();
+            UninterruptibleSleep(333ms *m_nodes_disconnected.size());
                 }
@@ -3261,2 +3262,3 @@ void CConnman::ThreadMessageHandler()
             }
+            UninterruptibleSleep(33ms);
         }

Cmd: ./bld-cmake/test/functional/test_runner.py $(for i in {1..20}; do echo -n "p2p_orphan_handling.py ";done) -j $( nproc )

Turns out https://github.com/bitcoin/bitcoin/pull/32092 was incomplete and the issue can still happen: https://github.com/bitcoin/bitcoin/actions/runs/39809026827/job/119929911875#step:14:14454 The basic idea is that peer1 disconnects to clear its orphan state. This happens in `DisconnectNodes`->`DeleteNode`->`FinalizeNode`->`TxDownloadManager::DisconnectedPeer`. In my prior fix, I assumed that `DisconnectNodes` in the `net` thread would be possible to sync on via a ping-pong. However, the msghand thread may hold a reference to the node, preventing deletion and only moving the node to `m_nodes_disconnected`. Afterward, the `net` thread may hand the pings to the `msghand` thread and go to sleep for a bit. Thus, the test proceed and cause a split `getdata`, meaning that `peer2.wait_for_parent_requests([int(parent_peekaboo_AB["txid"], 16), int(parent_missing["txid"], 16)])` fails. Fix this by just waiting for `TxDownloadManager::DisconnectedPeer` to have happened via `self.wait_until(lambda: len(node.getorphantxs(verbosity=2)[0]["from"]) == 1)`. Edit: Possible diff to reproduce with ~10% failure rate: ```diff diff --git a/src/net.cpp b/src/net.cpp index 1201d3bcb4..e81623484b 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -2229,2 +2229,3 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes, WakeMessageHandler(); + UninterruptibleSleep(333ms *m_nodes_disconnected.size()); } @@ -3261,2 +3262,3 @@ void CConnman::ThreadMessageHandler() } + UninterruptibleSleep(33ms); } ``` Cmd: `./bld-cmake/test/functional/test_runner.py $(for i in {1..20}; do echo -n "p2p_orphan_handling.py ";done) -j $( nproc )`
DrahtBot commented 2026-10-22 21:08:18 +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 Conradgabe

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

<!--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/36617 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36617.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36617#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 | [Conradgabe](https://github.com/bitcoin/bitcoin/pull/36617#issuecomment-6366524818) | 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. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

Base: df0dc1e2d2059213e49a9f115fd55a9f0119d26c
Head: faf819bf23e8752644695e332cc00413d94af938

I found no actionable issues in this static review.

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:1e40d4c29146a38cb238a037edaa35c22b13996a5383ed7a85e089a15d569009:c9412d948811529792d161d97aa6951829440e3d548b7c576bb8d4df4183e0d0 --> Base: `df0dc1e2d2059213e49a9f115fd55a9f0119d26c` Head: `faf819bf23e8752644695e332cc00413d94af938` I found no actionable issues in this static review. [Full review report](<https://review.fish.foo/traces/36617-faf819bf23e8752644695e332cc00413d94af938-1221-2.html>)
Conradgabe commented 2026-10-23 13:54:16 +00:00 (Migrated from github.com)

crACK faf819bf23

nit: the comment on peer2.sync_with_ping() ("Sync with the 'net' thread which completes the disconnection fully") is no longer correct given this PR update, since the ping doesn't guarantee peer1's deferred deletion has happened. The comment could be changed to something like "Sync with the 'net' thread and confirm peer2's messages have been processed".

github.com/bitcoin/bitcoin@faf819bf23/test/functional/p2p_orphan_handling.py (L829)

crACK faf819bf23e8752644695e332cc00413d94af938 nit: the comment on `peer2.sync_with_ping()` ("Sync with the 'net' thread which completes the disconnection fully") is no longer correct given this PR update, since the ping doesn't guarantee peer1's deferred deletion has happened. The comment could be changed to something like "Sync with the 'net' thread and confirm peer2's messages have been processed". https://github.com/bitcoin/bitcoin/blob/faf819bf23e8752644695e332cc00413d94af938/test/functional/p2p_orphan_handling.py#L829
maflcko commented 2026-10-23 14:05:38 +00:00 (Migrated from github.com)

nit: the comment on peer2.sync_with_ping() ("Sync with the 'net' thread which completes the disconnection fully") is no longer correct given this PR update, since the ping doesn't guarantee peer1's deferred deletion has happened. The comment could be changed to something like "Sync with the 'net' thread and confirm peer2's messages have been processed".

Can you please leave code review threads in the code tab. Otherwise, it is hard to follow the threads. Also, this comment doesn't seem right? self.wait_until(lambda: len(node.getorphantxs(verbosity=2)[0]["from"]) == 1) establishes that DisconnectedPeer has been (or is being) called. Thus, the next ping will ensure the disconnect is fully handled and the code and comment is correct?

> nit: the comment on `peer2.sync_with_ping()` ("Sync with the 'net' thread which completes the disconnection fully") is no longer correct given this PR update, since the ping doesn't guarantee peer1's deferred deletion has happened. The comment could be changed to something like "Sync with the 'net' thread and confirm peer2's messages have been processed". Can you please leave code review threads in the code tab. Otherwise, it is hard to follow the threads. Also, this comment doesn't seem right? `self.wait_until(lambda: len(node.getorphantxs(verbosity=2)[0]["from"]) == 1)` establishes that `DisconnectedPeer` has been (or is being) called. Thus, the next ping will ensure the disconnect is fully handled and the code and comment is correct?
Conradgabe commented 2026-10-23 15:48:19 +00:00 (Migrated from github.com)

Can you please leave code review threads in the code tab. Otherwise, it is hard to follow the threads

Sure, I’ll leave code review threads in the Code tab going forward. On the nit, I interpreted the comment as referring to sync_with_ping() synchronizing with the net thread after DisconnectedPeer has been invoked, rather than implying that the ping itself guarantees that the deferred deletion has already happened. Since the preceding wait_until(...) establishes that DisconnectedPeer has been called, the subsequent ping does ensure that the disconnect is fully handled, so I think the existing comment is still accurate.

> Can you please leave code review threads in the code tab. Otherwise, it is hard to follow the threads Sure, I’ll leave code review threads in the Code tab going forward. On the nit, I interpreted the comment as referring to `sync_with_ping()` synchronizing with the net thread after `DisconnectedPeer` has been invoked, rather than implying that the ping itself guarantees that the deferred deletion has already happened. Since the preceding `wait_until(...)` establishes that `DisconnectedPeer` has been called, the subsequent ping does ensure that the disconnect is fully handled, so I think the existing comment is still accurate.
maflcko commented 2026-10-23 15:52:02 +00:00 (Migrated from github.com)
Also when writing comments in discussions, you can not use LLMs, per https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md
sedited (Migrated from github.com) approved these changes 2026-11-04 19:41:32 +00:00
sedited (Migrated from github.com) left a comment
ACK faf819bf23e8752644695e332cc00413d94af938
fanquake commented 2026-11-05 11:00:49 +00:00 (Migrated from github.com)

Backported to 32.x in #36727.

Backported to `32.x` in #36727.
Sign in to join this conversation.
No reviewers
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!36617
No description provided.