From cb083876c192fa32d2984508491bac96a4236137 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= <ondrej@isc.org>
Date: Tue, 17 Jan 2023 07:18:16 +0100
Subject: [PATCH] Detach the views in zone_shutdown(), not in zone_free()

The .view (and possibly .prev_view) would be kept attached to the
removed zone until the zone is fully removed from the memory in
zone_free().  If this process is delayed because server is busy
something else like doing constant `rndc reconfig`, it could take
seconds to detach the view, possibly keeping multiple dead views in the
memory.  This could quickly lead to a massive memory bloat.

Release the views early in the zone_shutdown() call, and don't wait
until the zone is freed.

Conflict: NA
Reference: https://gitlab.isc.org/isc-projects/bind9/-/commit/cb083876c192fa32d2984508491bac96a4236137

(cherry picked from commit 13bb8212804ce385010387d681a6623481921023)
---
 lib/dns/zone.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/lib/dns/zone.c b/lib/dns/zone.c
index 51e2fc6cf2..9b025cdcaf 100644
--- a/lib/dns/zone.c
+++ b/lib/dns/zone.c
@@ -1246,6 +1246,8 @@ zone_free(dns_zone_t *zone) {
 	INSIST(zone->readio == NULL);
 	INSIST(zone->statelist == NULL);
 	INSIST(zone->writeio == NULL);
+	INSIST(zone->view == NULL);
+	INSIST(zone->prev_view == NULL);
 
 	if (zone->task != NULL) {
 		isc_task_detach(&zone->task);
@@ -1253,12 +1255,6 @@ zone_free(dns_zone_t *zone) {
 	if (zone->loadtask != NULL) {
 		isc_task_detach(&zone->loadtask);
 	}
-	if (zone->view != NULL) {
-		dns_view_weakdetach(&zone->view);
-	}
-	if (zone->prev_view != NULL) {
-		dns_view_weakdetach(&zone->prev_view);
-	}
 
 	/* Unmanaged objects */
 	while (!ISC_LIST_EMPTY(zone->setnsec3param_queue)) {
@@ -14971,6 +14967,15 @@ zone_shutdown(isc_task_t *task, isc_event_t *event) {
 
 	LOCK_ZONE(zone);
 	INSIST(zone != zone->raw);
+
+	/* Detach the views early, we don't need them anymore */
+	if (zone->view != NULL) {
+		dns_view_weakdetach(&zone->view);
+	}
+	if (zone->prev_view != NULL) {
+		dns_view_weakdetach(&zone->prev_view);
+	}
+
 	if (linked) {
 		isc_refcount_decrement(&zone->irefs);
 	}
-- 
2.23.0
