[PATCH] Upstream keepalive: resuming now ignores ready connections

Maxim Dounin mdounin at mdounin.ru
Fri Aug 21 02:10:16 UTC 2026


# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1787278203 -10800
#      Fri Aug 21 05:10:03 2026 +0300
# Node ID e679fac46aa87898f474a93301581c1b017a4758
# Parent  bcfb58c2512522328beeb25ad8d54d754ecc41b3
Upstream keepalive: resuming now ignores ready connections.

In ngx_http_upstream_get_keepalive_peer(), c->read->ready flag might be
set only if the upstream connection was reported readable in this event
loop iteration (and the close handler wasn't called yet).  Now such
connections are ignored, so the close handler will close them once
called.

This slightly reduces the race window between closing a connection by
the backend and using the connection to send a request at the same time.

diff --git a/src/http/modules/ngx_http_upstream_keepalive_module.c b/src/http/modules/ngx_http_upstream_keepalive_module.c
--- a/src/http/modules/ngx_http_upstream_keepalive_module.c
+++ b/src/http/modules/ngx_http_upstream_keepalive_module.c
@@ -264,6 +264,10 @@ ngx_http_upstream_get_keepalive_peer(ngx
         item = ngx_queue_data(q, ngx_http_upstream_keepalive_cache_t, queue);
         c = item->connection;
 
+        if (c->read->ready) {
+            continue;
+        }
+
         if (ngx_memn2cmp((u_char *) &item->sockaddr, (u_char *) pc->sockaddr,
                          item->socklen, pc->socklen)
             == 0)



More information about the nginx-devel mailing list