From 9ec7d78d164991b668cd50371ffe8f9b7b4b5ac3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= <ondrej@isc.org>
Date: Wed, 5 Jan 2022 11:48:22 +0100
Subject: [PATCH] On shutdown, return ISC_R_SHUTTINGDOWN from
 isc_taskmgr_excltask()

The isc_taskmgr_excltask() would return ISC_R_NOTFOUND either when the
exclusive task was not set (yet) or when the taskmgr is shutting down
and the exclusive task has been already cleared.

Distinguish between the two states and return ISC_R_SHUTTINGDOWN when
the taskmgr is being shut down instead of ISC_R_NOTFOUND.

(cherry picked from commit f9d90159b84831fd83d74594827fedf0f4e9e265)
Conflict: NA
Reference: https://gitlab.isc.org/isc-projects/bind9/-/commit/9ec7d78d164991b668cd50371ffe8f9b7b4b5ac3
---
 lib/isc/task.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/lib/isc/task.c b/lib/isc/task.c
index 2c0d89c2b9..3e894f8dab 100644
--- a/lib/isc/task.c
+++ b/lib/isc/task.c
@@ -1130,7 +1130,7 @@ isc_taskmgr_setexcltask(isc_taskmgr_t *mgr, isc_task_t *task) {
 
 isc_result_t
 isc_taskmgr_excltask(isc_taskmgr_t *mgr, isc_task_t **taskp) {
-	isc_result_t result = ISC_R_SUCCESS;
+	isc_result_t result;
 
 	REQUIRE(VALID_MANAGER(mgr));
 	REQUIRE(taskp != NULL && *taskp == NULL);
@@ -1138,6 +1138,9 @@ isc_taskmgr_excltask(isc_taskmgr_t *mgr, isc_task_t **taskp) {
 	LOCK(&mgr->excl_lock);
 	if (mgr->excl != NULL) {
 		isc_task_attach(mgr->excl, taskp);
+		result = ISC_R_SUCCESS;
+	} else if (atomic_load_relaxed(&mgr->exiting)) {
+		result = ISC_R_SHUTTINGDOWN;
 	} else {
 		result = ISC_R_NOTFOUND;
 	}
-- 
2.23.0

