http: Preserve socket errors before unlocking #37005

Open
natanleung wants to merge 1 commit from http-preserve-socket-errors into master AGit
natanleung commented 2026-11-28 13:24:01 +00:00 (Migrated from github.com)

Fixes #36973.

Background

For Windows MinGW build with DEBUG_LOCKORDER, releasing m_sock_mutex calls std::this_thread::get_id(). This can clear the Windows last-error value to 0 before the HTTP code reads it.

httpserver_tests/http_socket_error_tests test case mock Send() fails on the first call, then succeed on the next call. By clearing the error WSAEWOULDBLOCK, the test server does not retry and instead disconnects, causing the test failure.

Changes

  • Moved the WSAGetLastError() call into the critical sections for socket paths Send() and Recv()
  • Added a new unit test for Recv() in httpserver_tests/http_socket_error_tests
  • #35614 fixed an HTTP worker/I/O-thread race and introduced the regression
    identified in #36973.
  • A backport to 32.x was requested in the discussion on #36973. This patch
    targets master; the backport would be separate.
Fixes #36973. ### Background For Windows MinGW build with `DEBUG_LOCKORDER`, releasing `m_sock_mutex` calls `std::this_thread::get_id()`. This can clear the Windows last-error value to 0 before the HTTP code reads it. `httpserver_tests/http_socket_error_tests` test case mock `Send()` fails on the first call, then succeed on the next call. By clearing the error `WSAEWOULDBLOCK`, the test server does not retry and instead disconnects, causing the test failure. ### Changes * Moved the `WSAGetLastError()` call into the critical sections for socket paths `Send()` and `Recv()` * Added a new unit test for `Recv()` in `httpserver_tests/http_socket_error_tests` ### Related work * #35614 fixed an HTTP worker/I/O-thread race and introduced the regression identified in #36973. * A backport to `32.x` was requested in the discussion on #36973. This patch targets `master`; the backport would be separate.
DrahtBot commented 2026-11-28 13:24:12 +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 pinheadmz
Approach ACK winterrdog

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:

  • #36954 (net: make address overlap impossible when relaying addresses by vasild)
  • #35936 (net: reject oversized locators before allocating by l0rinc)
  • #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/37005 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/37005.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/37005#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 | [pinheadmz](https://github.com/bitcoin/bitcoin/pull/37005#issuecomment-7284954117) | | Approach ACK | [winterrdog](https://github.com/bitcoin/bitcoin/pull/37005#pullrequestreview-6099842894) | 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: * [#36954](https://github.com/bitcoin/bitcoin/pull/36954) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36954.svg"></sub> (net: make address overlap impossible when relaying addresses by vasild) * [#35936](https://github.com/bitcoin/bitcoin/pull/35936) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35936.svg"></sub> (net: reject oversized locators before allocating by l0rinc) * [#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

Earlier concerns neither republished nor resolved: 1 suggestion. See the Run archive linked from the Review report for their assessments.

🔵 Suggestion · CI omits the configuration needed by the socket regression tests

The new socket tests would likely pass without the fix in in-tree CI because its MinGW build omits DEBUG_LOCKORDER; this conclusion comes from configuration inspection, not an observed CI run.

Location: src/test/net_tests.cpp:1846 (head).

🔵 Suggestion · PR title and description omit the P2P fix and test

The PR describes only HTTP, although it also changes P2P send and receive error handling and adds net_tests/socket_error_tests, obscuring the scope reviewers and backporters need to assess.

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


Reviewed: 0409d2eadc60 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:e1a29e5e677c47d383756f0fcf8f3760725c91d1104dd3ccb2f333b6217eba26:47880f5569fd2047d249266f337c4f2a509adf7d96c3ccb1c7644c2c2a650bff --> <!-- ralph-base:2f3f6397bb92beccaccce16742f0574d1617fb3e --> <!-- ralph-head:0409d2eadc609d60238c6793989565b7bdcced9e --> <!-- ralph-case:6528cd7b77fdc95a7ade3ccf9a8b653a1a9b4359cf171dd2494b313020d41d2b --> Earlier concerns neither republished nor resolved: 1 suggestion. See the Run archive linked from the Review report for their assessments. **🔵 Suggestion · CI omits the configuration needed by the socket regression tests** The new socket tests would likely pass without the fix in in-tree CI because its MinGW build omits `DEBUG_LOCKORDER`; this conclusion comes from configuration inspection, not an observed CI run. Location: [`src/test/net_tests.cpp:1846`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/0409d2eadc609d60238c6793989565b7bdcced9e/src/test/net_tests.cpp#L1846>) (head). **🔵 Suggestion · PR title and description omit the P2P fix and test** The PR describes only HTTP, although it also changes P2P send and receive error handling and adds `net_tests/socket_error_tests`, obscuring the scope reviewers and backporters need to assess. Location: [`src/net.cpp:1668`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/0409d2eadc609d60238c6793989565b7bdcced9e/src/net.cpp#L1668>) (head). <!-- Review tier: sensitive --> --- Reviewed: [`0409d2eadc60`](<https://git.fish.foo/bitcoin/bitcoin/commit/0409d2eadc609d60238c6793989565b7bdcced9e>) · [Review report](<https://review.fish.foo/report/37005-0409d2eadc609d60238c6793989565b7bdcced9e-3499-5.html>) · [Previous reports](<https://review.fish.foo/traces/37005/>) [permalink](<https://review.fish.foo/report/37005-0409d2eadc609d60238c6793989565b7bdcced9e-3499-5.html>)
winterrdog (Migrated from github.com) reviewed 2026-11-28 19:54:12 +00:00
winterrdog (Migrated from github.com) left a comment

approach ACK

the error code from a failed recv()/send() is thread-local, so anything that runs on the same thread before the http server code comes around to read it can overwrite it. in this case, releasing the lock, m_sock_mutex, does exactly that in debug builds. a harmless "try again" error (WSAEWOULDBLOCK (10035)) then gets read as 0, which IOErrorIsPermanent treats as permanent and fatal thus dropping the client

so, reading it immediately after either socket API call, while still holding the lock, avoids that. MSDN docs also explicitly recommends doing it this way


also, +1 to Ralph's suggestion. the existing test only simulates failures on Send(), so we would not catch a regression on the Receive() side if the code is changed in an erroneous way, later on

approach ACK the error code from a failed `recv()`/`send()` is thread-local, so anything that runs on the same thread before the http server code comes around to read it can overwrite it. in this case, releasing the lock, `m_sock_mutex`, does exactly that in debug builds. a harmless `"try again"` error (`WSAEWOULDBLOCK` (`10035`)) then gets read as `0`, which `IOErrorIsPermanent` treats as permanent and fatal thus dropping the client so, reading it immediately after either socket API call, while still holding the lock, avoids that. MSDN docs also [explicitly recommends](https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-wsagetlasterror#:~:text=If%20a%20function,previously%20failed%20function.) doing it this way --- also, +1 to Ralph's [suggestion](https://git.fish.foo/bitcoin/bitcoin/pulls/37005#author-ralph). the existing test only simulates failures on `Send()`, so we would not catch a regression on the `Receive()` side if the code is changed in an erroneous way, later on
natanleung commented 2026-11-30 03:45:09 +00:00 (Migrated from github.com)

Thank you for the review @winterrdog.

I have addressed Ralph's suggestion and added a Recv() unit test in http_socket_error_tests.

Thank you for the review @winterrdog. I have addressed Ralph's suggestion and added a `Recv()` unit test in `http_socket_error_tests`.
pinheadmz commented 2026-11-30 16:36:57 +00:00 (Migrated from github.com)

Concept ACK but some housekeeping about this PR:

  1. PR Description incorrectly says "no tests modified"
  2. Ideally the test commit would come first to assert current behavior, then change the test with the patch
  3. The same logic (unlock before get error) exists in the P2P version of this code in net.cpp which does not have a unit test -- probably the fix should be applied there as well
  4. Style nit, no whitespace is more common in this codebase ie. {WITH_LOCK...)}; not { WITH_LOCK...) };

In general the PR description reads very LLM-y. Read through the https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md then my suggestion would be, completely re-write the PR description without AI assistance. If you can't, it probably means someone else should take over this PR. It's a pretty simple change and closes a pretty simple issue so I'd rather you learn how to be a valuable contributor.

Concept ACK but some housekeeping about this PR: 1. PR Description incorrectly says "no tests modified" 2. Ideally the test commit would come first to assert current behavior, then change the test with the patch 3. The same logic (unlock before get error) exists in the P2P version of this code in net.cpp which does not have a unit test -- probably the fix should be applied there as well 4. Style nit, no whitespace is more common in this codebase ie. `{WITH_LOCK...)};` not `{ WITH_LOCK...) };` In general the PR description reads very LLM-y. Read through the https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md then my suggestion would be, completely re-write the PR description without AI assistance. If you can't, it probably means someone else should take over this PR. It's a pretty simple change and closes a pretty simple issue so I'd rather you learn how to be a valuable contributor.
winterrdog commented 2026-11-30 21:10:27 +00:00 (Migrated from github.com)

re: https://github.com/bitcoin/bitcoin/pull/37005#issuecomment-7284954117

  1. Ideally the test commit would come first to assert current behavior, then change the test with the patch

this reminded me of this comment: https://github.com/bitcoin/bitcoin/pull/36305#pullrequestreview-5667835205. sometimes it is the reverse, like in that case. so i had assumed it was OK here. the developer notes support your idea, though

re: https://github.com/bitcoin/bitcoin/pull/37005#issuecomment-7284954117 > 2. Ideally the test commit would come first to assert current behavior, then change the test with the patch this reminded me of this comment: https://github.com/bitcoin/bitcoin/pull/36305#pullrequestreview-5667835205. sometimes it is the reverse, like in that case. so i had assumed it was OK here. the developer notes support your [idea](https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md#commit-structure-for-tests), though
winterrdog (Migrated from github.com) reviewed 2026-11-30 21:11:06 +00:00
winterrdog (Migrated from github.com) commented 2026-11-30 21:01:09 +00:00

nit: "once" is a bit ambiguous next to the Send() paragraph. maybe something like:

     * Its first Recv() also returns a recoverable error (WSAEAGAIN) without consuming
     * any request bytes, and after that it always succeeds.

what do you think ?

**nit**: _"once"_ is a bit ambiguous next to the `Send()` paragraph. maybe something like: ```suggestion * Its first Recv() also returns a recoverable error (WSAEAGAIN) without consuming * any request bytes, and after that it always succeeds. ``` --- what do you think ?
natanleung commented 2026-12-01 05:43:39 +00:00 (Migrated from github.com)

@pinheadmz Thank you for the feedback and review. I have updated the PR description accordingly and made the minor whitespace nit fix.

@winterrdog The test comment has been updated. I also agree that this is a test-driven change as the open issue #36973 served as the initial premise for the PR. The developer notes were helpful so that I know for next time.

@pinheadmz Thank you for the feedback and review. I have updated the PR description accordingly and made the minor whitespace nit fix. @winterrdog The test comment has been updated. I also agree that this is a test-driven change as the open issue #36973 served as the initial premise for the PR. The developer notes were helpful so that I know for next time.
maflcko commented 2026-12-01 06:53:49 +00:00 (Migrated from github.com)
> and made the minor whitespace nit fix. Please squash your commits according to https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#squashing-commits
pinheadmz commented 2026-12-01 16:33:12 +00:00 (Migrated from github.com)
Locally confirmed this fix does solve the error described in https://github.com/bitcoin/bitcoin/issues/36973
winterrdog commented 2026-12-01 17:04:50 +00:00 (Migrated from github.com)

from: https://github.com/bitcoin/bitcoin/pull/37005#issuecomment-7284954117:

  1. The same logic (unlock before get error) exists in the P2P version of this code in net.cpp which does not have a unit test -- probably the fix should be applied there as well

shouldn't this be addressed here, as well ?

i think let us do it all here, and backport at once. what do others suggest ?


if other reviewers agree, the PR author can start off with a patch like this:

net.cpp patch
diff --git a/src/net.cpp b/src/net.cpp
index c146f34d52..2f7c83a4ff 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -1648,6 +1648,7 @@ std::pair<size_t, bool> CConnman::SocketSendData(CNode& node) const
         if (expected_more.has_value()) Assume(!data.empty() == *expected_more);
         expected_more = more;
         data_left = !data.empty(); // will be overwritten on next loop if all of data gets sent
+        int nErr = 0;
         int nBytes = 0;
         if (!data.empty()) {
             LOCK(node.m_sock_mutex);
@@ -1664,6 +1665,7 @@ std::pair<size_t, bool> CConnman::SocketSendData(CNode& node) const
             }
 #endif
             nBytes = node.m_sock->Send(data.data(), data.size(), flags);
+            if (nBytes < 0) nErr = WSAGetLastError();
         }
         if (nBytes > 0) {
             node.m_last_send = NodeClock::now();
@@ -1682,7 +1684,6 @@ std::pair<size_t, bool> CConnman::SocketSendData(CNode& node) const
         } else {
             if (nBytes < 0) {
                 // error
-                int nErr = WSAGetLastError();
                 if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS) {
                     LogDebug(BCLog::NET, "socket send error, %s: %s", node.DisconnectMsg(), NetworkErrorString(nErr));
                     node.CloseSocketDisconnect();
@@ -2237,6 +2238,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
         {
             // typical socket buffer is 8K-64K
             uint8_t pchBuf[0x10000];
+            int nErr = 0;
             int nBytes = 0;
             {
                 LOCK(pnode->m_sock_mutex);
@@ -2244,6 +2246,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
                     continue;
                 }
                 nBytes = pnode->m_sock->Recv(pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
+                if (nBytes < 0) nErr = WSAGetLastError();
             }
             if (nBytes > 0)
             {
@@ -2272,7 +2275,6 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
             else if (nBytes < 0)
             {
                 // error
-                int nErr = WSAGetLastError();
                 if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
                 {
                     if (!pnode->fDisconnect) {

i think net_tests.cpp is a good home to add the new tests

from: https://github.com/bitcoin/bitcoin/pull/37005#issuecomment-7284954117: > 3. The same logic (unlock before get error) exists in the P2P version of this code in net.cpp which does not have a unit test -- probably the fix should be applied there as well shouldn't this be addressed here, as well ? i think let us do it all here, and backport at once. what do others suggest ? --- if other reviewers agree, the PR author can start off with a patch like this: <details><summary><code>net.cpp</code> patch</summary> ```diff diff --git a/src/net.cpp b/src/net.cpp index c146f34d52..2f7c83a4ff 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1648,6 +1648,7 @@ std::pair<size_t, bool> CConnman::SocketSendData(CNode& node) const if (expected_more.has_value()) Assume(!data.empty() == *expected_more); expected_more = more; data_left = !data.empty(); // will be overwritten on next loop if all of data gets sent + int nErr = 0; int nBytes = 0; if (!data.empty()) { LOCK(node.m_sock_mutex); @@ -1664,6 +1665,7 @@ std::pair<size_t, bool> CConnman::SocketSendData(CNode& node) const } #endif nBytes = node.m_sock->Send(data.data(), data.size(), flags); + if (nBytes < 0) nErr = WSAGetLastError(); } if (nBytes > 0) { node.m_last_send = NodeClock::now(); @@ -1682,7 +1684,6 @@ std::pair<size_t, bool> CConnman::SocketSendData(CNode& node) const } else { if (nBytes < 0) { // error - int nErr = WSAGetLastError(); if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS) { LogDebug(BCLog::NET, "socket send error, %s: %s", node.DisconnectMsg(), NetworkErrorString(nErr)); node.CloseSocketDisconnect(); @@ -2237,6 +2238,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes, { // typical socket buffer is 8K-64K uint8_t pchBuf[0x10000]; + int nErr = 0; int nBytes = 0; { LOCK(pnode->m_sock_mutex); @@ -2244,6 +2246,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes, continue; } nBytes = pnode->m_sock->Recv(pchBuf, sizeof(pchBuf), MSG_DONTWAIT); + if (nBytes < 0) nErr = WSAGetLastError(); } if (nBytes > 0) { @@ -2272,7 +2275,6 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes, else if (nBytes < 0) { // error - int nErr = WSAGetLastError(); if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS) { if (!pnode->fDisconnect) { ``` </details> i think `net_tests.cpp` is a good home to add the new tests
natanleung commented 2026-12-03 08:36:33 +00:00 (Migrated from github.com)

Thanks for the feedback.

@winterrdog Applied the same fix to P2P connections and added the same Send() and Recv() test cases.

@maflcko Squashed.

Thanks for the feedback. @winterrdog Applied the same fix to P2P connections and added the same `Send()` and `Recv()` test cases. @maflcko Squashed.
DrahtBot commented 2026-12-03 10:16:25 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task 32 bit ARM: https://github.com/bitcoin/bitcoin/actions/runs/47067651345/job/144136562068
LLM reason (✨ experimental): build_fail: Compilation of net_tests.cpp fails because -Werror=overloaded-virtual treats a hidden virtual assignment operator warning as an error.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • Intermittent network issues can occur; re-run CI for now, and report the issue if they keep happening.

  • A functional or integration test failure may be intermittent or unrelated to this pull request.
    If no dedicated issue thread exists, create a new one to track it.

Leave a comment here, if you need help tracking down a confusing failure.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `32 bit ARM`: https://github.com/bitcoin/bitcoin/actions/runs/47067651345/job/144136562068</sub> <sub>LLM reason (✨ experimental): build_fail: Compilation of `net_tests.cpp` fails because `-Werror=overloaded-virtual` treats a hidden virtual assignment operator warning as an error.</sub> <details><summary>Hints</summary> Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example: * Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch. * A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test. * Intermittent network issues can occur; re-run CI for now, and report the issue if they keep happening. * A functional or integration test failure may be intermittent or unrelated to this pull request. If no dedicated issue thread exists, create a new one to track it. Leave a comment here, if you need help tracking down a confusing failure. </details>
winterrdog (Migrated from github.com) reviewed 2026-12-03 23:17:57 +00:00
winterrdog (Migrated from github.com) left a comment

the PR title needs an update that includes net too, e.g. net, http: Preserve socket errors before unlocking. same goes for the PR description, since the P2P server now gets the same fix as the HTTP server


side note: when i run .\build\bin\test_bitcoin.exe --run_test=net_tests locally, it results in:

Running 22 test cases...
Assertion failed: nThreadsServicingQueue == 0, file ./scheduler.cpp, line 18

i doubt it has sth to do with this PR because the assertion failure happens on master too. anyone with an idea as to why it happens ?

the PR title needs an update that includes `net` too, e.g. `net, http: Preserve socket errors before unlocking`. same goes for the PR description, since the P2P server now gets the same fix as the HTTP server --- _side note_: when i run `.\build\bin\test_bitcoin.exe --run_test=net_tests` locally, it results in: ```text Running 22 test cases... Assertion failed: nThreadsServicingQueue == 0, file ./scheduler.cpp, line 18 ``` i doubt it has sth to do with this PR because the assertion failure happens on `master` too. anyone with an idea as to why it happens ?
winterrdog (Migrated from github.com) commented 2026-12-03 23:11:36 +00:00

could you add a short doc comment atop ErrorSock, like the one in httpserver_tests.cpp ?

it would help future readers see why the first Send()/Recv() fails

could you add a short doc comment atop `ErrorSock`, like the one in `httpserver_tests.cpp` ? it would help future readers see why the first `Send()`/`Recv()` fails
winterrdog (Migrated from github.com) commented 2026-12-03 23:10:30 +00:00

nit: > 0 only proves the counter moved, not that the bytes actually reached the socket. IMO, checking the pipe against the counter is stronger:

diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp
index 5b946ddbb4..77841e8237 100644
--- a/src/test/net_tests.cpp
+++ b/src/test/net_tests.cpp
@@ -1911,7 +1911,9 @@ BOOST_AUTO_TEST_CASE(socket_error_tests)
     // Second send succeeds on retry.
     connman.SocketHandlerPublic();
     BOOST_REQUIRE(!node.fDisconnect);
-    BOOST_REQUIRE_GT(WITH_LOCK(node.cs_vSend, return node.nSendBytes), 0U);
+    char sent_wire_bytes[32];
+    const ssize_t bytes_in_pipe{pipes->send.GetBytes(sent_wire_bytes, sizeof(sent_wire_bytes))};
+    BOOST_CHECK_EQUAL(static_cast<uint64_t>(bytes_in_pipe), WITH_LOCK(node.cs_vSend, return node.nSendBytes));

     // Queue the same test message for receiving via the socket.
     auto message{NetMsg::Make(NetMsgType::VERACK)};
**nit**: `> 0` only proves the counter moved, not that the bytes actually reached the socket. IMO, checking the pipe against the counter is stronger: ```diff diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp index 5b946ddbb4..77841e8237 100644 --- a/src/test/net_tests.cpp +++ b/src/test/net_tests.cpp @@ -1911,7 +1911,9 @@ BOOST_AUTO_TEST_CASE(socket_error_tests) // Second send succeeds on retry. connman.SocketHandlerPublic(); BOOST_REQUIRE(!node.fDisconnect); - BOOST_REQUIRE_GT(WITH_LOCK(node.cs_vSend, return node.nSendBytes), 0U); + char sent_wire_bytes[32]; + const ssize_t bytes_in_pipe{pipes->send.GetBytes(sent_wire_bytes, sizeof(sent_wire_bytes))}; + BOOST_CHECK_EQUAL(static_cast<uint64_t>(bytes_in_pipe), WITH_LOCK(node.cs_vSend, return node.nSendBytes)); // Queue the same test message for receiving via the socket. auto message{NetMsg::Make(NetMsgType::VERACK)}; ```
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!37005
No description provided.