From fb8a3c9ab23c3820c706714603f066132959ab90 Mon Sep 17 00:00:00 2001
From: Mark Andrews <marka@isc.org>
Date: Thu, 27 Aug 2020 13:21:13 +1000
Subject: [PATCH] Remove optimisation on obtaining a headlock as it triggers a
 tsan.

WARNING: ThreadSanitizer: data race (pid=15898)
  Write of size 8 at 0x7b6400011818 by thread T9 (mutexes: write M1597):
    #0 get_client /builds/isc-projects/bind9/bin/named/client.c:3876:3 (named+0x4db171)
    #1 ns_client_replace /builds/isc-projects/bind9/bin/named/client.c:3710:12 (named+0x4d737b)
    #2 query_recurse /builds/isc-projects/bind9/bin/named/query.c:4325:13 (named+0x4ff469)
    #3 query_find /builds/isc-projects/bind9/bin/named/query.c (named+0x4fb949)
    #4 ns_query_start /builds/isc-projects/bind9/bin/named/query.c:9675:8 (named+0x4f37cb)
    #5 client_request /builds/isc-projects/bind9/bin/named/client.c:3112:3 (named+0x4de9ef)
    #6 dispatch /builds/isc-projects/bind9/lib/isc/task.c:1157:7 (libisc.so.1107+0x50845)
    #7 run /builds/isc-projects/bind9/lib/isc/task.c:1331:2 (libisc.so.1107+0x4d799)

  Previous read of size 8 at 0x7b6400011818 by thread T2:
    #0 exit_check /builds/isc-projects/bind9/bin/named/client.c:698:5 (named+0x4d5d22)
    #1 ns_client_detach /builds/isc-projects/bind9/bin/named/client.c:3687:8 (named+0x4d7762)
    #2 query_find /builds/isc-projects/bind9/bin/named/query.c (named+0x4f9021)
    #3 query_resume /builds/isc-projects/bind9/bin/named/query.c:4164:12 (named+0x509b68)
    #4 dispatch /builds/isc-projects/bind9/lib/isc/task.c:1157:7 (libisc.so.1107+0x50845)
    #5 run /builds/isc-projects/bind9/lib/isc/task.c:1331:2 (libisc.so.1107+0x4d799)
Conflict: NA
Reference: https://gitlab.isc.org/isc-projects/bind9/-/commit/fb8a3c9ab23c3820c706714603f066132959ab90
---
 lib/isc/include/isc/queue.h | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/lib/isc/include/isc/queue.h b/lib/isc/include/isc/queue.h
index 210f302c84..d682ba4940 100644
--- a/lib/isc/include/isc/queue.h
+++ b/lib/isc/include/isc/queue.h
@@ -93,12 +93,8 @@
 	do { \
 		bool headlocked = false; \
 		ISC_QLINK_INSIST(!ISC_QLINK_LINKED(elt, link)); \
-		if ((queue).head == NULL) { \
-			LOCK(&(queue).headlock); \
-			headlocked = true; \
-		} \
 		LOCK(&(queue).taillock); \
-		if ((queue).tail == NULL && !headlocked) { \
+		if ((queue).tail == NULL) { \
 			UNLOCK(&(queue).taillock); \
 			LOCK(&(queue).headlock); \
 			LOCK(&(queue).taillock); \
-- 
2.23.0

