Repository navigation
http: Preserve socket errors before unlocking - #37005
natanleung wants to merge 1 commit into
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, 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. |
There was a problem hiding this comment.
approach ACK
WSAGetLastError() reads from a thread-local slot, so the http server code needs to read the error before anything else runs on the same thread, otherwise it could get overwritten, just like it happens here. in this case, releasing the lock, m_sock_mutex, does exactly that in debug builds. as a result, a harmless "try again" error (e.g. WSAEWOULDBLOCK) 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 recommend doing it this way
also, +1 to Ralph's suggestion, given these new findings. the existing unit tests 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
|
Thank you for the review @winterrdog. I have addressed Ralph's suggestion and added a |
|
Concept ACK but some housekeeping about this PR:
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. |
|
re: #37005 (comment)
this reminded me of this comment: #36305 (review). sometimes it is the reverse, like in that case. so i had assumed it was OK here. the developer notes support your idea, though |
| * After it sends successfully once, it continues to always succeed. | ||
| * | ||
| * Useful for testing "try again" logic around non-blocking socket Send() failures. | ||
| * Recv() returns a recoverable error (WSAEAGAIN) once, without consuming any request bytes. |
There was a problem hiding this comment.
nit: "once" is a bit ambiguous next to the Send() paragraph. maybe something like:
| * Recv() returns a recoverable error (WSAEAGAIN) once, without consuming any request bytes. | |
| * Its first Recv() also returns a recoverable error (WSAEAGAIN) without consuming | |
| * any request bytes, and after that it always succeeds. |
what do you think ?
|
@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. |
Please squash your commits according to https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#squashing-commits |
|
Locally confirmed this fix does solve the error described in #36973 |
|
from: #37005 (comment):
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:
|
55d2fcc to
3b8c543
Compare
|
Thanks for the feedback. @winterrdog Applied the same fix to P2P connections and added the same @maflcko Squashed. |
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
3b8c543 to
0409d2e
Compare
winterrdog
left a comment
There was a problem hiding this 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 ?
| // Second send succeeds on retry. | ||
| connman.SocketHandlerPublic(); | ||
| BOOST_REQUIRE(!node.fDisconnect); | ||
| BOOST_REQUIRE_GT(WITH_LOCK(node.cs_vSend, return node.nSendBytes), 0U); |
There was a problem hiding this comment.
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)};|
|
||
| BOOST_AUTO_TEST_CASE(socket_error_tests) | ||
| { | ||
| class ErrorSock : public DynSock |
There was a problem hiding this comment.
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

Fixes #36973.
Background
For Windows MinGW build with
DEBUG_LOCKORDER, releasingm_sock_mutexcallsstd::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_teststest case mockSend()fails on the first call, then succeed on the next call. By clearing the errorWSAEWOULDBLOCK, the test server does not retry and instead disconnects, causing the test failure.Changes
WSAGetLastError()call into the critical sections for socket pathsSend()andRecv()Recv()inhttpserver_tests/http_socket_error_testsRelated work
identified in Unit tests on Windows with
DEBUG_LOCKORDERhave been broken for a while #36973.32.xwas requested in the discussion on Unit tests on Windows withDEBUG_LOCKORDERhave been broken for a while #36973. This patchtargets
master; the backport would be separate.