Skip to content

net: unify send pause state - #36736

Open
l0rinc wants to merge 3 commits into
bitcoin:masterfrom
l0rinc:l0rinc/paused-send-state-cleanup
Open

l0rinc wants to merge 3 commits into
bitcoin:masterfrom
l0rinc:l0rinc/paused-send-state-cleanup

Conversation

@l0rinc

@l0rinc l0rinc commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

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.

The test send buffer limit is zero, so queued messages set `fPauseSend`.
Move the repeated resets into `ProcessMessagesOnce()`.
This adds resets at four call sites in `orphan_reconsideration_interruptible`, where the peer is already unpaused.
Keeping resets here will hide the later unpaused timestamp sentinel from test callers.
Hide the flag and timestamp behind `CNode` accessors and extract their shared updates into `SetPauseSend()`.
Both production update sites already hold `cs_vSend`.
Cover preserving an active pause's start time and recording a fresh one after resuming.
Encode the paused state in the timestamp, using epoch for unpaused.
This removes the separate flag and gives readers one atomic snapshot.
Preserve the original start time across repeated pause updates.
@DrahtBot DrahtBot changed the title net: unify send pause state net: unify send pause state Nov 2, 2026
@DrahtBot DrahtBot added the P2P label Nov 2, 2026
@DrahtBot

DrahtBot commented Nov 2, 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 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.

@jeanpablojp jeanpablojp left a comment

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.

Concept ACK

Comment thread src/net.cpp
);
return true;
}
if (auto last_pause{node.GetPauseSendSince()}; last_pause && now > *last_pause + TIMEOUT_INTERVAL) {

@jeanpablojp jeanpablojp Nov 18, 2026 •

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.

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,

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.

Is that an interesting difference?

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.

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.

Comment thread src/net_processing.cpp
// The send buffer provides backpressure. If there's no space in
// the buffer, pause processing until the next call.
if (pfrom.fPauseSend) break;
if (pfrom.IsSendPaused()) break;

@jeanpablojp jeanpablojp Nov 18, 2026 •

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.

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()

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.

3 participants