mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 09:35:38 +00:00
p2p: Suspend ping timeout while downloading blocks from a peer #36080
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!36080
Loading…
Reference in a new issue
No description provided.
Delete branch "202608_ping_ibd"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
While serving blocks, there is a system in place that prioritizes a peer's block requests before answering other p2p messages:
See
github.com/bitcoin/bitcoin@11090c8bb3/src/net_processing.cpp (L5436)As a result it can happen that if we do IBD with a low download bandwidth (that is distributed over 10 peers) a peer will not get around to answering our ping before the timeout of 20 minutes, in which case we would disconnect them, although they have done nothing wrong and are not even slow themselves (we are).
This situation has been described in #35761.
This PR fixes the issue by not enforcing the ping timeout from a peer while downloading blocks from them.
In order to do that, the ping timeout check is moved out of
MaybeSendPing()(which was a slightly awkward place anyway, given the name of the function) and suspended until there are no longer blocks in flight with that peer (with a grace period, so that we don't disconnect immediately after the last block was received before the peer got a chance to send us thepong).Note that during block download, there are still other timeouts:
BLOCK_DOWNLOAD_TIMEOUT_BASE/BLOCK_DOWNLOAD_TIMEOUT_PER_PEER) which will result in a timeout of600s × (1 + 0.5×9) = 55 minutesper block when downloading from 10 peers in parallelSo the ping timeout didn't add much value anyway in that situation.
Fixes #35761
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.
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:
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.
Concept ACK
nit: whenever there's a test after the fix, I have a hard time understanding what the behavior was before the fix, it's why I usually add a characterization test before the fix to document the previous behavior, showing that intermediary refactors don't update the test (i.e. aren't changing behavior) while the fix commit only does surgical changes to the assertions, documenting exactly how the fix changes the assumptions, see https://github.com/bitcoin/bitcoin/pull/35260
It would help with the review if most of the tests would be moved before the fix to help us understand both the before and after states.
Concept ACK! I am slowly going through it and learning more about block relay :)
(Comment placed in random location)
nit: In commit message be32083ddb17d5bc1667a56fe7f3b7a82f7e0e68, there's a typo:
timout -> timeoutSurfaced using Opus 5, I checked manually and I think it's correct:
You were previously checking whether to disconnect for inactivity first, and only then deciding whether to send a new ping, based on
peer.m_ping_queuedand whether enough time passed from the last ping. Now you're doing the opposite, and what might happen is:MaybeSendPing,peer.m_ping_queuedis true, sopingSendgets set to true, andpeer.m_ping_start = nowandpeer.m_ping_nonce_sentget reset.m_ping_startwill be fresh, and we won't disconnect.The chances are of course slim, and I'm not sure what's the best way to fix...
m_ping_queuedandm_ping_startbeforeMaybeSendPingis called, and here evaluate against those - not super clean, but will workMaybeSendPingyou could honor the user's ping request only if there's not an outstanding ping, but I don't think that's correctOpus 5 flagged this - I think it's true in theory, but I'm not sure in practice if it can ever happen, there's a lot of details around block relay that I don't really know and I'm trying to understand :)
You're using
m_last_block_time, but it gets updated only upon receiving a block we didn't already have, which means that a peer that sends us block that we already had would get a shorter post block pong grace, or none at all.github.com/bitcoin/bitcoin@dc0395c585/src/net_processing.cpp (L3676-L3681)Suppose peerA has been sending us blocks, and not responding to our pings in the meantime. We only miss one block, the tip. Then, either of this happens:
In any case, peerB sends us the last block in peerA's queue. Then peerA will be given a shorter post block pong grace, or none at all, if the last new block they sent was more than 1min ago. This is because peerA sending us the last block didn't update
m_last_block_time, since the block wasn't new to us.In e13760ad536f15a3280ae555c65ff922cf820478: I don't understand what's the purpose of this check; if the peer was a Bitcoin Core node, we wouldn't be connected anymore (this is the first commit, so there's still ping timeout). The peer is still connected only because
P2PInterfacehas no ping timeout.moved some of the test into the first commit. The grace period test coverage makes no sense there though.
fixed
makes sense, I removed the check.
I also don't think that this is a real issue in practice, because the original problem exists mostly during IBD, where bandwidth is spread over 10 peers and 16 blocks are queued at the same time. In IBD, we don't request a single block from multiple peers at the same time, so this wouldn't happen. Even if this could happen in theory as a one-off after catching up with the tip as in your scenario, it's a one-off and not a systematic problem as the status quo.
this feels like one of those fringe KI findings that I think don't really need to be addressed:
As far as I can see, a user can meddle with pings already today on master - if they send a
pingrpc every 19 minutes, an unresponsive peer will never be disconnected due to missing pings. For the particular issue here, they would need to time their ping exactly, which seems very unlikely. But I don't even think we need to fix the behavior on master.abeb912 to c551ff4: addressed feedback
Thanks for dealing with my AI-assisted review :) I only left an additional comment, I think the test can be made slightly more robust, by checking that the timeout and post block pong grace are only enforced if there are no blocks in flight, which kills one mutant.
I think here you should also wait for
POST_BLOCK_PONG_GRACE + 1seconds, and check that you don't get disconnected:This way you're checking that we're not disconnecting once the timeout elapsed and the
POST_BLOCK_PONG_GRACEperiod passed if there are still blocks in flight.If you don't add this check, you can mutate the original code like this, and the test will still pass:
good suggestion - done!
tested ACK
31280333ddBuilt each commit. Between
MaybeSendPingand the new location there are only two returns, and both already setfDisconnect, so no peer class gains or loses the timeout from the move alone.p2p_ping_ibd.py,--v2transportandp2p_ping.pypass at the tip, and the test from56c194e655passes against the95dcf8f10dbinary while only failing against the tip, so the relocation is behaviour-preserving.I also tested it between two regtest nodes, no mocktime, with a local proxy limiting the serving side to 8 KiB/s while the receiver downloaded 25 blocks of ~946 kB. Before the fix the receiver disconnected after ~22 minutes at height 211, with 14 blocks still in flight:
2026-09-21T04:38:36.772959Z [net] ping timeout: 1200.019086s, disconnecting peer=0
With the fix it stayed connected, answered a ping 30m47s late and finished the 25 blocks in ~48 minutes.
One question (non-blocking): with a block in flight, is the block download timeout now the only bound, and does its window reset on every front-of-queue arrival? That would let a peer that never pongs but keeps delivering stay connected for hours. Seems right to me but I just want to confirm it is intended.
Part of the testing was AI-assisted (build and test orchestration, log collection). I checked the results myself.
Code review ACK
31280333ddModulo my comment in the functional test, but it's also pretty harmless afaict, just would be good to address if you retouch.
I am not sure I am getting this incorrectly but the peer is the one that would need their time bumped because that's the receiving side. But also running the check immediately feels racy. It seems to
pongis being stalled by the stalling of blocks, so this can't actually fail as it is done now. I think the whole check can be removed since the assert just duplicates the one above if the timebump has no effect.review ACK
31280333dd🛐Show signature
Signature:
nit in the second commit
95dcf8f10d: Obviously this is fine, but personally I am not a fan of expanding massive scopes (and locks) that span several unrelated topics over hundreds of lines.For me, this makes it hard to review, because:
--function-contextwill pull in all tx getdata logic.So I am thinking if it could make sense to move this to its own small lock scope. I don't think placement matters: It could be in the end after
MaybeSendFeefilter, or early (beforeMaybeSendPing). I don't mind moving it out ofMaybeSendPing, I just wonder if there is a better placement with a smaller (lock) scope, so that future code changes in this area are easier to review.nit in the last commit
31280333dd: Not sure why this line is removed? It seems it should come right after theI had the same thought: This sub-test is mostly about the order of messages and that stalling can occur, not about exact timing. Seems fine to remove, but no strong opinion.
nit in the first commit: The timeout can probably be dropped? Sending 16MB should be faster than that even with sanitizers (and sanitizer come with an increased --timout-factor anyway).
Base:
69142eacd1374925cc9e4ea736c21fcec16307d0Head:
d83647e7c48a11dc54ad697342a4b7e3bce9061b🟡 Minor
Grant grace for late duplicate block responses (src/net_processing.cpp:6445, head)
When multiple peers are asked for the same block, one peer can deliver it first, causing
ProcessBlockto remove all in-flight requests. If another peer's already-issued response then arrives as a duplicate,ProcessBlockdoes not refreshm_last_block_time; an expired ping can therefore trigger this check immediately when the peer's in-flight list is empty. Track relevant block responses separately or otherwise grant grace for a late response to a canceled request. Parallel compact-block requests are permitted, but how often a late duplicate coincides with an expired ping in production is unconfirmed.Review coverage was incomplete. The alternatives experiment had incomplete evidence. The adversarial review had incomplete evidence. The adversarial_glm review had incomplete evidence.
Full review report
good point, I removed it. I just left the comment about what would happen at the peer, the other added test covers the other side anyway.
done
The reason I did that was to avoid locking
cs_mainmultiple times inSendMessages, which seemed to be the preexisting philosophy of that function. I checked that the refactor #35522 also doesn't change this. I feel that if taking/releasing the lock multiple times is not something we want to avoid, we could also just leave the check where it is inMaybeSendPingand add a lock there (even if it is a bit unexpected given the function name). So no strong opinion, but I didn't change it with the last push.readded the line, it wasn't meant to be removed.
3128033 to d83647e: addressed feedback
yes, I think that is correct. There is still the socket inactivity check, but this will not fire if the peer keeps on sending blocks. But I don't think that this is a problem, blocks are more helpful for us than ping messages, which are not really important by themselves but a way to check responsibility in times when we don't exchanges more important messages.
Only change is test nits
review ACK
d83647e7c4🌎Show signature
Signature:
reACK
d83647e7c4ACK
d83647e7c4ACK
d83647e7c4