]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MEDIUM: dns: release the session slot of expired TCP queries master
authorOlivier Houchard <ohouchard@haproxy.com>
Tue, 17 Nov 2026 16:20:57 +0000 (17:20 +0100)
committerOlivier Houchard <cognet@ci0.org>
Tue, 17 Nov 2026 17:18:26 +0000 (18:18 +0100)
A DNS-over-TCP session counts its pipelined queries in ds->nb_queries, up
to DNS_STREAM_MAX_PIPELINED_REQ (4), and only dns_recv_nameserver()
releases a slot, when a response is delivered. But when a query expires
without response, dns_process_query_exp() only decrements onfly_queries.
After 4 unanswered queries, the session is thus neither in the free nor in
the idle list anymore: it is never reused nor released and its connection
remains open forever, while new queries open new connections. With a TCP
nameserver not answering some names, one connection is leaked every 4
such queries, without limit.

Let's release the slot when a query expires, and move the session to the
free or idle list, under the stream server's lock, exactly as done when a
response is received.

This was introduced in 2.4 with commit fd647d5f5 ("MEDIUM: dns: adds code
to support pipelined DNS requests over TCP."), so this must be backported
to all stable branches.

src/dns.c

index 48d8d62e4a20d784463da8007bd0dcfee57327c6..49665cf3bc5038c2fe936d483ac6693de0af847b 100644 (file)
--- a/src/dns.c
+++ b/src/dns.c
@@ -1068,7 +1068,9 @@ static struct appctx *dns_session_create(struct dns_session *ds, int tempo)
 static struct task *dns_process_query_exp(struct task *t, void *context, unsigned int state)
 {
        struct dns_session *ds = (struct dns_session *)context;
+       struct dns_stream_server *dss = ds->dss;
        struct dns_query *query, *queryb;
+       int expired = 0;
 
        t->expire = TICK_ETERNITY;
 
@@ -1078,6 +1080,7 @@ static struct task *dns_process_query_exp(struct task *t, void *context, unsigne
                        LIST_DELETE(&query->list);
                        pool_free(dns_query_pool, query);
                        ds->onfly_queries--;
+                       expired++;
                }
                else {
                        t->expire = query->expire;
@@ -1085,6 +1088,20 @@ static struct task *dns_process_query_exp(struct task *t, void *context, unsigne
                }
        }
 
+       if (expired) {
+               /* no response will come, release the slots */
+               HA_SPIN_LOCK(DNS_LOCK, &dss->lock);
+               ds->nb_queries -= expired;
+               LIST_DEL_INIT(&ds->list);
+               if (ds->nb_queries)
+                       LIST_INSERT(&dss->free_sess, &ds->list);
+               else {
+                       LIST_INSERT(&dss->idle_sess, &ds->list);
+                       dss->idle_conns++;
+               }
+               HA_SPIN_UNLOCK(DNS_LOCK, &dss->lock);
+       }
+
        return t;
 }