Commit 746228f4c6 for bind

commit 746228f4c6e8af9c7bb0f98af3268cae532899db
Author: Aram Sargsyan <aram@isc.org>
Date:   Mon Nov 9 16:38:47 2026 +0000

    Fix timed out/canceled queries counting issue in TCP dispatch

    The 'disp->timedout' member counts how many queries were timed out
    so that we can expect as many outstanding answers if they happen
    to arrive eventually, and ignore them instead of treating as an
    error.

    There are code paths that can cause such outstanding queries that
    should have been ignored that aren't counted.

    Because of this, it's possible that when the answer to a query arrives
    after the resolver has given up on it, the resolver can treat it as
    unexpected and close the shared TCP connection.

    Fix the counting issues.

diff --git a/lib/dns/dispatch.c b/lib/dns/dispatch.c
index 55cb9770c6..390e9b8888 100644
--- a/lib/dns/dispatch.c
+++ b/lib/dns/dispatch.c
@@ -145,7 +145,8 @@ struct dns_dispatch {

 	uint_fast32_t requests; /*%< how many requests we have */

-	unsigned int timedout;
+	unsigned int timedout; /*%< how many timeouts/cancels we had, so we can
+				    ignore this many unexpected answers. */

 	struct cds_lfht_node ht_node;
 	struct rcu_head rcu_head;
@@ -854,7 +855,10 @@ tcp_recv(isc_nmhandle_t *handle, isc_result_t eresult, isc_region_t *region,
 			 */
 			result = ISC_R_CANCELED;
 		} else if (disp->timedout > 0) {
-			/* There was active query that timed-out before */
+			/*
+			 * There was an active query that timed-out or canceled
+			 * before.
+			 */
 			disp->timedout--;
 		} else {
 			result = ISC_R_UNEXPECTED;
@@ -876,6 +880,13 @@ tcp_recv(isc_nmhandle_t *handle, isc_result_t eresult, isc_region_t *region,
 		if (resp->timeout > 0) {
 			timeout = resp->timeout - dispentry_runtime(resp, &now);
 			if (timeout <= 0) {
+				/*
+				 * Count this so we can later ignore the
+				 * outstanding answer (if it eventually
+				 * arrives).
+				 */
+				disp->timedout++;
+
 				tcp_recv_add(&resps, resp, ISC_R_TIMEDOUT);
 			}
 		}
@@ -1779,6 +1790,13 @@ tcp_dispentry_cancel(dns_dispentry_t *resp, isc_result_t result) {

 	case DNS_DISPATCHSTATE_CONNECTED:
 		if (resp->reading) {
+			/*
+			 * This is not a timeout, but we still count it as one
+			 * so we can later ignore the outstanding answer (if it
+			 * happens to eventually arrive).
+			 */
+			disp->timedout++;
+
 			tcp_recv_add(&resps, resp, ISC_R_CANCELED);
 		}