From: Christopher Faulet Date: Wed, 2 Dec 2026 16:53:30 +0000 (+0100) Subject: BUG/MEDIUM: cache: report the end of message via HTX after a zero-copy forwarding X-Git-Url: https://git.kaiwu.me/web/http/static/%7B@url%7D?a=commitdiff_plain;h=eeda000416d2dfe9a570f1f77ec06fec26830d77;p=haproxy.git BUG/MEDIUM: cache: report the end of message via HTX after a zero-copy forwarding Since the commit c061ba30f ("MAJOR: cache: Send cached objects using zero-copy forwarding"), when all the payload of a cached object was sent using the zero-copy forwarding, the end of the message was directly reported by http_cache_fastfwd(), by setting the EOI and EOS flags on the applet. The end of the message was then only reported to the consumer via the IOBUF_FL_EOI flag. But the H1 mux does not emit the last chunk in that case. So, for an object ending on a DATA block (typically from an H2 origin without content-length) and too large to be sent at once with its headers, the response was sent chunked to HTTP/1.1 clients without its last chunk, and every cache hit failed. The H2 and H3 muxes were not affected because they set the END_STREAM flag on the last DATA frame. And objects ending on an EOT block were not affected either because the zero-copy forwarding stops on this block. Now, http_cache_fastfwd() only disables the zero-copy forwarding when all the payload was forwarded, like the stats applet does. The end of the message is then reported by the I/O handler via the HTX message, by setting the EOM flag. So the consumer can properly finish the message. A reg-test was added, with an H2 origin and HTTP/1.1 clients. This patch must be backported as far as 3.0. --- diff --git a/reg-tests/cache/zero_copy_eom.vtc b/reg-tests/cache/zero_copy_eom.vtc new file mode 100644 index 000000000..e5aa8dc06 --- /dev/null +++ b/reg-tests/cache/zero_copy_eom.vtc @@ -0,0 +1,84 @@ +varnishtest "End of a cached response sent using the zero-copy forwarding" + +# The H2 origin sends a response without content-length. The end of the +# message is carried by the last DATA frame (END_STREAM flag). So the cached +# object ends on a DATA block. The response is large enough to be sent using +# the zero-copy forwarding when it is served from the cache. HTTP/1.1 clients +# receive it chunked. The last chunk must be emitted at the end of the +# message, on a cache miss and on a cache hit. + +feature ignore_unknown_macro + +barrier b1 cond 2 + +server s1 { + rxpri + stream 0 { + txsettings + rxsettings + txsettings -ack + } -run + + stream 1 { + rxreq + txresp -noadd -status 200 -nostrend -hdr "cache-control" "max-age=60" + txdata -nostrend -datalen 10000 + txdata -nostrend -datalen 10000 + txdata -nostrend -datalen 10000 + txdata -datalen 10000 + } -run + + # keep the connection open until the response is received + barrier b1 sync +} -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}" + + cache my_cache + total-max-size 3 + max-age 60 + max-object-size 100000 + + frontend fe + bind "fd@${fe}" + http-after-response set-header x-cache-hit %[res.cache_hit] + default_backend be + + backend be + http-request cache-use my_cache + http-response cache-store my_cache + server s1 ${s1_addr}:${s1_port} proto h2 +} -start + +# cache miss +client c1 -connect ${h1_fe_sock} { + txreq + rxresp + expect resp.status == 200 + expect resp.http.x-cache-hit == 0 + expect resp.http.content-length == "" + expect resp.http.transfer-encoding == "chunked" + expect resp.bodylen == 40000 + barrier b1 sync +} -run + +# cache hit +client c2 -connect ${h1_fe_sock} { + txreq + rxresp + expect resp.status == 200 + expect resp.http.x-cache-hit == 1 + expect resp.http.content-length == "" + expect resp.http.transfer-encoding == "chunked" + expect resp.bodylen == 40000 +} -run diff --git a/src/cache.c b/src/cache.c index 22ae3dc0d..13492e47f 100644 --- a/src/cache.c +++ b/src/cache.c @@ -2280,9 +2280,6 @@ static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx) static size_t http_cache_fastfwd(struct appctx *appctx, struct buffer *buf, size_t count, unsigned int flags) { - struct cache_appctx *ctx = appctx->svcctx; - struct cache_entry *cache_ptr = ctx->entry; - struct shared_block *first = block_ptr(cache_ptr); size_t ret; BUG_ON(!appctx->to_forward || count > appctx->to_forward); @@ -2290,13 +2287,13 @@ static size_t http_cache_fastfwd(struct appctx *appctx, struct buffer *buf, size ret = ff_cache_dump_msg(appctx, buf, count); if (!appctx->to_forward) { + /* All the payload was forwarded. The end of the message is + * not reported here but by the I/O handler, via the HTX + * message, for the consumer to finish the message properly + * (for instance the H1 mux must emit the last chunk). + */ se_fl_clr(appctx->sedesc, SE_FL_MAY_FASTFWD_PROD); applet_fl_clr(appctx, APPCTX_FL_FASTFWD); - if (ctx->sent == first->len - sizeof(*cache_ptr)) { - applet_set_eoi(appctx); - applet_set_eos(appctx); - appctx->st0 = HTX_CACHE_END; - } } return ret; }