net: unify send pause state #36736

Open
l0rinc wants to merge 3 commits from l0rinc/paused-send-state-cleanup into master AGit
l0rinc commented 2026-11-02 22:12:21 +00:00 (Migrated from github.com)

Problem: Each peer's send pause state is a flag plus a separate start timestamp.
Message queueing and socket sends each duplicate the transition logic that keeps the two in sync, and the inactivity check (which needs both), reads them separately.

Fix: Move the pause transitions into CNode accessors over one atomic timestamp.
The inactivity check then reads the pause status and start time as one snapshot.

Follow-up to #36667.

**Problem:** Each peer's send pause state is a flag plus a separate start timestamp. Message queueing and socket sends each duplicate the transition logic that keeps the two in sync, and the inactivity check (which needs both), reads them separately. **Fix:** Move the pause transitions into `CNode` accessors over one atomic timestamp. The inactivity check then reads the pause status and start time as one snapshot. Follow-up to #36667.
DrahtBot commented 2026-11-02 22:12:32 +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
Concept ACK jeanpablojp

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:

  • #36819 ([RFC] enable clang-tidy optin.performance.Padding by fanquake)
  • #35558 (p2p: Prefill compact blocks by davidgumberg)
  • #35027 (net: add -outboundbind option for outgoing source address by 8144225309)

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.

<!--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/36736 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36736.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36736#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 | | ---- | --------- | | Concept ACK | [jeanpablojp](https://github.com/bitcoin/bitcoin/pull/36736#pullrequestreview-5967090558) | 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: * [#36819](https://github.com/bitcoin/bitcoin/pull/36819) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36819.svg"></sub> ([RFC] enable `clang-tidy` `optin.performance.Padding` by fanquake) * [#35558](https://github.com/bitcoin/bitcoin/pull/35558) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35558.svg"></sub> (p2p: Prefill compact blocks by davidgumberg) * [#35027](https://github.com/bitcoin/bitcoin/pull/35027) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35027.svg"></sub> (net: add -outboundbind option for outgoing source address by 8144225309) 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-->
First-time contributor

🔵 Suggestion · Paused-send timeout decisions lack focused test coverage

The new accessor test would still pass if the rewritten inactivity check stopped timing out paused peers or used the wrong deadline.

Location: src/net.cpp:2087 (head).

Review coverage was incomplete.


Reviewed: f5feda749552 · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:965af1044d9e41839565702e4d9bd0a751eda81f6e8b6402747fa296ef84ebea:7efd3c3ba41667382004cfa7a7db56605da7d8f9b7ab69bc483f27117390ba48 --> <!-- ralph-base:ddea9196493d3b8fd841a36a6c53efd85dac7a05 --> <!-- ralph-head:f5feda7495526b04ea418edcef540e596ce8bbf1 --> <!-- ralph-case:54b3932bd63d2844a36f8ae39dcaacf76ddd9066582056eb3a14c2d2cea1803e --> **🔵 Suggestion · Paused-send timeout decisions lack focused test coverage** The new accessor test would still pass if the rewritten inactivity check stopped timing out paused peers or used the wrong deadline. Location: [`src/net.cpp:2087`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/f5feda7495526b04ea418edcef540e596ce8bbf1/src/net.cpp#L2087>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`f5feda749552`](<https://git.fish.foo/bitcoin/bitcoin/commit/f5feda7495526b04ea418edcef540e596ce8bbf1>) · [Full review report](<https://review.fish.foo/traces/36736-f5feda7495526b04ea418edcef540e596ce8bbf1-2021-1.html>) · [Previous reports](<https://review.fish.foo/traces/36736/>) [permalink](<https://review.fish.foo/traces/36736-f5feda7495526b04ea418edcef540e596ce8bbf1-2021-1.html>)
jeanpablojp (Migrated from github.com) reviewed 2026-11-18 23:50:09 +00:00
jeanpablojp (Migrated from github.com) left a comment

Concept ACK

Concept ACK
jeanpablojp (Migrated from github.com) commented 2026-11-18 23:50:10 +00:00

Ralph suggested a test for this branch, which has had none since #36667.

The test below fails if the branch is removed or > is changed to >=.

Suggestion
diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp
index 21ab8937ac..6f5a78da71 100644
--- a/src/test/net_tests.cpp
+++ b/src/test/net_tests.cpp
@@ -163,6 +163,15 @@ BOOST_AUTO_TEST_CASE(cnode_pause_send)
     peer.SetPauseSend(true);
     BOOST_CHECK(peer.IsSendPaused());
     BOOST_CHECK_EQUAL(peer.GetPauseSendSince(), NodeClock::now());
+
+    auto& connman{static_cast<ConnmanTestMsg&>(*m_node.connman)};
+    peer.fSuccessfullyConnected = true;
+    clock += TIMEOUT_INTERVAL;
+    peer.m_last_send = NodeClock::now();
+    peer.m_last_recv = NodeClock::now();
+    BOOST_CHECK(!connman.InactivityCheckPublic(peer, NodeClock::now()));
+    clock += 1s;
+    BOOST_CHECK(connman.InactivityCheckPublic(peer, NodeClock::now()));
 }
 
 BOOST_AUTO_TEST_CASE(cnetaddr_basic)
diff --git a/src/test/util/net.h b/src/test/util/net.h
index 7317e49075..23556eb650 100644
--- a/src/test/util/net.h
+++ b/src/test/util/net.h
@@ -94,6 +94,11 @@ struct ConnmanTestMsg : public CConnman {
         SocketHandler();
     }
 
+    bool InactivityCheckPublic(const CNode& node, NodeClock::time_point now) const
+    {
+        return InactivityCheck(node, now);
+    }
+
     void Handshake(CNode& node,
                    bool successfully_connected,
                    ServiceFlags remote_services,
Ralph suggested a test for this branch, which has had none since #36667. The test below fails if the branch is removed or `>` is changed to `>=`. <details> <summary>Suggestion</summary> ```diff diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp index 21ab8937ac..6f5a78da71 100644 --- a/src/test/net_tests.cpp +++ b/src/test/net_tests.cpp @@ -163,6 +163,15 @@ BOOST_AUTO_TEST_CASE(cnode_pause_send) peer.SetPauseSend(true); BOOST_CHECK(peer.IsSendPaused()); BOOST_CHECK_EQUAL(peer.GetPauseSendSince(), NodeClock::now()); + + auto& connman{static_cast<ConnmanTestMsg&>(*m_node.connman)}; + peer.fSuccessfullyConnected = true; + clock += TIMEOUT_INTERVAL; + peer.m_last_send = NodeClock::now(); + peer.m_last_recv = NodeClock::now(); + BOOST_CHECK(!connman.InactivityCheckPublic(peer, NodeClock::now())); + clock += 1s; + BOOST_CHECK(connman.InactivityCheckPublic(peer, NodeClock::now())); } BOOST_AUTO_TEST_CASE(cnetaddr_basic) diff --git a/src/test/util/net.h b/src/test/util/net.h index 7317e49075..23556eb650 100644 --- a/src/test/util/net.h +++ b/src/test/util/net.h @@ -94,6 +94,11 @@ struct ConnmanTestMsg : public CConnman { SocketHandler(); } + bool InactivityCheckPublic(const CNode& node, NodeClock::time_point now) const + { + return InactivityCheck(node, now); + } + void Handshake(CNode& node, bool successfully_connected, ServiceFlags remote_services, ``` </details>
jeanpablojp (Migrated from github.com) commented 2026-11-18 23:50:10 +00:00

No test fails with this check, the one for blocks below and the one in ProcessMessages all removed, and none did before this PR either.

The test below fails without any one of the three.

Suggestion
diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp
index 21ab8937ac..d114ed4e66 100644
--- a/src/test/net_tests.cpp
+++ b/src/test/net_tests.cpp
@@ -1822,4 +1822,37 @@ BOOST_AUTO_TEST_CASE(addlocal_onlynet_externalip)
     fDiscover = discover_orig;
 }
 
+BOOST_AUTO_TEST_CASE(paused_peer_backpressure)
+{
+    LOCK(NetEventsInterface::g_msgproc_mutex);
+    auto& connman{static_cast<ConnmanTestMsg&>(*m_node.connman)};
+    CNode peer{/*id=*/0, /*sock=*/nullptr, /*addrIn=*/CAddress{}, /*nKeyedNetGroupIn=*/0, /*nLocalHostNonceIn=*/0, /*addrBindIn=*/CService{}, /*addrNameIn=*/"", ConnectionType::INBOUND, /*inbound_onion=*/false, /*network_key=*/0};
+    connman.Handshake(peer, /*successfully_connected=*/true, ServiceFlags(NODE_NETWORK | NODE_WITNESS), ServiceFlags(NODE_NETWORK | NODE_WITNESS), PROTOCOL_VERSION, /*relay_txs=*/true);
+    std::vector<std::string> sent;
+    const auto capture_orig{CaptureMessage};
+    CaptureMessage = [&](const CAddress&, const std::string& type, std::span<const unsigned char>, bool incoming) { if (!incoming) sent.push_back(type); };
+    connman.SetCaptureMessages(true);
+
+    // Serving the first block fills the send buffer, so the rest of the request has to wait.
+    std::atomic<bool> interrupt{false};
+    const CInv block{MSG_WITNESS_BLOCK, Params().GenesisBlock().GetHash()};
+    for (const CInv& queued : {block, CInv{MSG_WTX, uint256::ONE}}) {
+        connman.FlushSendBuffer(peer);
+        BOOST_REQUIRE(connman.ReceiveMsgFrom(peer, NetMsg::Make(NetMsgType::GETDATA, std::vector{block, queued})));
+        connman.ProcessMessagesOnce(peer);
+        m_node.peerman->ProcessMessages(peer, interrupt);
+        BOOST_CHECK_EQUAL(sent.size(), 1U);
+        connman.ProcessMessagesOnce(peer);
+        sent.clear();
+    }
+
+    // Nor is a new message processed while the peer stays paused.
+    connman.FlushSendBuffer(peer);
+    BOOST_REQUIRE(connman.ReceiveMsgFrom(peer, NetMsg::Make(NetMsgType::PING, uint64_t{0})));
+    m_node.peerman->ProcessMessages(peer, interrupt);
+    BOOST_CHECK(sent.empty());
+    CaptureMessage = capture_orig;
+    m_node.peerman->FinalizeNode(peer);
+}
+
 BOOST_AUTO_TEST_SUITE_END()
No test fails with this check, the one for blocks below and the one in `ProcessMessages` all removed, and none did before this PR either. The test below fails without any one of the three. <details> <summary>Suggestion</summary> ```diff diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp index 21ab8937ac..d114ed4e66 100644 --- a/src/test/net_tests.cpp +++ b/src/test/net_tests.cpp @@ -1822,4 +1822,37 @@ BOOST_AUTO_TEST_CASE(addlocal_onlynet_externalip) fDiscover = discover_orig; } +BOOST_AUTO_TEST_CASE(paused_peer_backpressure) +{ + LOCK(NetEventsInterface::g_msgproc_mutex); + auto& connman{static_cast<ConnmanTestMsg&>(*m_node.connman)}; + CNode peer{/*id=*/0, /*sock=*/nullptr, /*addrIn=*/CAddress{}, /*nKeyedNetGroupIn=*/0, /*nLocalHostNonceIn=*/0, /*addrBindIn=*/CService{}, /*addrNameIn=*/"", ConnectionType::INBOUND, /*inbound_onion=*/false, /*network_key=*/0}; + connman.Handshake(peer, /*successfully_connected=*/true, ServiceFlags(NODE_NETWORK | NODE_WITNESS), ServiceFlags(NODE_NETWORK | NODE_WITNESS), PROTOCOL_VERSION, /*relay_txs=*/true); + std::vector<std::string> sent; + const auto capture_orig{CaptureMessage}; + CaptureMessage = [&](const CAddress&, const std::string& type, std::span<const unsigned char>, bool incoming) { if (!incoming) sent.push_back(type); }; + connman.SetCaptureMessages(true); + + // Serving the first block fills the send buffer, so the rest of the request has to wait. + std::atomic<bool> interrupt{false}; + const CInv block{MSG_WITNESS_BLOCK, Params().GenesisBlock().GetHash()}; + for (const CInv& queued : {block, CInv{MSG_WTX, uint256::ONE}}) { + connman.FlushSendBuffer(peer); + BOOST_REQUIRE(connman.ReceiveMsgFrom(peer, NetMsg::Make(NetMsgType::GETDATA, std::vector{block, queued}))); + connman.ProcessMessagesOnce(peer); + m_node.peerman->ProcessMessages(peer, interrupt); + BOOST_CHECK_EQUAL(sent.size(), 1U); + connman.ProcessMessagesOnce(peer); + sent.clear(); + } + + // Nor is a new message processed while the peer stays paused. + connman.FlushSendBuffer(peer); + BOOST_REQUIRE(connman.ReceiveMsgFrom(peer, NetMsg::Make(NetMsgType::PING, uint64_t{0}))); + m_node.peerman->ProcessMessages(peer, interrupt); + BOOST_CHECK(sent.empty()); + CaptureMessage = capture_orig; + m_node.peerman->FinalizeNode(peer); +} + BOOST_AUTO_TEST_SUITE_END() ``` </details>
l0rinc (Migrated from github.com) reviewed 2026-12-03 17:49:11 +00:00
l0rinc (Migrated from github.com) commented 2026-12-03 17:49:11 +00:00

Is that an interesting difference?

Is that an interesting difference?
jeanpablojp (Migrated from github.com) reviewed 2026-12-03 18:27:54 +00:00
jeanpablojp (Migrated from github.com) commented 2026-12-03 18:27:54 +00:00

No, not by itself. The >= was only there to show the test checks the timeout too. My point was that this disconnect has no test coverage today.

No, not by itself. The `>=` was only there to show the test checks the timeout too. My point was that this disconnect has no test coverage today.
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!36736
No description provided.