From: Christopher Faulet Date: Wed, 2 Dec 2026 16:17:18 +0000 (+0100) Subject: BUG/MEDIUM: bwlim: never stop the forwarding on the trailers X-Git-Url: https://git.kaiwu.me/web/%7Bsubstring-before(@doc,%20'.xml')%7D.html?a=commitdiff_plain;h=1b30ef85121425a731d1e0cc29fd64e560ad9961;p=haproxy.git BUG/MEDIUM: bwlim: never stop the forwarding on the trailers The bwlim filter limits the amount of data forwarded by returning, from its http_payload callback, the number of bytes allowed by the quota. But this quota was applied on all the HTTP payload, including the trailers (TLR blocks) and the EOT block. So the forwarding could stop in the middle of the trailers. However, the muxes emit the trailers all at once and cannot send them partially. On the H1 side, an error was reported because a TLR block was larger than the amount of data to send, and the response was aborted without its trailers. On the H2 side, all the trailers were emitted, consuming more data than forwarded, leading to an inconsistent state and the stream could linger until the client timeout (or a crash on a BUG_ON() with DEBUG_STRICT). Only the payload must be subject to the bandwidth limitation. So now, when the limit ends on a TLR block or on the EOT block, it means all the data were forwarded, and everything is forwarded. This is also true when nothing can be forwarded because the stream is paused: the trailers are no longer delayed. This patch must be backported to all stable versions. --- diff --git a/reg-tests/filters/bwlim-trailers.vtc b/reg-tests/filters/bwlim-trailers.vtc new file mode 100644 index 000000000..5f238432d --- /dev/null +++ b/reg-tests/filters/bwlim-trailers.vtc @@ -0,0 +1,109 @@ +varnishtest "Bandwidth limitation of a message with trailers" + +# Only the payload is subject to the bandwidth limitation. The trailers (TLR +# blocks and EOT block) are not, and they cannot be partially forwarded +# anyway, the muxes emit them all at once. So, when the limit ends on the +# trailers, they must all be forwarded. Here the server sends a chunked +# response with 10 bytes of payload and two trailers. In the HTX message, the +# payload is a DATA block of 10 bytes, each trailer is a TLR block of 13 bytes +# and the EOT block is 1 byte length. The limits are chosen to stop the +# forwarding: +# +# - at the beginning of the first TLR block (10 bytes) +# - in the middle of the first TLR block (15 bytes) +# - at the beginning of the second TLR block (23 bytes) +# - at the beginning of the EOT block (36 bytes) +# +# In all cases, the response must be received with its trailers. + +feature ignore_unknown_macro + +server s1 -repeat 4 { + rxreq + txresp -nolen -hdr "Transfer-Encoding: chunked" \ + -body "a\r\n0123456789\r\n0\r\nx-trailer1: foo\r\nx-trailer2: bar\r\n\r\n" +} -start + +haproxy h1 -conf { + global + .if feature(THREAD) + thread-groups 1 + .endif + + defaults + mode http + timeout connect "${HAPROXY_TEST_TIMEOUT-5s}" + timeout client "${HAPROXY_TEST_TIMEOUT-5s}" + timeout server "${HAPROXY_TEST_TIMEOUT-5s}" + + frontend fe + bind "fd@${fe}" proto h2 + filter bwlim-out lim10 default-limit 10 default-period 1s min-size 1 + filter bwlim-out lim15 default-limit 15 default-period 1s min-size 1 + filter bwlim-out lim23 default-limit 23 default-period 1s min-size 1 + filter bwlim-out lim36 default-limit 36 default-period 1s min-size 1 + http-request set-var(txn.path) path + http-response set-bandwidth-limit lim10 if { var(txn.path) -m str /10 } + http-response set-bandwidth-limit lim15 if { var(txn.path) -m str /15 } + http-response set-bandwidth-limit lim23 if { var(txn.path) -m str /23 } + http-response set-bandwidth-limit lim36 if { var(txn.path) -m str /36 } + default_backend be + + backend be + server s1 ${s1_addr}:${s1_port} +} -start + +client c1 -connect ${h1_fe_sock} { + txpri + stream 0 { + txsettings + rxsettings + txsettings -ack + rxsettings + expect settings.ack == true + } -run + + stream 1 { + txreq -url "/15" + rxhdrs + expect resp.status == 200 + rxdata + expect resp.body == "0123456789" + rxhdrs + expect resp.http.x-trailer1 == "foo" + expect resp.http.x-trailer2 == "bar" + } -run + + stream 3 { + txreq -url "/23" + rxhdrs + expect resp.status == 200 + rxdata + expect resp.body == "0123456789" + rxhdrs + expect resp.http.x-trailer1 == "foo" + expect resp.http.x-trailer2 == "bar" + } -run + + stream 5 { + txreq -url "/36" + rxhdrs + expect resp.status == 200 + rxdata + expect resp.body == "0123456789" + rxhdrs + expect resp.http.x-trailer1 == "foo" + expect resp.http.x-trailer2 == "bar" + } -run + + stream 7 { + txreq -url "/10" + rxhdrs + expect resp.status == 200 + rxdata + expect resp.body == "0123456789" + rxhdrs + expect resp.http.x-trailer1 == "foo" + expect resp.http.x-trailer2 == "bar" + } -run +} -run diff --git a/src/flt_bwlim.c b/src/flt_bwlim.c index 7d33f52dd..2dfcbb25b 100644 --- a/src/flt_bwlim.c +++ b/src/flt_bwlim.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -320,7 +321,24 @@ static int bwlim_http_headers(struct stream *s, struct filter *filter, struct ht static int bwlim_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, unsigned int offset, unsigned int len) { - return bwlim_apply_limit(filter, msg->chn, len); + struct htx *htx = htxbuf(&msg->chn->buf); + struct htx_ret htxret; + int ret; + + ret = bwlim_apply_limit(filter, msg->chn, len); + if (ret < 0 || ret >= len) + return ret; + + /* Only the payload is subject to the limitation. If the limit ends on + * the trailers (TLR blocks) or on the EOT block, it means all the data + * were forwarded. So forward everything. The muxes cannot emit the + * trailers partially anyway. + */ + htxret = htx_find_offset(htx, offset + ret); + if (htxret.blk && + (htx_get_blk_type(htxret.blk) == HTX_BLK_TLR || htx_get_blk_type(htxret.blk) == HTX_BLK_EOT)) + ret = len; + return ret; } /**************************************************************************