From 1b6c6f59df1cfdd59cc50896732373d845e2e885 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Wed, 18 Nov 2026 21:52:31 -0500 Subject: [PATCH] Complete held writes within the proxy write loop. --- include/bitcoin/network/net/proxy.hpp | 2 ++ include/bitcoin/network/net/socket.hpp | 5 ++++ src/net/proxy_queue.cpp | 26 +++++++++++++++++---- src/net/socket.cpp | 17 ++++++++++++++ test/net/socket.cpp | 32 ++++++++++++++++++++++++++ 5 files changed, 77 insertions(+), 5 deletions(-) diff --git a/include/bitcoin/network/net/proxy.hpp b/include/bitcoin/network/net/proxy.hpp index a3bd36a03..407201598 100644 --- a/include/bitcoin/network/net/proxy.hpp +++ b/include/bitcoin/network/net/proxy.hpp @@ -359,6 +359,8 @@ class BCT_API proxy queue deferred_{}; size_t backlog_{}; bool writing_{}; + bool looping_{}; + bool again_{}; bool batched_{}; bool parted_{}; bool opened_{}; diff --git a/include/bitcoin/network/net/socket.hpp b/include/bitcoin/network/net/socket.hpp index c2c30e231..98b64f718 100644 --- a/include/bitcoin/network/net/socket.hpp +++ b/include/bitcoin/network/net/socket.hpp @@ -209,6 +209,9 @@ class BCT_API socket /// A held write completes when held, before it is written. void hold(bool enable) NOEXCEPT; + /// Complete a write held since hold, if any (strand). + void settle() NOEXCEPT; + /// Writes are held (strand). bool holding() const NOEXCEPT; @@ -733,7 +736,9 @@ class BCT_API socket // readable bytes of which are the held writes. http::flat_buffer response_; std::function deferred_{}; + std::function settled_{}; bool hold_{}; + bool settling_{}; bool sending_{}; // Websocket framing (once upgraded). diff --git a/src/net/proxy_queue.cpp b/src/net/proxy_queue.cpp index e4a6818bd..890f48b0f 100644 --- a/src/net/proxy_queue.cpp +++ b/src/net/proxy_queue.cpp @@ -104,17 +104,33 @@ void proxy::do_write(pending& write_, bool notification) NOEXCEPT write(); } +// A write invoked by a completion within the loop is invoked by the loop. void proxy::write() NOEXCEPT { BC_ASSERT(stranded()); - if (queue_.empty()) + if (looping_) + { + again_ = true; return; + } // Invokes oldest writer on the queue, completion invokes handle_write. - // Its writes are held for the next writer while another is queued. - writing_ = true; - socket_->hold(queue_.size() > one); - queue_.front().call(); + // Its writes are held for the next writer while another is queued, and a + // held write completes on settle. + looping_ = true; + do + { + again_ = false; + if (queue_.empty()) + break; + + writing_ = true; + socket_->hold(queue_.size() > one); + queue_.front().call(); + socket_->settle(); + } + while (again_); + looping_ = false; } void proxy::handle_write(const code& ec, size_t bytes) NOEXCEPT diff --git a/src/net/socket.cpp b/src/net/socket.cpp index db9801373..61a3d62f2 100644 --- a/src/net/socket.cpp +++ b/src/net/socket.cpp @@ -436,6 +436,15 @@ void socket::hold(bool enable) NOEXCEPT { BC_ASSERT(stranded()); hold_ = enable; + settling_ = enable; +} + +void socket::settle() NOEXCEPT +{ + BC_ASSERT(stranded()); + settling_ = false; + if (settled_) + std::exchange(settled_, {})(); } bool socket::holding() const NOEXCEPT @@ -448,6 +457,7 @@ void socket::release(count_handler&& handler) NOEXCEPT { BC_ASSERT(stranded()); hold_ = false; + settling_ = false; transmit({}, false, handler); } @@ -471,6 +481,13 @@ void socket::transmit(const std::span& buffers, if (hold && hold_ && size <= floored_subtract(bound, held)) { response_.append(buffers); + if (settling_) + { + settling_ = false; + settled_ = std::bind(handler, error::success, size); + return; + } + io::post(strand_, std::bind(handler, error::success, size)); return; } diff --git a/test/net/socket.cpp b/test/net/socket.cpp index 7689a9177..4f19ad727 100644 --- a/test/net/socket.cpp +++ b/test/net/socket.cpp @@ -1096,6 +1096,7 @@ BC_FIXTURE_TEST_CASE(socket__hold__fits_minimum__completes_held_and_written_with written.set(!ec && size == second.size() && !instance->holding()); }); }); + instance->settle(); }); BC_REQUIRE(held.get()); @@ -1110,6 +1111,34 @@ BC_FIXTURE_TEST_CASE(socket__hold__fits_minimum__completes_held_and_written_with instance->stop(); } +BC_FIXTURE_TEST_CASE(socket__settle__held__completes_on_settle, acceptor_fixture) +{ + static const system::data_array<2> data{ 'a', 'b' }; + auto params = clear; + params.minimum_buffer = 64; + const awaiter accepted{}; + const auto instance = accept(params, accepted); + BC_REQUIRE_EQUAL(accepted.get(), error::success); + + const awaiter settled{}; + io::post(instance->strand(), [=]() NOEXCEPT + { + const auto completed = std::make_shared(false); + instance->hold(true); + instance->tcp_write({ data.data(), data.size() }, [=](const code& ec, size_t size) NOEXCEPT + { + *completed = !ec && size == data.size(); + }); + + const auto before = *completed; + instance->settle(); + settled.set(!before && *completed && instance->holding()); + }); + + BC_REQUIRE(settled.get()); + instance->stop(); +} + BC_FIXTURE_TEST_CASE(socket__hold__exceeds_minimum__written, acceptor_fixture) { static const system::data_array<2> data{ 'a', 'b' }; @@ -1127,6 +1156,7 @@ BC_FIXTURE_TEST_CASE(socket__hold__exceeds_minimum__written, acceptor_fixture) { written.set(!ec && size == data.size() && !instance->holding()); }); + instance->settle(); }); BC_REQUIRE(written.get()); @@ -1161,6 +1191,7 @@ BC_FIXTURE_TEST_CASE(socket__release__held__written, acceptor_fixture) released.set(!ec && is_zero(size) && !instance->holding()); }); }); + instance->settle(); }); BC_REQUIRE(held.get()); @@ -1193,6 +1224,7 @@ BC_FIXTURE_TEST_CASE(socket__lazy_stop__held__written, acceptor_fixture) held.set(!ec && instance->holding()); instance->lazy_stop(); }); + instance->settle(); }); BC_REQUIRE(held.get());