From 818520216d85c7e86f05de76449782fabaa31fa1 Mon Sep 17 00:00:00 2001
From: Mark Andrews <marka@isc.org>
Date: Thu, 27 Aug 2020 14:41:30 +1000
Subject: [PATCH] Missing locks in ns_lwresd_shutdown.

WARNING: ThreadSanitizer: data race
  Read of size 8 at 0x000000000001 by main thread:
    #0 ns_lwresd_shutdown bin/named/lwresd.c:885:3
    #1 destroy_managers bin/named/./main.c:938:2
    #2 cleanup bin/named/./main.c:1346:2
    #3 main bin/named/./main.c:1594:2

  Previous write of size 8 at 0x000000000001 by thread T1 (mutexes: write M1):
    #0 configure_listener bin/named/lwresd.c:768:2
    #1 ns_lwresd_configure bin/named/lwresd.c:836:5
    #2 load_configuration bin/named/./server.c:8230:2
    #3 run_server bin/named/./server.c
    #4 dispatch lib/isc/task.c:1157:7
    #5 run lib/isc/task.c:1331:2
Conflict: NA
Reference: https://gitlab.isc.org/isc-projects/bind9/-/commit/818520216d85c7e86f05de76449782fabaa31fa1
---
 bin/named/lwresd.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/bin/named/lwresd.c b/bin/named/lwresd.c
index 1f834cb38f..499314110e 100644
--- a/bin/named/lwresd.c
+++ b/bin/named/lwresd.c
@@ -880,9 +880,13 @@ ns_lwresd_shutdown(void) {
 
 	RUNTIME_CHECK(isc_once_do(&once, initialize_mutex) == ISC_R_SUCCESS);
 
+	LOCK(&listeners_lock);
 	while (!ISC_LIST_EMPTY(listeners)) {
 		listener = ISC_LIST_HEAD(listeners);
 		ISC_LIST_UNLINK(listeners, listener, link);
+		UNLOCK(&listeners_lock);
 		ns_lwreslistener_detach(&listener);
+		LOCK(&listeners_lock);
 	}
+	UNLOCK(&listeners_lock);
 }
-- 
2.23.0

