]> git.kaiwu.me - nginx.git/commitdiff
Core: avoid repeating send_chain() when it sent nothing
authorRoman Arutyunyan <arut@nginx.com>
Tue, 17 Nov 2026 19:44:39 +0000 (23:44 +0400)
committerSergey Kandaurov <pluknet@nginx.com>
Thu, 19 Nov 2026 16:56:13 +0000 (20:56 +0400)
Previously, ngx_chain_writer() 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 re-post was introduced without a progress check in b3b368184
(1.21.4).  A check is now added by requiring that c->send_chain()
advanced c->sent.

src/core/ngx_output_chain.c

index eb467e06a845ae80c723c9ed14e42e1f44d09c8c..b3a623e1c0b74388ee65a4d12b0c740de6dcd12d 100644 (file)
@@ -670,7 +670,7 @@ ngx_chain_writer(void *data, ngx_chain_t *in)
 {
     ngx_chain_writer_ctx_t *ctx = data;
 
-    off_t              size;
+    off_t              size, sent;
     ngx_chain_t       *cl, *ln, *chain;
     ngx_connection_t  *c;
 
@@ -787,6 +787,8 @@ ngx_chain_writer(void *data, ngx_chain_t *in)
         return NGX_OK;
     }
 
+    sent = c->sent;
+
     chain = c->send_chain(c, ctx->out, ctx->limit);
 
     ngx_log_debug1(NGX_LOG_DEBUG_CORE, c->log, 0,
@@ -796,7 +798,7 @@ ngx_chain_writer(void *data, ngx_chain_t *in)
         return NGX_ERROR;
     }
 
-    if (chain && c->write->ready) {
+    if (chain && c->write->ready && c->sent != sent) {
         ngx_post_event(c->write, &ngx_posted_next_events);
     }