From: Roman Arutyunyan Date: Tue, 17 Nov 2026 19:02:36 +0000 (+0400) Subject: Avoid repeating send_chain() when it sent nothing X-Git-Tag: release-1.31.10~2 X-Git-Url: https://git.kaiwu.me/http/%22../static/%7B@url%7D?a=commitdiff_plain;h=2c84628229bdeedd16db5980af6e8fdf754cc80d;p=nginx.git Avoid repeating send_chain() when it sent nothing Previously, ngx_http_write_filter() re-posted the write event to continue a capped send whenever output data remained and c->write->ready was still set. This was wrong when c->send_chain() returned without sending anything while the write event still looked ready, notably when SSL_write() returned SSL_ERROR_WANT_READ. The event was then re-posted on every event loop iteration, busy-looping the worker at 100% CPU. The implicit progress check was lost in 00f4be1ce (1.21.4). It is now restored by requiring that c->send_chain() advanced c->sent. Reported by yxd05. --- diff --git a/src/http/ngx_http_write_filter_module.c b/src/http/ngx_http_write_filter_module.c index 9188ee948..a16c04a55 100644 --- a/src/http/ngx_http_write_filter_module.c +++ b/src/http/ngx_http_write_filter_module.c @@ -47,7 +47,7 @@ ngx_module_t ngx_http_write_filter_module = { ngx_int_t ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in) { - off_t size, sent, nsent, limit; + off_t size, sent, psent, nsent, limit; ngx_uint_t last, flush, sync; ngx_msec_t delay; ngx_chain_t *cl, *ln, **ll, *chain; @@ -308,13 +308,14 @@ ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in) if (r->limit_rate) { + psent = sent; nsent = c->sent; if (r->limit_rate_after) { - sent -= r->limit_rate_after; - if (sent < 0) { - sent = 0; + psent -= r->limit_rate_after; + if (psent < 0) { + psent = 0; } nsent -= r->limit_rate_after; @@ -323,7 +324,7 @@ ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in) } } - delay = (ngx_msec_t) ((nsent - sent) * 1000 / r->limit_rate); + delay = (ngx_msec_t) ((nsent - psent) * 1000 / r->limit_rate); if (delay > 0) { c->write->delayed = 1; @@ -331,7 +332,7 @@ ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in) } } - if (chain && c->write->ready && !c->write->delayed) { + if (chain && c->write->ready && !c->write->delayed && sent != c->sent) { ngx_post_event(c->write, &ngx_posted_next_events); }