http: Improve HTTPRemoteClient::MaybeDisconnect() #36159

Manually merged
hodlinator merged 5 commits from 2026/09/should_disconnect into master 2026-10-19 08:23:38 +00:00 AGit
hodlinator commented 2026-09-03 10:41:02 +00:00 (Migrated from github.com)

Issues

#35182 had 1 case where we flag a client for disconnect where we do not log the reason.

#35829 introduced the somewhat poorly named non-const HTTPRemoteClient::MaybeDisconnect(). It also lacks testing.

The control-flow in MaybeDisconnect()/ShouldDisconnect() is somewhat complex. https://github.com/bitcoin/bitcoin/pull/35829#discussion_r3846342291

Commits solving these issues in same order

  • http: Add log message for remaining case where we set HTTPRemoteClient::m_disconnect = true
  • refactor: Rename MaybeDisconnect() to ShouldDisconnect() and make it const
  • refactor: Enable mocking the time around ShouldDisconnect()
  • test: Add test for behavior of ShouldDisconnect()
  • refactor: Flatten control flow in ShouldDisconnect()
### Issues #35182 had 1 case where we flag a client for disconnect where we do not log the reason. #35829 introduced the somewhat poorly named non-`const` `HTTPRemoteClient::MaybeDisconnect()`. It also lacks testing. The control-flow in `MaybeDisconnect()/ShouldDisconnect()` is somewhat complex. https://github.com/bitcoin/bitcoin/pull/35829#discussion_r3846342291 ### Commits solving these issues in same order * http: Add log message for remaining case where we set `HTTPRemoteClient::m_disconnect = true` * refactor: Rename `MaybeDisconnect()` to `ShouldDisconnect()` and make it `const` * refactor: Enable mocking the time around `ShouldDisconnect()` * test: Add test for behavior of `ShouldDisconnect()` * refactor: Flatten control flow in `ShouldDisconnect()`
DrahtBot commented 2026-09-03 10:41:09 +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 pinheadmz, winterrdog, sedited
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:

  • #36204 (http: disconnect clients that never finish a request by janb84)
  • #36074 (scripted-diff: [test] Add util/check.h includes for assertions by maflcko)
  • #35713 (Remove boost as a unit test runner by rustaceanrob)

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.

LLM Linter (✨ experimental)

Possible typos and grammar issues:

  • src/test/httpserver_tests.cpp: claiming its able to send everything → claiming it's able to send everything [typo: its should be it's]

2026-10-06 19:40:23

<!--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/36159 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36159.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36159#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 | [pinheadmz](https://github.com/bitcoin/bitcoin/pull/36159#pullrequestreview-5513688107), [winterrdog](https://github.com/bitcoin/bitcoin/pull/36159#pullrequestreview-5520066826), [sedited](https://github.com/bitcoin/bitcoin/pull/36159#pullrequestreview-5568681599) | | Concept ACK | [jeanpablojp](https://github.com/bitcoin/bitcoin/pull/36159#pullrequestreview-5114081037) | 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: * [#36204](https://github.com/bitcoin/bitcoin/pull/36204) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36204.svg"></sub> (http: disconnect clients that never finish a request by janb84) * [#36074](https://github.com/bitcoin/bitcoin/pull/36074) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36074.svg"></sub> (scripted-diff: [test] Add util/check.h includes for assertions by maflcko) * [#35713](https://github.com/bitcoin/bitcoin/pull/35713) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35713.svg"></sub> (Remove boost as a unit test runner by rustaceanrob) 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--> ### LLM Linter (✨ experimental) Possible typos and grammar issues: - `src/test/httpserver_tests.cpp`: `claiming its able to send everything` → `claiming it's able to send everything` [typo: `its` should be `it's`] <sup>2026-10-06 19:40:23</sup>
jeanpablojp (Migrated from github.com) reviewed 2026-09-04 14:19:11 +00:00
jeanpablojp (Migrated from github.com) left a comment

Concept ACK

Built the four commits and mutation-tested the changed branches.

now is a whole second, taken before the clients exist and compared against an m_idle_since they stamp later, so the checks expecting a disconnect at now + timeout + 1s only hold while both fall in the same second. About 0.6ms sits in between on busy_slow, and a rollover there makes the comparison 30s > 30s. Forced one to confirm. Worth taking now per block, after WriteReply() for busy_slow?

Concept ACK Built the four commits and mutation-tested the changed branches. `now` is a whole second, taken before the clients exist and compared against an `m_idle_since` they stamp later, so the checks expecting a disconnect at `now + timeout + 1s` only hold while both fall in the same second. About 0.6ms sits in between on `busy_slow`, and a rollover there makes the comparison `30s > 30s`. Forced one to confirm. Worth taking `now` per block, after `WriteReply()` for `busy_slow`?
jeanpablojp (Migrated from github.com) commented 2026-09-04 14:19:11 +00:00

This also stops the idle line being logged when the client is already flagged. Before, is_idle was worked out first and logged inside if (m_disconnect || is_idle), so a client already flagged and past the timeout got both lines. Looks like an improvement, since that line was claiming a timeout for a client leaving for another reason. Worth a mention in the commit message, since it says refactor:?

This also stops the idle line being logged when the client is already flagged. Before, `is_idle` was worked out first and logged inside `if (m_disconnect || is_idle)`, so a client already flagged and past the timeout got both lines. Looks like an improvement, since that line was claiming a timeout for a client leaving for another reason. Worth a mention in the commit message, since it says `refactor:`?
jeanpablojp (Migrated from github.com) commented 2026-09-04 14:19:12 +00:00

A connection that is accepted and never sends anything keeps m_connection_busy at its initial true, so the disconnect_all branch never picks it up. Inside ShouldDisconnect() only the idle timeout is left, and with -rpcservertimeout=0 there is no idle timeout either. On regtest shutdown goes from 0.7s to 30.7s and ends in a Force-disconnecting warning. At the default of 30s that timeout and StopHTTPServer()'s own deadline are the same number. Releasing them at once would give up the 503 a client can still get today by sending after stop. Is the wait deliberate?

A connection that is accepted and never sends anything keeps `m_connection_busy` at its initial `true`, so the `disconnect_all` branch never picks it up. Inside `ShouldDisconnect()` only the idle timeout is left, and with `-rpcservertimeout=0` there is no idle timeout either. On regtest shutdown goes from 0.7s to 30.7s and ends in a `Force-disconnecting` warning. At the default of 30s that timeout and `StopHTTPServer()`'s own deadline are the same number. Releasing them at once would give up the `503` a client can still get today by sending after `stop`. Is the wait deliberate?
jeanpablojp (Migrated from github.com) commented 2026-09-04 14:19:11 +00:00

ZeroSock::Send() already returns len, so could this just be a ZeroSock?

        // Allow disconnecting directly after WriteReply() when it is able to
        // complete the optimistic send of the full response due to the ZeroSock.
        auto busy_fast{std::make_shared<Client>(std::make_unique<ZeroSock>())};
`ZeroSock::Send()` already returns `len`, so could this just be a `ZeroSock`? ```suggestion // Allow disconnecting directly after WriteReply() when it is able to // complete the optimistic send of the full response due to the ZeroSock. auto busy_fast{std::make_shared<Client>(std::make_unique<ZeroSock>())}; ```
jeanpablojp (Migrated from github.com) commented 2026-09-04 14:19:12 +00:00

Every client here is HTTP/1.0, so m_keep_alive is never true and the branch for a healthy persistent connection is only ever asserted not firing. Replacing that condition with false leaves this test green. Worth one more client?

    {
        // A keep-alive client whose reply is out is neither flagged nor idle,
        // so the shutdown branch is the only thing that can release it.
        auto keepalive{std::make_shared<Client>(std::make_unique<ZeroSock>())};
        keepalive->MutateRecvBuffer().append("GET / HTTP/1.1\nHost: 127.0.0.1\n\n");
        {
            std::unique_ptr request{HTTPRemoteClient::TryReadRequest(keepalive)};
            BOOST_REQUIRE(request);
            request->WriteReply(HTTP_OK);
        }
        const auto now{Now<SteadySeconds>()};
        BOOST_CHECK(!keepalive->ShouldDisconnect(now, timeout, /*disconnect_all=*/false));
        BOOST_CHECK( keepalive->ShouldDisconnect(now, timeout, /*disconnect_all=*/true));
    }

    {
        // A malformed request flags the client, and that outranks the rest.
Every client here is `HTTP/1.0`, so `m_keep_alive` is never true and the branch for a healthy persistent connection is only ever asserted not firing. Replacing that condition with `false` leaves this test green. Worth one more client? ```suggestion { // A keep-alive client whose reply is out is neither flagged nor idle, // so the shutdown branch is the only thing that can release it. auto keepalive{std::make_shared<Client>(std::make_unique<ZeroSock>())}; keepalive->MutateRecvBuffer().append("GET / HTTP/1.1\nHost: 127.0.0.1\n\n"); { std::unique_ptr request{HTTPRemoteClient::TryReadRequest(keepalive)}; BOOST_REQUIRE(request); request->WriteReply(HTTP_OK); } const auto now{Now<SteadySeconds>()}; BOOST_CHECK(!keepalive->ShouldDisconnect(now, timeout, /*disconnect_all=*/false)); BOOST_CHECK( keepalive->ShouldDisconnect(now, timeout, /*disconnect_all=*/true)); } { // A malformed request flags the client, and that outranks the rest. ```
hodlinator (Migrated from github.com) reviewed 2026-09-05 20:39:43 +00:00
hodlinator (Migrated from github.com) left a comment

Thanks for the feedback @jeanpablojp (https://github.com/bitcoin/bitcoin/pull/36159#pullrequestreview-5114081037)!

Addressed all of it to some extent in the latest push (352fdc2797) + inline comment. Opted for mocking the clock and freeze it rather than trying to keep up with the current time of the actual clock as it drifts throughout the added test case.

Thanks for the feedback @jeanpablojp (https://github.com/bitcoin/bitcoin/pull/36159#pullrequestreview-5114081037)! Addressed all of it to some extent in the latest push (352fdc279751694348c6b406322de5582f59ad49) + inline comment. Opted for mocking the clock and freeze it rather than trying to keep up with the current time of the actual clock as it drifts throughout the added test case.
hodlinator (Migrated from github.com) commented 2026-09-05 19:34:28 +00:00

re https://github.com/bitcoin/bitcoin/pull/36159#discussion_r3934900182:
You are pointing out behavior on master, right?
Maybe we could disconnect clients quicker if they just open the connection and don't send anything, within 5 seconds or so? Not sure I want to increase the complexity of this PR though. That would probably introduce another timeout-setting.

re https://github.com/bitcoin/bitcoin/pull/36159#discussion_r3934900182: You are pointing out behavior on master, right? Maybe we could disconnect clients quicker if they just open the connection and don't send anything, within 5 seconds or so? Not sure I want to increase the complexity of this PR though. That would probably introduce another timeout-setting.
DrahtBot commented 2026-09-05 21:31:27 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task MSan: https://github.com/bitcoin/bitcoin/actions/runs/33990407720/job/101371509013
LLM reason (✨ experimental): CI failed during the build link step (bitcoin-wallet) due to a ld.bfd error: “bad reloc symbol index … in section .debug_addr,” causing the final link to fail.

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.

  • An intermittent issue.

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

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `MSan`: https://github.com/bitcoin/bitcoin/actions/runs/33990407720/job/101371509013</sub> <sub>LLM reason (✨ experimental): CI failed during the build link step (`bitcoin-wallet`) due to a `ld.bfd` error: “bad reloc symbol index … in section `.debug_addr`,” causing the final link to fail.</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. * An intermittent issue. Leave a comment here, if you need help tracking down a confusing failure. </details>
jeanpablojp (Migrated from github.com) reviewed 2026-09-05 22:27:13 +00:00
jeanpablojp (Migrated from github.com) commented 2026-09-05 22:27:13 +00:00

Yes, master, but also measured at the merge-base and on today's master, the same both times, under a second to shut down with no such connection open and 30.7s with one at -rpcservertimeout=0.

The slots are the sharper half. With -rpcmaxconnections=4 and four sockets that connect and send nothing, a normal call has to wait for the idle timeout to free one, and -rpcservertimeout=0 means nothing frees them. Agreed it does not belong here though, and your five seconds sounds like the right direction. Happy to open a follow-up.

Yes, master, but also measured at the merge-base and on today's master, the same both times, under a second to shut down with no such connection open and 30.7s with one at `-rpcservertimeout=0`. The slots are the sharper half. With `-rpcmaxconnections=4` and four sockets that connect and send nothing, a normal call has to wait for the idle timeout to free one, and `-rpcservertimeout=0` means nothing frees them. Agreed it does not belong here though, and your five seconds sounds like the right direction. Happy to open a follow-up.
winterrdog commented 2026-09-06 00:19:24 +00:00 (Migrated from github.com)

tACK 352fdc2797

successfully built and tested on this toolchain: Debian/clang++ 18/x86_64.


P.S: the CI failure is completely independent of the changes introduced by this PR

tACK 352fdc279751694348c6b406322de5582f59ad49 successfully built and tested on this toolchain: Debian/`clang++ 18`/`x86_64`. --- P.S: the CI failure is completely independent of the changes introduced by this PR
pinheadmz (Migrated from github.com) approved these changes 2026-09-24 17:38:05 +00:00
pinheadmz (Migrated from github.com) left a comment

ACK 352fdc2797

Built and tested on macos/arm64. Reviewed the changes and checked with both meatball brain and LLM that the refactor didn't change behavior. Great new text coverage helps with that a lot as well. Left a few non blocking suggestions.

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

ACK 352fdc279751694348c6b406322de5582f59ad49
-----BEGIN PGP SIGNATURE-----

iQJPBAEBCAA5FiEE5hdzzW4BBA4vG9eM5+KYS2KJyToFAmq1X5EbFIAAAAAABAAO
bWFudTIsMi41KzEuMTIsMCwzAAoJEOfimEtiick6nf8QALaqDEDS8I6yCKfKkJ9L
xEpZ46pQu9Ip04KhRfa2oHzplxr7Zc+0UQC0IiPqfgsVOOg0lH9jrzIgttdyEa/Z
xWnfpxjW/BKKrBGpdp9DcDeNZqQ36IknKMvDDLCtSv677XcxysQqxHNDUh9XNmyy
CgGJkCKF0y3kzPs/Zu/IUZxLEzdE6Uzr+fz4PphwbO8ewAqygDRa9Sl2a9m7Dp5W
50UG6exRmMsrjWZF+oO1CWHvfEC5OTJPIK4u+BGpCuTjZzGvCGrncnbTyrxDlZBh
rE5Tk+/mmWjlE1NMoNf3quTjYDb0TwEp+jdNoaHEn3ivHgWig33H/4Y5yXxZh4Y4
5jhMzekBo7P2NcsTkuin3g72V/ZxYWbJbC9y3Vxs86mbBwqVDmGiHH1HL9JRI0Kt
DiM1F+VGMx0kc/ZrzY64397CwuwZAuYPFlM7aVMsQlLtqlex6JiY7lAYFuTttm5R
u7OHtCVFZ3RFkR/M+LZm2GwLiMQN4GVfdIfO5bEQCfXMoVyLvzZ8r6kb8s1uwgvE
DSmvwnOgEizVbp3uo3GmaFItpUxbY2bG8dkx37VPwyWBpU2TuJ1Y4S2AsmcCRriV
ccDrOqY45LV7E19y3alKNLM7mwQVcw27TyIvT/XU4Rs63dcNOneRQ8at4mCB1WM/
JZzZqJl1uPRsV77e2IWOAxzJ
=k0v8
-----END PGP SIGNATURE-----

pinheadmz's public key is on openpgp.org

ACK 352fdc279751694348c6b406322de5582f59ad49 Built and tested on macos/arm64. Reviewed the changes and checked with both meatball brain and LLM that the refactor didn't change behavior. Great new text coverage helps with that a lot as well. Left a few non blocking suggestions. <details><summary>Show Signature</summary> ``` -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 ACK 352fdc279751694348c6b406322de5582f59ad49 -----BEGIN PGP SIGNATURE----- iQJPBAEBCAA5FiEE5hdzzW4BBA4vG9eM5+KYS2KJyToFAmq1X5EbFIAAAAAABAAO bWFudTIsMi41KzEuMTIsMCwzAAoJEOfimEtiick6nf8QALaqDEDS8I6yCKfKkJ9L xEpZ46pQu9Ip04KhRfa2oHzplxr7Zc+0UQC0IiPqfgsVOOg0lH9jrzIgttdyEa/Z xWnfpxjW/BKKrBGpdp9DcDeNZqQ36IknKMvDDLCtSv677XcxysQqxHNDUh9XNmyy CgGJkCKF0y3kzPs/Zu/IUZxLEzdE6Uzr+fz4PphwbO8ewAqygDRa9Sl2a9m7Dp5W 50UG6exRmMsrjWZF+oO1CWHvfEC5OTJPIK4u+BGpCuTjZzGvCGrncnbTyrxDlZBh rE5Tk+/mmWjlE1NMoNf3quTjYDb0TwEp+jdNoaHEn3ivHgWig33H/4Y5yXxZh4Y4 5jhMzekBo7P2NcsTkuin3g72V/ZxYWbJbC9y3Vxs86mbBwqVDmGiHH1HL9JRI0Kt DiM1F+VGMx0kc/ZrzY64397CwuwZAuYPFlM7aVMsQlLtqlex6JiY7lAYFuTttm5R u7OHtCVFZ3RFkR/M+LZm2GwLiMQN4GVfdIfO5bEQCfXMoVyLvzZ8r6kb8s1uwgvE DSmvwnOgEizVbp3uo3GmaFItpUxbY2bG8dkx37VPwyWBpU2TuJ1Y4S2AsmcCRriV ccDrOqY45LV7E19y3alKNLM7mwQVcw27TyIvT/XU4Rs63dcNOneRQ8at4mCB1WM/ JZzZqJl1uPRsV77e2IWOAxzJ =k0v8 -----END PGP SIGNATURE----- ``` pinheadmz's public key is [on openpgp.org](https://keys.openpgp.org/vks/v1/by-fingerprint/E61773CD6E01040E2F1BD78CE7E2984B6289C93A) </details>
pinheadmz (Migrated from github.com) commented 2026-09-24 17:04:22 +00:00

Possible impovement: move the Log("Disconnecting client...") here inside the lambda. Then ShouldDisconnect() could be clarified by filling those blank spaces with return statements

Possible impovement: move the Log("Disconnecting client...") here inside the lambda. Then `ShouldDisconnect()` could be clarified by filling those blank spaces with `return` statements
Lines 1138-1139
pinheadmz (Migrated from github.com) commented 2026-09-24 15:43:52 +00:00

352fdc2797

tiny nit, comment placement: I think this comment can just be removed or something added to the above comment that this is where even keep-alive clients get dropped, because we are shutting down.

your other comments all describe a logic decision about to be made on the next line and so I think its a bit confusing to have this written here.

352fdc279751694348c6b406322de5582f59ad49 tiny nit, comment placement: I think this comment can just be removed or something added to the above comment that this is where even keep-alive clients get dropped, because we are shutting down. your other comments all describe a logic decision about to be made on the next line and so I think its a bit confusing to have this written here.
First-time contributor

Base: aef8a04966d7ab6c04edc5d19b733e78205405de
Head: 2abf671d2e14b5ceb91fb570258945b41ef78299

💡 Suggestion

Check shutdown while response bytes remain buffered (src/test/httpserver_tests.cpp:834, head)

Before draining the buffer, also assert !busy_slow->ShouldDisconnect(now + timeout - 1s, timeout, /*disconnect_all=*/true). WriteReply() clears m_req_busy, but this SlowSocket still has buffered response bytes and m_connection_busy remains true. The existing shutdown check at line 828 runs only while the worker is handling the request, so without this assertion a regression that checks m_req_busy instead of m_connection_busy could disconnect before the response is fully sent and still pass these tests.

Review coverage was incomplete. The archaeology review had incomplete evidence.

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> Base: `aef8a04966d7ab6c04edc5d19b733e78205405de` Head: `2abf671d2e14b5ceb91fb570258945b41ef78299` ##### 💡 Suggestion **Check shutdown while response bytes remain buffered** (src/test/httpserver_tests.cpp:834, head) Before draining the buffer, also assert `!busy_slow->ShouldDisconnect(now + timeout - 1s, timeout, /*disconnect_all=*/true)`. `WriteReply()` clears `m_req_busy`, but this `SlowSocket` still has buffered response bytes and `m_connection_busy` remains true. The existing shutdown check at line 828 runs only while the worker is handling the request, so without this assertion a regression that checks `m_req_busy` instead of `m_connection_busy` could disconnect before the response is fully sent and still pass these tests. Review coverage was incomplete. The archaeology review had incomplete evidence. [Full review report](<https://review.fish.foo/traces/36159-2abf671d2e14b5ceb91fb570258945b41ef78299-497-3.html>)
sedited commented 2026-10-06 08:07:24 +00:00 (Migrated from github.com)

@hodlinator do you want to take @pinheadmz's nits? Asking because this is already a followup/refactoring pull request.

@hodlinator do you want to take @pinheadmz's nits? Asking because this is already a followup/refactoring pull request.
hodlinator commented 2026-10-06 19:52:32 +00:00 (Migrated from github.com)
re https://github.com/bitcoin/bitcoin/pull/36159#issuecomment-6012097013 & https://github.com/bitcoin/bitcoin/pull/36159#pullrequestreview-5306713554: Thanks for the review @pinheadmz, applied your neat nits. Also attempted to improve some comments. `git range-diff 352fdc27~4...352fdc27 2abf671d~4...2abf671d`
pinheadmz (Migrated from github.com) approved these changes 2026-10-14 06:01:27 +00:00
pinheadmz (Migrated from github.com) left a comment

ACK 2abf671d2e

Built, tested. Re-reviewed. Thanks for taking my suggestions!

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

ACK 2abf671d2e14b5ceb91fb570258945b41ef78299
-----BEGIN PGP SIGNATURE-----

iQJPBAEBCAA5FiEE5hdzzW4BBA4vG9eM5+KYS2KJyToFAmrPGqMbFIAAAAAABAAO
bWFudTIsMi41KzEuMTIsMCwzAAoJEOfimEtiick612wP/3qL+Lw+FuCh+T8oAdMc
x8E2M59R7lM1npICusZ3HMYxdx8+Umk1OmQolhnlcEk9SWni5skPIpnFhem+pLUt
Zq/6Fl6rHEphzLnRDgBzynhndmjnGWMivpOc8yZRjfJG6x3pKqow5yhCP3x+wiQm
qBIPKKiVrQbEVha0SKlM68quBRl6oKxv7cBkQ4GJYQ7AVe9assdXoVWq9vnfnk9I
bz8HZ8kfRsXDXuBBUa6IfHZ+hc4e9uA8UuVwK2KzTtF8pwBkF9ipPc1sHY37eXqe
02A/2jwMCPAp24NTsZ3QMFsf+7kvDkwqfLzUOJim72SHsNzze7d3pKSC/5qh2ana
3ljZA9HervZizi8n7CEYXnzFc+1jN/ocs6vzYSYAjK9/+QtxUvSMDIH00xkFQWFu
Nr/yjYIVHo4f4EIIKckIcHiDHqSuUnhewj39ges2RSTMvjLsSry0KnuKzlUt5hqT
e8SO+mlul43W4cxmd2Bp8J39BwbHwc2eT9a47BTmgdLXnRYweT0DupA/NhjKSz4h
UmGmihT/nG2n/tq9FaQm9D03Llw0ROAHAZoGZ7Sxc4YVSwrIOP0C7xmqTgxcMRPx
c/LAo6JTsu/1/r5DY+jiw3CCt1NTfkKH2EIgUC5rkUWVUFI5Meen/LigsOq0z33k
9MBfzFPboMW63Feqc6zGifAW
=Bwbg
-----END PGP SIGNATURE-----

pinheadmz's public key is on openpgp.org

ACK 2abf671d2e14b5ceb91fb570258945b41ef78299 Built, tested. Re-reviewed. Thanks for taking my suggestions! <details><summary>Show Signature</summary> ``` -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 ACK 2abf671d2e14b5ceb91fb570258945b41ef78299 -----BEGIN PGP SIGNATURE----- iQJPBAEBCAA5FiEE5hdzzW4BBA4vG9eM5+KYS2KJyToFAmrPGqMbFIAAAAAABAAO bWFudTIsMi41KzEuMTIsMCwzAAoJEOfimEtiick612wP/3qL+Lw+FuCh+T8oAdMc x8E2M59R7lM1npICusZ3HMYxdx8+Umk1OmQolhnlcEk9SWni5skPIpnFhem+pLUt Zq/6Fl6rHEphzLnRDgBzynhndmjnGWMivpOc8yZRjfJG6x3pKqow5yhCP3x+wiQm qBIPKKiVrQbEVha0SKlM68quBRl6oKxv7cBkQ4GJYQ7AVe9assdXoVWq9vnfnk9I bz8HZ8kfRsXDXuBBUa6IfHZ+hc4e9uA8UuVwK2KzTtF8pwBkF9ipPc1sHY37eXqe 02A/2jwMCPAp24NTsZ3QMFsf+7kvDkwqfLzUOJim72SHsNzze7d3pKSC/5qh2ana 3ljZA9HervZizi8n7CEYXnzFc+1jN/ocs6vzYSYAjK9/+QtxUvSMDIH00xkFQWFu Nr/yjYIVHo4f4EIIKckIcHiDHqSuUnhewj39ges2RSTMvjLsSry0KnuKzlUt5hqT e8SO+mlul43W4cxmd2Bp8J39BwbHwc2eT9a47BTmgdLXnRYweT0DupA/NhjKSz4h UmGmihT/nG2n/tq9FaQm9D03Llw0ROAHAZoGZ7Sxc4YVSwrIOP0C7xmqTgxcMRPx c/LAo6JTsu/1/r5DY+jiw3CCt1NTfkKH2EIgUC5rkUWVUFI5Meen/LigsOq0z33k 9MBfzFPboMW63Feqc6zGifAW =Bwbg -----END PGP SIGNATURE----- ``` pinheadmz's public key is [on openpgp.org](https://keys.openpgp.org/vks/v1/by-fingerprint/E61773CD6E01040E2F1BD78CE7E2984B6289C93A) </details>
winterrdog (Migrated from github.com) reviewed 2026-10-14 15:11:13 +00:00
winterrdog (Migrated from github.com) left a comment

reACK 2abf671d2e

reACK 2abf671d2e14b5ceb91fb570258945b41ef78299
winterrdog (Migrated from github.com) commented 2026-10-14 15:04:00 +00:00

cosmetic nit: now that every branch returns, i think plain ifs (without else) would read a bit nicer here. right now, the comments sit between a return true; and the next } else if, so they look like they belong to the previous block (kind of puzzled me at first glance but later on i realised what you meant). with separate ifs each comment lands right above the condition it explains, and the behavior would be identical. sth like:

diff --git a/src/httpserver.cpp b/src/httpserver.cpp
index dc2b8e8310..86696d2c69 100644
--- a/src/httpserver.cpp
+++ b/src/httpserver.cpp
@@ -1175,30 +1175,31 @@ bool HTTPRemoteClient::ShouldDisconnect(MockableSteadyClock::time_point now, std
     if (m_disconnect) {
         // We should already have logged the reason when we set m_disconnect.
         return true;
+    }
     // Check for idle timeout. We reset the timer when we send and receive data,
     // but if the server is busy handling a request we should ignore the timeout until
     // the reply is sent. If we did erase the shared_ptr<HTTPRemoteClient> reference in m_connected
     // while the server is busy with a request, it might be prematurely dropped before
     // the response has been sent, or if the HTTPRequest was holding a temporary shared_ptr
     // client on a worker thread - it would keep the socket open even after "disconnecting".
-    } else if (rpcservertimeout.count() > 0 &&
-               now - m_idle_since.load() > rpcservertimeout &&
-               !m_req_busy) {
+    if (rpcservertimeout.count() > 0 &&
+        now - m_idle_since.load() > rpcservertimeout &&
+        !m_req_busy) {
         LogDebug(BCLog::HTTP,
                  "HTTP client idle timeout %s (id=%llu)",
                  m_origin,
                  m_id);
         return true;
+    }
     // Disconnect this client because the server is shutting down and we need to
     // disconnect all clients... Unless the client is still being handled by a
     // worker thread, in which case we'd rather continue the I/O loop (but might
     // not complete sending back the full response).
-    } else if (disconnect_all && !m_connection_busy) {
+    if (disconnect_all && !m_connection_busy) {
         return true;
-    } else {
-        // No reason to disconnect.
-        return false;
     }
+    // No reason to disconnect.
+    return false;
 }

 void HTTPServer::ClearConnectedClients()

non-blocking, it is :)

**cosmetic nit**: now that every branch `return`s, i think plain `if`s (without `else`) would read a bit nicer here. right now, the comments sit between a `return true;` and the next `} else if`, so they look like they belong to the previous block (kind of puzzled me at first glance but later on i realised what you meant). with separate `if`s each comment lands right above the condition it explains, and the behavior would be identical. sth like: ```diff diff --git a/src/httpserver.cpp b/src/httpserver.cpp index dc2b8e8310..86696d2c69 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -1175,30 +1175,31 @@ bool HTTPRemoteClient::ShouldDisconnect(MockableSteadyClock::time_point now, std if (m_disconnect) { // We should already have logged the reason when we set m_disconnect. return true; + } // Check for idle timeout. We reset the timer when we send and receive data, // but if the server is busy handling a request we should ignore the timeout until // the reply is sent. If we did erase the shared_ptr<HTTPRemoteClient> reference in m_connected // while the server is busy with a request, it might be prematurely dropped before // the response has been sent, or if the HTTPRequest was holding a temporary shared_ptr // client on a worker thread - it would keep the socket open even after "disconnecting". - } else if (rpcservertimeout.count() > 0 && - now - m_idle_since.load() > rpcservertimeout && - !m_req_busy) { + if (rpcservertimeout.count() > 0 && + now - m_idle_since.load() > rpcservertimeout && + !m_req_busy) { LogDebug(BCLog::HTTP, "HTTP client idle timeout %s (id=%llu)", m_origin, m_id); return true; + } // Disconnect this client because the server is shutting down and we need to // disconnect all clients... Unless the client is still being handled by a // worker thread, in which case we'd rather continue the I/O loop (but might // not complete sending back the full response). - } else if (disconnect_all && !m_connection_busy) { + if (disconnect_all && !m_connection_busy) { return true; - } else { - // No reason to disconnect. - return false; } + // No reason to disconnect. + return false; } void HTTPServer::ClearConnectedClients() ``` --- non-blocking, it is :)
hodlinator (Migrated from github.com) reviewed 2026-10-14 17:17:46 +00:00
hodlinator (Migrated from github.com) commented 2026-10-14 17:17:46 +00:00

That's fair, the comments before the prior scope's ending curly brace didn't sit 100% comfortably with me either. Will do if I need to rebase again or touch for other reasons.

That's fair, the comments before the prior scope's ending curly brace didn't sit 100% comfortably with me either. Will do if I need to rebase again or touch for other reasons.
sedited (Migrated from github.com) approved these changes 2026-10-19 08:16:23 +00:00
sedited (Migrated from github.com) left a comment
ACK 2abf671d2e14b5ceb91fb570258945b41ef78299
Sign in to join this conversation.
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!36159
No description provided.