From 79cc319db25d74cc02752b9fb2000575847faf72 Mon Sep 17 00:00:00 2001 From: fl0l0u Date: Sun, 15 Nov 2026 17:29:48 +0000 Subject: [PATCH] vendor: cpp-httplib 0.66.0 -- the thread-creation failure stops aborting 0.64.0's ThreadPool spawns a dynamic worker inside the accept loop with no guard: when the process runs out of threads, std::thread's std::system_error escapes the loop and std::terminate takes the whole server down. A client that opens enough connections makes thread exhaustion a scheduled event; radiance's socket gate and drain budget narrowed that window but never closed it. 0.66.0 wraps the spawn in try/catch and keeps the job queued for an existing worker -- the abort becomes a queue. The pair moves to what llama.cpp carries at b8abe41a5 ("vendor: update cpp-httplib to 0.66.0 (#32049)"), byte for byte; the LICENSE was already byte-identical. Patch 0022 re-verified, not rewritten: the same hunk at a +26-line offset with no fuzz, upstream still bounds the keep-alive drain only by the connection's timeout, so the 5 s budget still stands. The six CPPHTTPLIB_* defines vendor/CMakeLists.txt passes were each re-checked as live knobs in the new pair. UPSTREAM carries the new record and the audit note; the manifest re-hashes in this commit. --- vendor/SHA256SUMS | 6 +-- vendor/UPSTREAM | 49 +++++++++++++---------- vendor/cpp-httplib/httplib.cpp | 73 ++++++++++++++++++++++++++++------ vendor/cpp-httplib/httplib.h | 25 +++++++++--- 4 files changed, 112 insertions(+), 41 deletions(-) diff --git a/vendor/SHA256SUMS b/vendor/SHA256SUMS index 6f57b8ca..c22fdd7b 100644 --- a/vendor/SHA256SUMS +++ b/vendor/SHA256SUMS @@ -1,6 +1,6 @@ a01f72edb9d0a7e9da75c5648cf62ae6f1c335afbfc150088a129288072dc32b vendor/CMakeLists.txt -d6f9a0fe461e2f7c0b051e39d7979c53e8b14884f87377e3b616a339d481fee9 vendor/cpp-httplib/httplib.cpp -d7168fd3d9266985c01e827c7a7cc002c0b0a0abcc8ff16d965ccbbaca3e4a2d vendor/cpp-httplib/httplib.h +bc202cbe3648a6650aa1e8f11d589cfc141742497035962c2c957f9e4bed8d41 vendor/cpp-httplib/httplib.cpp +1733a55d8917e6c316ed3376ff0f76617189ea567de82427c55eca375899e4ae vendor/cpp-httplib/httplib.h 4b45cbe16d7b71b89ae6127e26e0d90a029198ca5e958ad8e3d0b8bbed364d8b vendor/cpp-httplib/LICENSE f5a25d5d70073fe26494962e0b4f300565511f0028f8b1548f1a0c6c531bfc6d vendor/llama/common/chat-auto-parser-generator.cpp eb93af41e2ec50e62880af5da55f68bee83e00c5a6a4896e77cb9d3f8a970b20 vendor/llama/common/chat-auto-parser.h @@ -50,7 +50,7 @@ d5104362705e687271807f499481db6033d85988e13fac12f36e370c6fce0551 vendor/shim/ll 5d556a22069133f7931850a13094d7390ba091051f0dc98cb1cfca824142a705 vendor/shim/llama_shim.cpp 628efd8a88524082c4736d3bc52790aa459b40728f213246c54550011f2754a2 vendor/shim/log.h 1c343ff2dbf801873acc2ee67c0e874137fb36df31421f36fc5cb6812463aae4 vendor/shim/ryml_impl.cpp -e378c8ae16e85f2063943132cf4c35aa255734704b54786403ad413a3b575877 vendor/UPSTREAM +1de6e88333f9b22cce8a0a579da092a090151a0179b3f6984989943941406a9b vendor/UPSTREAM 45e384d3d52c73cba3a64d6e6c25d47cd738cd8a55c30629e3201046eda62947 vendor/yyjson/LICENSE d2d58ef0a3b2267862dc363832c4f3185fd375aea933d5d3419d1a940d6ece2e vendor/yyjson/yyjson.c ef803cda5c06b8962face6dfa39c3284b3bcf3e73f4d7317664690cc779679ca vendor/yyjson/yyjson.h diff --git a/vendor/UPSTREAM b/vendor/UPSTREAM index 3f9b0184..a0821a09 100644 --- a/vendor/UPSTREAM +++ b/vendor/UPSTREAM @@ -178,15 +178,15 @@ vendor/cpp-httplib/httplib.h vendor/cpp-httplib/httplib.cpp vendor/cpp-httplib/LICENSE <- llama.cpp vendor/cpp-httplib/cpp-httplib/* - The HTTP library at cpp-httplib v0.64.0 SPLIT from upstream's single ~24,300-line header - into a .h/.cpp pair, so one translation unit pays for it. Apart from the split the pair - matches upstream exactly when it was lifted; the split itself drops two decorative - `// ----` separator comment lines, moves `inline` from definitions to declarations, and - adds the .cpp's `#include "httplib.h"` and namespace brace. The only other difference - from upstream is the PATH: llama.cpp nests it one directory deeper and puts the outer - directory on the include path. Flattening lets `` resolve with - vendor/ alone on the path, which is the one include directory core/CMakeLists.txt - already adds. + The HTTP library at cpp-httplib v0.66.0, carried as the .h/.cpp SPLIT pair llama.cpp + distributes (v0.64.0 split upstream's single ~24,300-line header into a pair so that one + translation unit pays for it). Apart from the split mechanics the split itself introduced + (two decorative `// ----` separator lines dropped, `inline` moved from definitions to + declarations, the .cpp's `#include "httplib.h"` and namespace brace added) and patch 0022 + below, the pair is llama.cpp's bytes at the recorded commit, byte for byte. The PATH is + ours: llama.cpp nests the pair one directory deeper; flattening lets + `` resolve with vendor/ alone on the path, which is the one include + directory core/CMakeLists.txt already adds. cpp-httplib's own CMakeLists.txt is NOT carried. vendor/CMakeLists.txt builds httplib.cpp itself, because upstream's version reaches for llama.cpp's license and compile-flag helpers @@ -195,24 +195,33 @@ vendor/cpp-httplib/LICENSE <- llama.cpp vendor/cpp-httplib/cpp vendor/CMakeLists.txt sets, so a future disagreement between them would have been invisible -- the copy nobody builds is the copy nobody checks. - Upstream tracked: cpp-httplib tag v0.64.0 (yhirose/cpp-httplib); the split pair as carried by - llama.cpp commit 1ab23c33b ("vendor : update cpp-httplib to 0.64.0 (#31400)"). Earlier this pair - sat at v0.46.1 -- what llama.cpp carried at the lifted commit -- and carried patches/0022; the - bump retires most of that patch, because 0.64.0 natively closes the connection on every 4xx/5xx - response (the "Connection: close" option is honored by a scope_exit in write_response), skips - the keep-alive drain for any response that committed to close, bounds the drain that does run - (CPPHTTPLIB_UNREAD_PAYLOAD_MAX_LENGTH, the connection timeout as a total max_timeout, and a - close for a part-read body), and routes exceptions from the pre-routing handler and the - WebSocket upgrade through the exception handler. What survives is the budget the library still - ties to the connection's own timeout and cannot lower without capping uploads too: see below. + Upstream tracked: cpp-httplib tag v0.66.0 (yhirose/cpp-httplib); the split pair as carried by + llama.cpp commit b8abe41a5 ("vendor: update cpp-httplib to 0.66.0 (#32049)"). Earlier this pair + sat at v0.46.1 -- what llama.cpp carried at the lifted commit -- and carried patches/0022, and + at v0.64.0; the v0.64.0 bump retired most of that patch, because 0.64.0 natively closes the + connection on every 4xx/5xx response (the "Connection: close" option is honored by a scope_exit + in write_response), skips the keep-alive drain for any response that committed to close, bounds + the drain that does run (CPPHTTPLIB_UNREAD_PAYLOAD_MAX_LENGTH, the connection timeout as a + total max_timeout, and a close for a part-read body), and routes exceptions from the pre-routing + handler and the WebSocket upgrade through the exception handler. What survived is the budget the + library still ties to the connection's own timeout and cannot lower without capping uploads + too: see below. The v0.66.0 bump is the audit's upstream-currency finding: 0.66.0's ThreadPool + wraps the dynamic worker's creation in try/catch (std::system_error) and keeps the job queued + when the thread budget runs out, where 0.64.0 let the exception escape the accept loop into + std::terminate -- thread exhaustion was a process abort. radiance's socket gate and drain + budget narrowed that window; they never closed it. LOCAL CHANGES: patches/0022-httplib-the-drain-keeps-a-five-second-budget.patch On 0022: a response whose body the handler left unread drains on the keep-alive socket, and - 0.64.0 bounds that drain only by the connection's read timeout -- the same knob that bounds a + upstream bounds that drain only by the connection's read timeout -- the same knob that bounds a whole request's reads, uploads included, so radiance cannot simply lower it. A client dribbling one byte per gap paces a worker for that full timeout after the response was already sent. 0022 caps the drain's total budget at 5 s (or the connection timeout, whichever is smaller). + Re-verified against 0.66.0: the hunk lands at the same site with a +26-line offset and no + fuzz, and upstream changed nothing around it -- the drain is still bound only by the + connection's timeout, so the patch stands. The six CPPHTTPLIB_* defines vendor/CMakeLists.txt + passes were each re-checked as live knobs in the 0.66.0 pair. vendor/yyjson/yyjson.h vendor/yyjson/yyjson.c diff --git a/vendor/cpp-httplib/httplib.cpp b/vendor/cpp-httplib/httplib.cpp index e5efe4d7..62c172f5 100644 --- a/vendor/cpp-httplib/httplib.cpp +++ b/vendor/cpp-httplib/httplib.cpp @@ -1918,10 +1918,14 @@ select_read_interruptible(socket_t sock, time_t sec, time_t usec, while (!interrupted()) { auto left = std::chrono::duration_cast( deadline - std::chrono::steady_clock::now()); - auto wait = - (std::max)((std::min)(left, slice), std::chrono::microseconds(0)); - auto ret = select_impl(sock, POLLIN, 0, static_cast(wait.count())); - if (ret != 0 || wait < slice) { return ret; } + auto last = left < slice; + // poll() takes whole milliseconds, so round the last wait up to keep it + // from ending before the deadline. + auto wait_usec = last ? (std::max)((left.count() + 999) / 1000 * 1000, + static_cast(0)) + : slice.count(); + auto ret = select_impl(sock, POLLIN, 0, static_cast(wait_usec)); + if (ret != 0 || last) { return ret; } } return 0; } @@ -4413,6 +4417,7 @@ bool redirect(T &cli, Request &req, Response &res, Error &error) { Request new_req = req; new_req.path = path; + new_req.params.clear(); new_req.redirect_count_ -= 1; if (res.status == StatusCode::SeeOther_303 && @@ -7698,7 +7703,16 @@ bool ThreadPool::enqueue(std::function fn) { if (jobs_.size() > idle_thread_count_ && threads_.size() + dynamic_threads_.size() < max_thread_count_) { cleanup_finished_threads(); - dynamic_threads_.emplace_back(std::thread([this]() { worker(true); })); +#ifndef CPPHTTPLIB_NO_EXCEPTIONS + try { +#endif + dynamic_threads_.emplace_back(std::thread([this]() { worker(true); })); +#ifndef CPPHTTPLIB_NO_EXCEPTIONS + } catch (const std::system_error &) { + // No thread budget left: keep the job queued for an existing worker + // instead of letting the exception escape the accept loop. + } +#endif } } @@ -7937,7 +7951,8 @@ ssize_t SocketStream::read(char *ptr, size_t size) { ssize_t SocketStream::write(const char *ptr, size_t size) { if (!wait_writable()) { return -1; } -#if defined(_WIN32) && !defined(_WIN64) + // Winsock's send() takes an int length, even on 64-bit Windows +#ifdef _WIN32 size = (std::min)(size, static_cast((std::numeric_limits::max)())); #endif @@ -8152,14 +8167,22 @@ make_host_and_port_string_always_port(const std::string &host, int port) { return prepare_host_string(host) + ":" + std::to_string(port); } +// Where a client connects for `host`, as one string: the mapped address or +// name, or `host` itself. +std::string +addr_map_target(const std::map &addr_map, + const std::string &host) { + std::string connect_host; + std::string ip; + apply_addr_map(addr_map, host, connect_host, ip); + return ip.empty() ? connect_host : ip; +} + // Request target of a CONNECT for `host`, with `addr_map` applied. std::string make_connect_request_target(const std::map &addr_map, const std::string &host, int port) { - std::string connect_host; - std::string ip; - apply_addr_map(addr_map, host, connect_host, ip); - return make_host_and_port_string_always_port(ip.empty() ? connect_host : ip, + return make_host_and_port_string_always_port(addr_map_target(addr_map, host), port); } @@ -10352,7 +10375,10 @@ Server::process_request(Stream &strm, const std::string &remote_addr, if ((req.method == "GET" || req.method == "HEAD") && req.has_header("Range")) { const auto &range_header_value = req.get_header_value("Range"); - if (!detail::parse_range_header(range_header_value, req.ranges)) { + // RFC 9110 14.2: a range unit other than bytes must be ignored + auto unit = range_header_value.substr(0, range_header_value.find('=')); + if (detail::case_ignore::equal(unit, "bytes") && + !detail::parse_range_header(range_header_value, req.ranges)) { connection_closed = true; res.status = StatusCode::RangeNotSatisfiable_416; output_error_log(Error::InvalidRangeHeader, &req); @@ -10890,6 +10916,9 @@ bool ClientImpl::send_(Request &req, Response &res, Error &error) { // socket. socket_should_be_closed_when_request_is_done_ = false; + // A redirect or a retry sent inside a request keeps its stop() + if (socket_requests_in_flight_ == 0) { stop_requested_ = false; } + if (!socket_reusable()) { if (!ensure_socket_connection(socket_, error)) { return false; } @@ -10937,6 +10966,7 @@ bool ClientImpl::send_(Request &req, Response &res, Error &error) { }); ret = process_socket(socket_, req.start_time_, [&](Stream &strm) { + strm.set_read_interrupt([this] { return stop_requested_.load(); }); return handle_request(strm, req, res, close_connection, error); }); @@ -11549,8 +11579,13 @@ bool ClientImpl::redirect(Request &req, Response &res, Error &error) { auto path = std::move(next_path) + next_query; + // A Unix socket client sends "Host: localhost", so a Location naming + // localhost names this client too. + auto is_same_host = next_host == host_ || + (address_family_ == AF_UNIX && next_host == "localhost"); + // Same host redirect - use current client - if (next_scheme == scheme && next_host == host_ && next_port == port_) { + if (next_scheme == scheme && is_same_host && next_port == port_) { return detail::redirect(*this, req, res, path, location, error); } @@ -11701,6 +11736,14 @@ void ClientImpl::setup_redirect_client(ClientType &client) { // Copy network and socket settings client.addr_map_ = addr_map_; client.tunnel_addr_map_ = tunnel_addr_map_; + + // A Unix socket client reaches every host through its own socket, as curl's + // --unix-socket does, unless the caller mapped that host elsewhere. The + // Location's host is never taken as a socket path. + if (address_family_ == AF_UNIX) { + auto &target = client.addr_map_[client.host()]; + if (target.empty()) { target = detail::addr_map_target(addr_map_, host_); } + } client.set_address_family(address_family_); client.set_tcp_nodelay(tcp_nodelay_); client.set_ipv6_v6only(ipv6_v6only_); @@ -13155,6 +13198,9 @@ void ClientImpl::stop() { // are not thread-safe. if (socket_requests_in_flight_ > 0) { shutdown_socket(socket_); + // shutdown() does not wake a read waiting in WSAPoll on Windows, so the + // request's reads also give up once they see this flag. + stop_requested_ = true; // Aside from that, we set a flag for the socket to be closed when we're // done. @@ -18117,6 +18163,9 @@ ssize_t read(session_t session, void *buf, size_t len, TlsError &err) { } auto wsession = static_cast(session); + constexpr auto max_len = + static_cast((std::numeric_limits::max)()); + if (len > max_len) { len = max_len; } int ret = wolfSSL_read(wsession->ssl, buf, static_cast(len)); if (ret > 0) { diff --git a/vendor/cpp-httplib/httplib.h b/vendor/cpp-httplib/httplib.h index de77674a..489d1ef0 100644 --- a/vendor/cpp-httplib/httplib.h +++ b/vendor/cpp-httplib/httplib.h @@ -8,8 +8,8 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.64.0" -#define CPPHTTPLIB_VERSION_NUM "0x004000" +#define CPPHTTPLIB_VERSION "0.66.0" +#define CPPHTTPLIB_VERSION_NUM "0x004200" #ifdef _WIN32 #if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0A00 @@ -1029,18 +1029,27 @@ inline std::string resolve_relative_location(const std::string &location, // A ':' in the first segment means the value has a scheme. if (location.find(':') < location.find_first_of("/?#")) { return location; } - if (location[0] == '#') { return base.substr(0, base.find('#')) + location; } + // A path starting with "//" would be re-parsed as an authority. Prefix "/." + // (as the URL Standard does); remove_dot_segments strips it later. + auto on_this_host = [](std::string path) -> std::string { + if (path.compare(0, 2, "//") == 0) { path.insert(0, "/."); } + return path; + }; + + if (location[0] == '#') { + return on_this_host(base.substr(0, base.find('#')) + location); + } auto base_path = base.substr(0, base.find_first_of("?#")); - if (location[0] == '?') { return base_path + location; } + if (location[0] == '?') { return on_this_host(base_path + location); } if (base_path.empty() || base_path[0] != '/') { base_path = "/"; } auto merged = base_path.substr(0, base_path.rfind('/') + 1) + location; // Remove "." and ".." segments from the merged path. auto path_end = (std::min)(merged.find_first_of("?#"), merged.size()); - return remove_dot_segments(merged.substr(0, path_end)) + - merged.substr(path_end); + return on_this_host(remove_dot_segments(merged.substr(0, path_end)) + + merged.substr(path_end)); } } // namespace detail @@ -3058,6 +3067,10 @@ protected: bool socket_should_be_closed_when_request_is_done_ = false; size_t socket_tls_settings_version_ = 0; + // Set by stop() while a request is in flight. The request's reads poll it, + // because shutting the socket down does not wake a read on Windows. + std::atomic stop_requested_{false}; + std::atomic tls_settings_version_{0}; // Hostname to connection target map. The value is an IP literal or another