From f16798587eea24aa413a582431f613f5f93c59de Mon Sep 17 00:00:00 2001 From: Olivier Houchard Date: Tue, 17 Nov 2026 17:20:57 +0100 Subject: [PATCH] BUG/MEDIUM: dns: release the session slot of expired TCP queries 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 | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/dns.c b/src/dns.c index 48d8d62e4..49665cf3b 100644 --- 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; } -- 2.47.3