From 21b00934403697a6ac98422b38a517d0833c946f Mon Sep 17 00:00:00 2001
From: Evan Hunt <each@isc.org>
Date: Tue, 4 Jan 2022 12:08:43 -0800
Subject: [PATCH] Prevent a shutdown race in catz_create_chg_task()

If a catz event is scheduled while the task manager was being
shut down, task-exclusive mode is unavailable. This needs to be
handled as an error rather than triggering an assertion.

(cherry picked from commit 973ac1d8912d8d885e9a002ffee4acbaf23e9c81)
Conflict: NA
Reference: https://gitlab.isc.org/isc-projects/bind9/-/commit/21b00934403697a6ac98422b38a517d0833c946f
---
 bin/named/server.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/bin/named/server.c b/bin/named/server.c
index 9c0f12f63f..cad992f873 100644
--- a/bin/named/server.c
+++ b/bin/named/server.c
@@ -2921,11 +2921,16 @@ static isc_result_t
 catz_create_chg_task(dns_catz_entry_t *entry, dns_catz_zone_t *origin,
 		     dns_view_t *view, isc_taskmgr_t *taskmgr, void *udata,
 		     isc_eventtype_t type) {
-	catz_chgzone_event_t *event;
-	isc_task_t *task;
+	catz_chgzone_event_t *event = NULL;
+	isc_task_t *task = NULL;
 	isc_result_t result;
 	isc_taskaction_t action = NULL;
 
+	result = isc_taskmgr_excltask(taskmgr, &task);
+	if (result != ISC_R_SUCCESS) {
+		return (result);
+	}
+
 	switch (type) {
 	case DNS_EVENT_CATZADDZONE:
 	case DNS_EVENT_CATZMODZONE:
@@ -2936,6 +2941,7 @@ catz_create_chg_task(dns_catz_entry_t *entry, dns_catz_zone_t *origin,
 		break;
 	default:
 		REQUIRE(0);
+		ISC_UNREACHABLE();
 	}
 
 	event = (catz_chgzone_event_t *)isc_event_allocate(
@@ -2946,13 +2952,11 @@ catz_create_chg_task(dns_catz_entry_t *entry, dns_catz_zone_t *origin,
 	event->origin = NULL;
 	event->view = NULL;
 	event->mod = (type == DNS_EVENT_CATZMODZONE);
+
 	dns_catz_entry_attach(entry, &event->entry);
 	dns_catz_zone_attach(origin, &event->origin);
 	dns_view_attach(view, &event->view);
 
-	task = NULL;
-	result = isc_taskmgr_excltask(taskmgr, &task);
-	REQUIRE(result == ISC_R_SUCCESS);
 	isc_task_send(task, ISC_EVENT_PTR(&event));
 	isc_task_detach(&task);
 
-- 
2.23.0

