cli: respect -rpcclienttimeout even when sending #36676

Manually merged
darosior merged 1 commit from 2610_cli_socket_error_fix into master 2026-10-28 15:49:52 +00:00 AGit
darosior commented 2026-10-27 16:38:47 +00:00 (Migrated from github.com)

The move away from libevent introduced a small regression in this regard. In #36299, we switched the timeout to apply per socket wait, but continued on to another socket wait in case of timeout, instead of returning. See commit message for details.

In general, we also shouldn't ignore Sock::Wait's errors and timeouts by silently retrying forever.

Fix these issues by returning instead when Sock::Wait times out or returns an error. Tentative bot functional test for this in second commit that appears to be robust enough.

The move away from libevent introduced a small regression in this regard. In #36299, we switched the timeout to apply per socket wait, but continued on to another socket wait in case of timeout, instead of returning. See commit message for details. In general, we also shouldn't ignore `Sock::Wait`'s errors and timeouts by silently retrying forever. Fix these issues by returning instead when `Sock::Wait` times out or returns an error. ~~Tentative bot functional test for this in second commit that appears to be robust enough.~~
DrahtBot commented 2026-10-27 16:38:59 +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
ACK jpk68, l0rinc, pinheadmz

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

<!--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/36676 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36676.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36676#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 | | ---- | --------- | | ACK | [jpk68](https://github.com/bitcoin/bitcoin/pull/36676#issuecomment-6452954493), [l0rinc](https://github.com/bitcoin/bitcoin/pull/36676#issuecomment-6453756956), [pinheadmz](https://github.com/bitcoin/bitcoin/pull/36676#pullrequestreview-5684988035) | 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. <!--5faf32d7da4f0f540f40219e4f7537a3-->
darosior commented 2026-10-27 17:18:03 +00:00 (Migrated from github.com)

Tentative bot functional test for this in second commit that appears to be robust enough.

Well, scratch that. It's flaky on Windows. I'm dropping it from this PR: reviewers can easily bot-implement one locally to sanity check the behaviour. The only robust alternative would be a unit test, but would require some refactoring. That's overkill for this PR's trivial fix to the CLI.

> Tentative bot functional test for this in second commit that appears to be robust enough. Well, scratch that. It's flaky on Windows. I'm dropping it from this PR: reviewers can easily bot-implement one locally to sanity check the behaviour. The only robust alternative would be a unit test, but would require some refactoring. That's overkill for this PR's trivial fix to the CLI.
First-time contributor

2 previous suggestions


Previous suggestions (2)
[Suggestion · still present] Send timeouts are reported as generic send failures

When -rpcclienttimeout expires during sending, the CLI does not identify the timeout, unlike when the same timeout occurs during receiving.

When Sock::Wait() times out, it returns with occurred set to 0. SendRequest() then returns false, and Post() reports Failed to send HTTP request for every false return. The resulting CLI message does not point to -rpcclienttimeout as the cause or suggest raising it. By contrast, a receive timeout is reported as (timeout). Throw CConnectionFailed{"timeout"} or a send-specific timeout message when the wait returns with no events. Keep the generic failure for error events and permanent send errors. Both paths still raise CConnectionFailed, so -rpcwait retry behavior is unchanged.

Location: src/bitcoin-cli.cpp:917 (head).

First reported at e5d9a7e04b50c723aeb65984e5048ffd098f6737. Original review. Last checked at e5d9a7e04b50c723aeb65984e5048ffd098f6737.

[Suggestion · still present] No test covers the stalled-send timeout path

Restoring the old continue would leave the tests unable to catch a return of the indefinite hang.

The raw-socket test sends a response and then calls recv(), so the client's small request does not block and the test does not reach the branch where the wait ends without SendEvent. Add a test that creates backpressure: for example, have a listener set a small SO_RCVBUF before listening, accept the connection, and never read from it. Run bitcoin-cli -rpcclienttimeout=1 with a request large enough to fill the loopback buffers, such as a multi-megabyte argument passed through -stdin to echo, and assert that the command fails rather than hanging until the harness timeout. A small request fits in the kernel send buffer and only exercises the receive timeout. Since backpressure was unreliable on Windows in an earlier attempt, the test may need to be skipped on platforms where it cannot be produced reliably.

Location: src/bitcoin-cli.cpp:917 (head).

First reported at e5d9a7e04b50c723aeb65984e5048ffd098f6737. Original review. Last checked at e5d9a7e04b50c723aeb65984e5048ffd098f6737. The author plans to address this separately. Author reply 2026-10-27T17:18:03Z

Review coverage was incomplete.

Review limitations
  • verifier: Static review only. Whether the suggested backpressure test runs reliably (given loopback buffer autotuning and Windows behavior) cannot be settled without running it.

Base: b1172114df07 · Head: e5d9a7e04b50 · Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:e2fe968a37ae1ddd4f6c9a1c838e2c7e987936ced6bc079676c4696454ebe54c:350e91124b6e3dbe432b7e1ad4e61783f45b932fec5afb7cd7cb3a0800488b1b --> <!-- ralph-base:b1172114df0740f3252e26b8cd71e256da3c2d70 --> <!-- ralph-head:e5d9a7e04b50c723aeb65984e5048ffd098f6737 --> <!-- ralph-case:0a220f5cf201ce056ec223e4624b36b35a8530598181726538fb80bac6d35ee3 --> 2 previous suggestions --- <details> <summary>Previous suggestions (2)</summary> <details> <summary>[Suggestion · still present] Send timeouts are reported as generic send failures</summary> When `-rpcclienttimeout` expires during sending, the CLI does not identify the timeout, unlike when the same timeout occurs during receiving. When `Sock::Wait()` times out, it returns with `occurred` set to 0. `SendRequest()` then returns false, and `Post()` reports `Failed to send HTTP request` for every false return. The resulting CLI message does not point to `-rpcclienttimeout` as the cause or suggest raising it. By contrast, a receive timeout is reported as `(timeout)`. Throw `CConnectionFailed{"timeout"}` or a send-specific timeout message when the wait returns with no events. Keep the generic failure for error events and permanent send errors. Both paths still raise `CConnectionFailed`, so `-rpcwait` retry behavior is unchanged. Location: [`src/bitcoin-cli.cpp:917`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/e5d9a7e04b50c723aeb65984e5048ffd098f6737/src/bitcoin-cli.cpp#L917>) (head). First reported at `e5d9a7e04b50c723aeb65984e5048ffd098f6737`. [Original review](https://review.fish.foo/traces/36676-e5d9a7e04b50c723aeb65984e5048ffd098f6737-1507-2.html). Last checked at `e5d9a7e04b50c723aeb65984e5048ffd098f6737`. </details> <details> <summary>[Suggestion · still present] No test covers the stalled-send timeout path</summary> Restoring the old `continue` would leave the tests unable to catch a return of the indefinite hang. The raw-socket test sends a response and then calls `recv()`, so the client's small request does not block and the test does not reach the branch where the wait ends without `SendEvent`. Add a test that creates backpressure: for example, have a listener set a small `SO_RCVBUF` before listening, accept the connection, and never read from it. Run `bitcoin-cli -rpcclienttimeout=1` with a request large enough to fill the loopback buffers, such as a multi-megabyte argument passed through `-stdin` to `echo`, and assert that the command fails rather than hanging until the harness timeout. A small request fits in the kernel send buffer and only exercises the receive timeout. Since backpressure was unreliable on Windows in an earlier attempt, the test may need to be skipped on platforms where it cannot be produced reliably. Location: [`src/bitcoin-cli.cpp:917`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/e5d9a7e04b50c723aeb65984e5048ffd098f6737/src/bitcoin-cli.cpp#L917>) (head). First reported at `e5d9a7e04b50c723aeb65984e5048ffd098f6737`. [Original review](https://review.fish.foo/traces/36676-e5d9a7e04b50c723aeb65984e5048ffd098f6737-1507-2.html). Last checked at `e5d9a7e04b50c723aeb65984e5048ffd098f6737`. The author plans to address this separately. [Author reply](https://github.com/bitcoin/bitcoin/pull/36676#issuecomment-6448602797) 2026-10-27T17:18:03Z </details> </details> Review coverage was incomplete. ##### Review limitations - verifier: Static review only. Whether the suggested backpressure test runs reliably (given loopback buffer autotuning and Windows behavior) cannot be settled without running it. <!-- Review tier: sensitive --> --- Base: [`b1172114df07`](<https://git.fish.foo/bitcoin/bitcoin/commit/b1172114df0740f3252e26b8cd71e256da3c2d70>) · Head: [`e5d9a7e04b50`](<https://git.fish.foo/bitcoin/bitcoin/commit/e5d9a7e04b50c723aeb65984e5048ffd098f6737>) · [Full review report](<https://review.fish.foo/traces/36676-e5d9a7e04b50c723aeb65984e5048ffd098f6737-1594-4.html>)
jpk68 commented 2026-10-27 20:36:37 +00:00 (Migrated from github.com)
ACK e5d9a7e04b50c723aeb65984e5048ffd098f6737
l0rinc commented 2026-10-27 21:15:46 +00:00 (Migrated from github.com)

tested ACK e5d9a7e04b

I also had it in my backlog (came up a few weeks ago when I was reviewing a different change - cc: @maflcko).
Tested locally that a stalled upload reports a send failure and that the provided fix is correct.

tested ACK e5d9a7e04b50c723aeb65984e5048ffd098f6737 I also had it in my backlog (came up a few weeks ago when I was reviewing a different change - cc: @maflcko). Tested locally that a stalled upload reports a send failure and that the provided fix is correct.
pinheadmz (Migrated from github.com) approved these changes 2026-10-28 15:12:43 +00:00
pinheadmz (Migrated from github.com) left a comment

ACK e5d9a7e04b

Built and tested on macos/arm64.

The undesired behavior is not just an ordinary timeout while waiting for a server reply, but a timeout while waiting for the socket to be writable on the client end. Even if no server exists at all, the client's kernel's TCP stack may still happily receive bytes with no where to send them.

To reproduce I ran a trivial accept-and-do-not-read server and made bitcoin-cli connections to it. The only way to garuntee the "wait forever" behavior is to send an excessive amount of data with bitcoin-cli and trigger TCP backpressure so the socket actually becomes not writable.

On master, in that case, we continue to wait forever. With this patch, we quit with (Failed to send HTTP request).

One thing thats not exactly clear in the PR description or commit message is that a timeout is NOT treated as an error. select() and poll() return 0 on timeout, and so Wait() and WaitMany() still return true. On master, we already quit on error. This PR also quits on timeout.

Show Signature
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

ACK e5d9a7e04b50c723aeb65984e5048ffd098f6737
-----BEGIN PGP SIGNATURE-----

iQJPBAEBCAA5FiEE5hdzzW4BBA4vG9eM5+KYS2KJyToFAmriD40bFIAAAAAABAAO
bWFudTIsMi41KzEuMTIsMCwzAAoJEOfimEtiick67R8P/2nB0EV+mZEP0XTYb0VK
6IyxFDmrv1/NS37TNO84KjEM2L3Ff60+8pQZ/rCZ/ZMIS7BOZIb9dSEJb2OH2kvW
W8I8anYFjsKYy++ZN8fn4iQMlkYp1MjhGt2RzVPx4tYJpcTjCJmM+tstb2WHikvl
W/vnzmHfJpl3dX29A5q9d+cf6Ks4CN1hWGQfAJtiBIfD+snYzaeiRV5aI75fPb6N
g38jlkiBulIOPuVd1LbdxgyY0oT4L9///1qCFHMyHJhva9NAfWFaIzvnVD1dCZPw
ocSyhiBvITVLMXzxm86eKL1CX3vfg+z6nB1Q5jMNhmTOs13bTWrJGI40WcFo1G5C
HVeqciuDIByDSAIr78hrLpTpH5+itgZQRWM9tTgcI6o/OTodF8ejsexynOGedCtS
aYfnt6KjBk2T+x5BN5u0jbsswqoEuLfiLwEKwQc0DCLDncQAHOfUn90FiAjcEP86
go9oCA92i9TBaLHruKXdqthLyIRTEHacG117rcRTShd9tysyeci3I2LeoNzwTwNx
XOjD6ctuB0xOs2rOLTVQnKuF3D+9heCkuSu+Vh2zT8A3LRC53oF5+5PDcZW+fELN
VX+dqBKWMUaBSaJD1e84i94I9GdJ2mfeDYkR5j1GZ+r7imkQAZP9C+SjzgojaXBv
95ycWHMvmPgo+jEZPFTI/nQX
=ll+o
-----END PGP SIGNATURE-----

pinheadmz's public key is on openpgp.org

ACK e5d9a7e04b50c723aeb65984e5048ffd098f6737 Built and tested on macos/arm64. The undesired behavior is not just an ordinary timeout while waiting for a server reply, but a timeout while waiting for the socket to be writable on the client end. Even if no server exists at all, the client's kernel's TCP stack may still happily receive bytes with no where to send them. To reproduce I ran a trivial accept-and-do-not-read server and made bitcoin-cli connections to it. The only way to garuntee the "wait forever" behavior is to send an excessive amount of data with bitcoin-cli and trigger TCP backpressure so the socket actually becomes not writable. On master, in that case, we `continue` to wait forever. With this patch, we quit with `(Failed to send HTTP request)`. One thing thats not exactly clear in the PR description or commit message is that a timeout is NOT treated as an error. `select()` and `poll()` return `0` on timeout, and so `Wait()` and `WaitMany()` still return `true`. On master, we already quit on error. This PR also quits on timeout. <details><summary>Show Signature</summary> ``` -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 ACK e5d9a7e04b50c723aeb65984e5048ffd098f6737 -----BEGIN PGP SIGNATURE----- iQJPBAEBCAA5FiEE5hdzzW4BBA4vG9eM5+KYS2KJyToFAmriD40bFIAAAAAABAAO bWFudTIsMi41KzEuMTIsMCwzAAoJEOfimEtiick67R8P/2nB0EV+mZEP0XTYb0VK 6IyxFDmrv1/NS37TNO84KjEM2L3Ff60+8pQZ/rCZ/ZMIS7BOZIb9dSEJb2OH2kvW W8I8anYFjsKYy++ZN8fn4iQMlkYp1MjhGt2RzVPx4tYJpcTjCJmM+tstb2WHikvl W/vnzmHfJpl3dX29A5q9d+cf6Ks4CN1hWGQfAJtiBIfD+snYzaeiRV5aI75fPb6N g38jlkiBulIOPuVd1LbdxgyY0oT4L9///1qCFHMyHJhva9NAfWFaIzvnVD1dCZPw ocSyhiBvITVLMXzxm86eKL1CX3vfg+z6nB1Q5jMNhmTOs13bTWrJGI40WcFo1G5C HVeqciuDIByDSAIr78hrLpTpH5+itgZQRWM9tTgcI6o/OTodF8ejsexynOGedCtS aYfnt6KjBk2T+x5BN5u0jbsswqoEuLfiLwEKwQc0DCLDncQAHOfUn90FiAjcEP86 go9oCA92i9TBaLHruKXdqthLyIRTEHacG117rcRTShd9tysyeci3I2LeoNzwTwNx XOjD6ctuB0xOs2rOLTVQnKuF3D+9heCkuSu+Vh2zT8A3LRC53oF5+5PDcZW+fELN VX+dqBKWMUaBSaJD1e84i94I9GdJ2mfeDYkR5j1GZ+r7imkQAZP9C+SjzgojaXBv 95ycWHMvmPgo+jEZPFTI/nQX =ll+o -----END PGP SIGNATURE----- ``` pinheadmz's public key is [on openpgp.org](https://keys.openpgp.org/vks/v1/by-fingerprint/E61773CD6E01040E2F1BD78CE7E2984B6289C93A) </details>
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!36676
No description provided.