Archived
server: bound what an unauthenticated socket can make the transport hold
Three ways a client spent the server's patience or memory without ever being admitted: The gate's 401 answers before any handler sees the request, so its body sits unread on a keep-alive socket, and httplib's anti-smuggling drain will wait for it: one byte per read timeout paces a whole worker, at the pacing client's chosen rate. The refusal now closes the connection instead, says so on the response, and buries the body with the socket -- which is also what makes draining a refusal pointless. A drain that does have a purpose -- a framed body on a request the router answered without reading it -- inherits the stream's 30 s read wait per packet gap and no total bound at all. It keeps both waits now: five seconds per gap (the stream's own set_read_timeout, the same knob the WebSocket upgrade uses) and five in total (a deadline the discard provider checks), then the connection dies and the timeout is restored for whatever the connection still has coming. And bodies. The per-request cap bounds one body and no sum, every byte of a body is buffered before parse, admission or any accounting sees it, and a few hundred concurrent large POSTs is an OOM that reaches the engine before any refusal does. read_and_bridge now holds every buffered byte against a transport-wide budget and answers 429 with Retry-After to the newcomer the budget has no room for; the reservation releases on every exit, including the peer vanishing mid-body. The budget defaults to eight bodies at the per-request cap and moves through ServerOptions::max_inflight_body_bytes. Vendor: the two httplib hunks are recorded as patches/0022, and UPSTREAM's "no file content differs" for the pair is replaced with what the split actually is and what is now edited in it. transport_test: a_refusal_closes_the_connection, a_stalled_drain_dies_on_its_deadline, the_inflight_body_budget. ASan+UBSan: transport/wire/lifecycle/server/config/diag/dashboard, 7/7.
This commit is contained in:
1 parent
ec53bce5dc
commit
5f7244da4d
8 files changed
+268
-12
No files matched your search
+44
-4
@@ -656,24 +656,64 @@ int HttpTransport::serve(const Router& router, const TransportOptions& o) {
|
||||
[st](bool ok) { if (st->on_close) st->on_close(ok); });
|
||||
};
|
||||
|
||||
/* The sum of buffered bodies across all connections, and its budget. Bodies are buffered
|
||||
* whole before parse, admission or any accounting sees them, so the per-request cap alone
|
||||
* bounds none of the total: a few hundred concurrent large POSTs is an OOM that reaches the
|
||||
* engine before any 429 does. Over budget, the body is refused mid-read with a 429. */
|
||||
const size_t inflight_limit =
|
||||
o.max_inflight_body_bytes ? o.max_inflight_body_bytes : 8 * o.max_body_bytes;
|
||||
auto inflight = std::make_shared<std::atomic<size_t>>(0);
|
||||
|
||||
/* A body is read straight into the string the handler parses. httplib's own Request hands
|
||||
* it out only const, so it would be copied and held twice. A multipart body (an image
|
||||
* upload) is read part by part, each into its own string, under the same limit. */
|
||||
auto read_and_bridge = [bridge](const httplib::Request& q, httplib::Response& s,
|
||||
const httplib::ContentReader& read) {
|
||||
auto read_and_bridge = [bridge, inflight, inflight_limit](const httplib::Request& q,
|
||||
httplib::Response& s,
|
||||
const httplib::ContentReader& read) {
|
||||
std::string body;
|
||||
std::vector<FormPart> form;
|
||||
/* Every byte appended is held against the budget until the handler is done with the
|
||||
* body it belongs to; the guard releases the whole reservation on any exit. */
|
||||
size_t held = 0;
|
||||
struct Releaser {
|
||||
std::shared_ptr<std::atomic<size_t>> n;
|
||||
size_t* held;
|
||||
~Releaser() { n->fetch_sub(*held); }
|
||||
} releaser{inflight, &held};
|
||||
bool over = false;
|
||||
auto room = [&](size_t n) {
|
||||
held += n;
|
||||
if (inflight->fetch_add(n) + n <= inflight_limit) return true;
|
||||
over = true;
|
||||
return false;
|
||||
};
|
||||
const bool ok = q.is_multipart_form_data()
|
||||
? read([&form](const httplib::FormData& f) {
|
||||
form.push_back(FormPart{ f.name, f.filename, f.content_type, {} });
|
||||
return true;
|
||||
},
|
||||
[&form](const char* p, size_t n) {
|
||||
[&form, &room](const char* p, size_t n) {
|
||||
if (!room(n)) return false;
|
||||
if (!form.empty()) form.back().data.append(p, n);
|
||||
return true;
|
||||
})
|
||||
: read([&body](const char* p, size_t n) { body.append(p, n); return true; });
|
||||
: read([&body, &room](const char* p, size_t n) {
|
||||
if (!room(n)) return false;
|
||||
body.append(p, n);
|
||||
return true;
|
||||
});
|
||||
if (!ok) {
|
||||
if (over) {
|
||||
/* The summed budget, not the per-request cap: name it so the client reads the
|
||||
* retry for what it is, not as its own file having grown too large. */
|
||||
s.status = 429;
|
||||
s.set_header("Retry-After", "5");
|
||||
s.set_content(error_body(q.path, 429, "rate_limit_error",
|
||||
"the server is holding more request bodies than its "
|
||||
"total buffering budget allows; retry shortly"),
|
||||
"application/json");
|
||||
return;
|
||||
}
|
||||
/* httplib has already set 413 for a body over the limit; anything else is a peer
|
||||
* that stopped sending partway. */
|
||||
if (s.status < 0) {
|
||||
|
||||
Reference in new issue
Block a user