4ba560
From 20424b3bfe8d3fae92c11a30e79aeffd26dc2891 Mon Sep 17 00:00:00 2001
4ba560
From: Aram Sargsyan <aram@isc.org>
4ba560
Date: Mon, 14 Nov 2022 12:18:06 +0000
4ba560
Subject: [PATCH] Cancel all fetch events in dns_resolver_cancelfetch()
4ba560
4ba560
Although 'dns_fetch_t' fetch can have two associated events, one for
4ba560
each of 'DNS_EVENT_FETCHDONE' and 'DNS_EVENT_TRYSTALE' types, the
4ba560
dns_resolver_cancelfetch() function is designed in a way that it
4ba560
expects only one existing event, which it must cancel, and when it
4ba560
happens so that 'stale-answer-client-timeout' is enabled and there
4ba560
are two events, only one of them is canceled, and it results in an
4ba560
assertion in dns_resolver_destroyfetch(), when it finds a dangling
4ba560
event.
4ba560
4ba560
Change the logic of dns_resolver_cancelfetch() function so that it
4ba560
cancels both the events (if they exist), and in the right order.
4ba560
4ba560
(cherry picked from commit ec2098ca35039e4f81fd0aa7c525eb960b8f47bf)
4ba560
---
4ba560
 lib/dns/resolver.c | 53 +++++++++++++++++++++++++++++++++++-----------
4ba560
 lib/ns/query.c     |  4 +++-
4ba560
 2 files changed, 44 insertions(+), 13 deletions(-)
4ba560
4ba560
diff --git a/lib/dns/resolver.c b/lib/dns/resolver.c
4ba560
index 18585b5..7cbfbb2 100644
4ba560
--- a/lib/dns/resolver.c
4ba560
+++ b/lib/dns/resolver.c
4ba560
@@ -11254,8 +11254,9 @@ void
4ba560
 dns_resolver_cancelfetch(dns_fetch_t *fetch) {
4ba560
 	fetchctx_t *fctx;
4ba560
 	dns_resolver_t *res;
4ba560
-	dns_fetchevent_t *event, *next_event;
4ba560
-	isc_task_t *etask;
4ba560
+	dns_fetchevent_t *event = NULL;
4ba560
+	dns_fetchevent_t *event_trystale = NULL;
4ba560
+	dns_fetchevent_t *event_fetchdone = NULL;
4ba560
 
4ba560
 	REQUIRE(DNS_FETCH_VALID(fetch));
4ba560
 	fctx = fetch->private;
4ba560
@@ -11267,32 +11268,60 @@ dns_resolver_cancelfetch(dns_fetch_t *fetch) {
4ba560
 	LOCK(&res->buckets[fctx->bucketnum].lock);
4ba560
 
4ba560
 	/*
4ba560
-	 * Find the completion event for this fetch (as opposed
4ba560
+	 * Find the events for this fetch (as opposed
4ba560
 	 * to those for other fetches that have joined the same
4ba560
-	 * fctx) and send it with result = ISC_R_CANCELED.
4ba560
+	 * fctx) and send them with result = ISC_R_CANCELED.
4ba560
 	 */
4ba560
-	event = NULL;
4ba560
 	if (fctx->state != fetchstate_done) {
4ba560
+		dns_fetchevent_t *next_event = NULL;
4ba560
 		for (event = ISC_LIST_HEAD(fctx->events); event != NULL;
4ba560
 		     event = next_event) {
4ba560
 			next_event = ISC_LIST_NEXT(event, ev_link);
4ba560
 			if (event->fetch == fetch) {
4ba560
 				ISC_LIST_UNLINK(fctx->events, event, ev_link);
4ba560
-				break;
4ba560
+				switch (event->ev_type) {
4ba560
+				case DNS_EVENT_TRYSTALE:
4ba560
+					INSIST(event_trystale == NULL);
4ba560
+					event_trystale = event;
4ba560
+					break;
4ba560
+				case DNS_EVENT_FETCHDONE:
4ba560
+					INSIST(event_fetchdone == NULL);
4ba560
+					event_fetchdone = event;
4ba560
+					break;
4ba560
+				default:
4ba560
+					ISC_UNREACHABLE();
4ba560
+				}
4ba560
+				if (event_trystale != NULL &&
4ba560
+				    event_fetchdone != NULL)
4ba560
+				{
4ba560
+					break;
4ba560
+				}
4ba560
 			}
4ba560
 		}
4ba560
 	}
4ba560
-	if (event != NULL) {
4ba560
-		etask = event->ev_sender;
4ba560
-		event->ev_sender = fctx;
4ba560
-		event->result = ISC_R_CANCELED;
4ba560
-		isc_task_sendanddetach(&etask, ISC_EVENT_PTR(&event));
4ba560
+
4ba560
+	/*
4ba560
+	 * The "trystale" event must be sent before the "fetchdone" event,
4ba560
+	 * because the latter clears the "recursing" query attribute, which is
4ba560
+	 * required by both events (handled by the same callback function).
4ba560
+	 */
4ba560
+	if (event_trystale != NULL) {
4ba560
+		isc_task_t *etask = event_trystale->ev_sender;
4ba560
+		event_trystale->ev_sender = fctx;
4ba560
+		event_trystale->result = ISC_R_CANCELED;
4ba560
+		isc_task_sendanddetach(&etask, ISC_EVENT_PTR(&event_trystale));
4ba560
 	}
4ba560
+	if (event_fetchdone != NULL) {
4ba560
+		isc_task_t *etask = event_fetchdone->ev_sender;
4ba560
+		event_fetchdone->ev_sender = fctx;
4ba560
+		event_fetchdone->result = ISC_R_CANCELED;
4ba560
+		isc_task_sendanddetach(&etask, ISC_EVENT_PTR(&event_fetchdone));
4ba560
+ 	}
4ba560
+
4ba560
 	/*
4ba560
 	 * The fctx continues running even if no fetches remain;
4ba560
 	 * the answer is still cached.
4ba560
 	 */
4ba560
-
4ba560
 	UNLOCK(&res->buckets[fctx->bucketnum].lock);
4ba560
 }
4ba560
 
4ba560
diff --git a/lib/ns/query.c b/lib/ns/query.c
4ba560
index f66bab4..4f61374 100644
4ba560
--- a/lib/ns/query.c
4ba560
+++ b/lib/ns/query.c
4ba560
@@ -6021,7 +6021,9 @@ fetch_callback(isc_task_t *task, isc_event_t *event) {
4ba560
 	CTRACE(ISC_LOG_DEBUG(3), "fetch_callback");
4ba560
 
4ba560
 	if (event->ev_type == DNS_EVENT_TRYSTALE) {
4ba560
-		query_lookup_stale(client);
4ba560
+		if (devent->result != ISC_R_CANCELED) {
4ba560
+			query_lookup_stale(client);
4ba560
+		}
4ba560
 		isc_event_free(ISC_EVENT_PTR(&event));
4ba560
 		return;
4ba560
 	}
4ba560
-- 
4ba560
2.39.1
4ba560