e07df0
From 64d62856d251b58fe3350e9c3cf985a78debba5d Mon Sep 17 00:00:00 2001
e07df0
From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= <ondrej@isc.org>
e07df0
Date: Wed, 7 Aug 2019 05:46:45 -0400
e07df0
Subject: [PATCH] Merge branch '1148-deadlock-hangs-named-v9_11' into 'v9_11'
e07df0
e07df0
Convert (dns_view_t){ .weakrefs } to isc_refcount_t
e07df0
e07df0
See merge request isc-projects/bind9!2227
e07df0
e07df0
(cherry picked from commit e3903e83962c659bbac52454c816588c24078b5e)
e07df0
---
e07df0
 lib/dns/client.c           | 121 +++++++++++++++++--------------------
e07df0
 lib/dns/include/dns/view.h |   2 +-
e07df0
 lib/dns/view.c             |  88 +++++++++------------------
e07df0
 lib/isc/app_api.c          |  18 +++++-
e07df0
 4 files changed, 103 insertions(+), 126 deletions(-)
e07df0
e07df0
diff --git a/lib/dns/client.c b/lib/dns/client.c
e07df0
index b0dfa57dbe..e7eb40cd07 100644
e07df0
--- a/lib/dns/client.c
e07df0
+++ b/lib/dns/client.c
e07df0
@@ -17,6 +17,7 @@
e07df0
 #include <isc/buffer.h>
e07df0
 #include <isc/mem.h>
e07df0
 #include <isc/mutex.h>
e07df0
+#include <isc/refcount.h>
e07df0
 #include <isc/safe.h>
e07df0
 #include <isc/sockaddr.h>
e07df0
 #include <isc/socket.h>
e07df0
@@ -92,8 +93,9 @@ struct dns_client {
e07df0
 	unsigned int			find_timeout;
e07df0
 	unsigned int			find_udpretries;
e07df0
 
e07df0
+	isc_refcount_t			references;
e07df0
+
e07df0
 	/* Locked */
e07df0
-	unsigned int			references;
e07df0
 	dns_viewlist_t			viewlist;
e07df0
 	ISC_LIST(struct resctx)		resctxs;
e07df0
 	ISC_LIST(struct reqctx)		reqctxs;
e07df0
@@ -463,8 +465,7 @@ dns_client_createx2(isc_mem_t *mctx, isc_appctx_t *actx,
e07df0
 
e07df0
 	result = isc_mutex_init(&client->lock);
e07df0
 	if (result != ISC_R_SUCCESS) {
e07df0
-		isc_mem_put(mctx, client, sizeof(*client));
e07df0
-		return (result);
e07df0
+		goto cleanup_client;
e07df0
 	}
e07df0
 
e07df0
 	client->actx = actx;
e07df0
@@ -474,12 +475,14 @@ dns_client_createx2(isc_mem_t *mctx, isc_appctx_t *actx,
e07df0
 
e07df0
 	client->task = NULL;
e07df0
 	result = isc_task_create(client->taskmgr, 0, &client->task);
e07df0
-	if (result != ISC_R_SUCCESS)
e07df0
-		goto cleanup;
e07df0
+	if (result != ISC_R_SUCCESS) {
e07df0
+		goto cleanup_lock;
e07df0
+	}
e07df0
 
e07df0
 	result = dns_dispatchmgr_create(mctx, NULL, &dispatchmgr);
e07df0
-	if (result != ISC_R_SUCCESS)
e07df0
-		goto cleanup;
e07df0
+	if (result != ISC_R_SUCCESS) {
e07df0
+		goto cleanup_task;
e07df0
+	}
e07df0
 	client->dispatchmgr = dispatchmgr;
e07df0
 
e07df0
 	/*
e07df0
@@ -491,8 +494,9 @@ dns_client_createx2(isc_mem_t *mctx, isc_appctx_t *actx,
e07df0
 		result = getudpdispatch(AF_INET, dispatchmgr, socketmgr,
e07df0
 					taskmgr, ISC_TRUE,
e07df0
 					&dispatchv4, localaddr4);
e07df0
-		if (result == ISC_R_SUCCESS)
e07df0
+		if (result == ISC_R_SUCCESS) {
e07df0
 			client->dispatchv4 = dispatchv4;
e07df0
+		}
e07df0
 	}
e07df0
 
e07df0
 	client->dispatchv6 = NULL;
e07df0
@@ -500,22 +504,30 @@ dns_client_createx2(isc_mem_t *mctx, isc_appctx_t *actx,
e07df0
 		result = getudpdispatch(AF_INET6, dispatchmgr, socketmgr,
e07df0
 					taskmgr, ISC_TRUE,
e07df0
 					&dispatchv6, localaddr6);
e07df0
-		if (result == ISC_R_SUCCESS)
e07df0
+		if (result == ISC_R_SUCCESS) {
e07df0
 			client->dispatchv6 = dispatchv6;
e07df0
+		}
e07df0
 	}
e07df0
 
e07df0
 	/* We need at least one of the dispatchers */
e07df0
 	if (dispatchv4 == NULL && dispatchv6 == NULL) {
e07df0
 		INSIST(result != ISC_R_SUCCESS);
e07df0
-		goto cleanup;
e07df0
+		goto cleanup_dispatchmgr;
e07df0
+	}
e07df0
+
e07df0
+	result = isc_refcount_init(&client->references, 1);
e07df0
+	if (result != ISC_R_SUCCESS) {
e07df0
+		goto cleanup_dispatches;
e07df0
 	}
e07df0
 
e07df0
 	/* Create the default view for class IN */
e07df0
 	result = createview(mctx, dns_rdataclass_in, options, taskmgr,
e07df0
 			    RESOLVER_NTASKS, socketmgr, timermgr,
e07df0
 			    dispatchmgr, dispatchv4, dispatchv6, &view);
e07df0
-	if (result != ISC_R_SUCCESS)
e07df0
-		goto cleanup;
e07df0
+	if (result != ISC_R_SUCCESS) {
e07df0
+		goto cleanup_references;
e07df0
+	}
e07df0
+
e07df0
 	ISC_LIST_INIT(client->viewlist);
e07df0
 	ISC_LIST_APPEND(client->viewlist, view, link);
e07df0
 
e07df0
@@ -535,32 +547,38 @@ dns_client_createx2(isc_mem_t *mctx, isc_appctx_t *actx,
e07df0
 	client->find_udpretries = DEF_FIND_UDPRETRIES;
e07df0
 	client->attributes = 0;
e07df0
 
e07df0
-	client->references = 1;
e07df0
 	client->magic = DNS_CLIENT_MAGIC;
e07df0
 
e07df0
 	*clientp = client;
e07df0
 
e07df0
 	return (ISC_R_SUCCESS);
e07df0
 
e07df0
- cleanup:
e07df0
+ cleanup_references:
e07df0
+	isc_refcount_decrement(&client->references, NULL);
e07df0
+	isc_refcount_destroy(&client->references);
e07df0
+ cleanup_dispatches:
e07df0
 	if (dispatchv4 != NULL)
e07df0
 		dns_dispatch_detach(&dispatchv4);
e07df0
 	if (dispatchv6 != NULL)
e07df0
 		dns_dispatch_detach(&dispatchv6);
e07df0
-	if (dispatchmgr != NULL)
e07df0
-		dns_dispatchmgr_destroy(&dispatchmgr);
e07df0
-	if (client->task != NULL)
e07df0
-		isc_task_detach(&client->task);
e07df0
+ cleanup_dispatchmgr:
e07df0
+	dns_dispatchmgr_destroy(&dispatchmgr);
e07df0
+ cleanup_task:
e07df0
+	isc_task_detach(&client->task);
e07df0
+ cleanup_lock:
e07df0
+	DESTROYLOCK(&client->lock);
e07df0
+ cleanup_client:
e07df0
 	isc_mem_put(mctx, client, sizeof(*client));
e07df0
 
e07df0
 	return (result);
e07df0
 }
e07df0
 
e07df0
 static void
e07df0
-destroyclient(dns_client_t **clientp) {
e07df0
-	dns_client_t *client = *clientp;
e07df0
+destroyclient(dns_client_t *client) {
e07df0
 	dns_view_t *view;
e07df0
 
e07df0
+	isc_refcount_destroy(&client->references);
e07df0
+
e07df0
 	while ((view = ISC_LIST_HEAD(client->viewlist)) != NULL) {
e07df0
 		ISC_LIST_UNLINK(client->viewlist, view, link);
e07df0
 		dns_view_detach(&view);
e07df0
@@ -592,32 +610,22 @@ destroyclient(dns_client_t **clientp) {
e07df0
 	client->magic = 0;
e07df0
 
e07df0
 	isc_mem_putanddetach(&client->mctx, client, sizeof(*client));
e07df0
-
e07df0
-	*clientp = NULL;
e07df0
 }
e07df0
 
e07df0
 void
e07df0
 dns_client_destroy(dns_client_t **clientp) {
e07df0
 	dns_client_t *client;
e07df0
-	isc_boolean_t destroyok = ISC_FALSE;
e07df0
+	isc_uint32_t references;
e07df0
 
e07df0
 	REQUIRE(clientp != NULL);
e07df0
 	client = *clientp;
e07df0
+	*clientp = NULL;
e07df0
 	REQUIRE(DNS_CLIENT_VALID(client));
e07df0
 
e07df0
-	LOCK(&client->lock);
e07df0
-	client->references--;
e07df0
-	if (client->references == 0 && ISC_LIST_EMPTY(client->resctxs) &&
e07df0
-	    ISC_LIST_EMPTY(client->reqctxs) &&
e07df0
-	    ISC_LIST_EMPTY(client->updatectxs)) {
e07df0
-		destroyok = ISC_TRUE;
e07df0
+	isc_refcount_decrement(&client->references, &references);
e07df0
+	if (references == 0U) {
e07df0
+		destroyclient(client);
e07df0
 	}
e07df0
-	UNLOCK(&client->lock);
e07df0
-
e07df0
-	if (destroyok)
e07df0
-		destroyclient(&client);
e07df0
-
e07df0
-	*clientp = NULL;
e07df0
 }
e07df0
 
e07df0
 isc_result_t
e07df0
@@ -1407,6 +1415,7 @@ dns_client_startresolve(dns_client_t *client, dns_name_t *name,
e07df0
 	rctx->event = event;
e07df0
 
e07df0
 	rctx->magic = RCTX_MAGIC;
e07df0
+	isc_refcount_increment(&client->references, NULL);
e07df0
 
e07df0
 	LOCK(&client->lock);
e07df0
 	ISC_LIST_APPEND(client->resctxs, rctx, link);
e07df0
@@ -1477,10 +1486,10 @@ dns_client_destroyrestrans(dns_clientrestrans_t **transp) {
e07df0
 	resctx_t *rctx;
e07df0
 	isc_mem_t *mctx;
e07df0
 	dns_client_t *client;
e07df0
-	isc_boolean_t need_destroyclient = ISC_FALSE;
e07df0
 
e07df0
 	REQUIRE(transp != NULL);
e07df0
 	rctx = (resctx_t *)*transp;
e07df0
+	*transp = NULL;
e07df0
 	REQUIRE(RCTX_VALID(rctx));
e07df0
 	REQUIRE(rctx->fetch == NULL);
e07df0
 	REQUIRE(rctx->event == NULL);
e07df0
@@ -1502,11 +1511,6 @@ dns_client_destroyrestrans(dns_clientrestrans_t **transp) {
e07df0
 	INSIST(ISC_LINK_LINKED(rctx, link));
e07df0
 	ISC_LIST_UNLINK(client->resctxs, rctx, link);
e07df0
 
e07df0
-	if (client->references == 0 && ISC_LIST_EMPTY(client->resctxs) &&
e07df0
-	    ISC_LIST_EMPTY(client->reqctxs) &&
e07df0
-	    ISC_LIST_EMPTY(client->updatectxs))
e07df0
-		need_destroyclient = ISC_TRUE;
e07df0
-
e07df0
 	UNLOCK(&client->lock);
e07df0
 
e07df0
 	INSIST(ISC_LIST_EMPTY(rctx->namelist));
e07df0
@@ -1516,10 +1520,8 @@ dns_client_destroyrestrans(dns_clientrestrans_t **transp) {
e07df0
 
e07df0
 	isc_mem_put(mctx, rctx, sizeof(*rctx));
e07df0
 
e07df0
-	if (need_destroyclient)
e07df0
-		destroyclient(&client);
e07df0
+	dns_client_destroy(&client);
e07df0
 
e07df0
-	*transp = NULL;
e07df0
 }
e07df0
 
e07df0
 isc_result_t
e07df0
@@ -1793,6 +1795,7 @@ dns_client_startrequest(dns_client_t *client, dns_message_t *qmessage,
e07df0
 
e07df0
 	LOCK(&client->lock);
e07df0
 	ISC_LIST_APPEND(client->reqctxs, ctx, link);
e07df0
+	isc_refcount_increment(&client->references, NULL);
e07df0
 	UNLOCK(&client->lock);
e07df0
 
e07df0
 	ctx->request = NULL;
e07df0
@@ -1807,6 +1810,8 @@ dns_client_startrequest(dns_client_t *client, dns_message_t *qmessage,
e07df0
 		return (ISC_R_SUCCESS);
e07df0
 	}
e07df0
 
e07df0
+	isc_refcount_decrement(&client->references, NULL);
e07df0
+
e07df0
  cleanup:
e07df0
 	if (ctx != NULL) {
e07df0
 		LOCK(&client->lock);
e07df0
@@ -1847,10 +1852,10 @@ dns_client_destroyreqtrans(dns_clientreqtrans_t **transp) {
e07df0
 	reqctx_t *ctx;
e07df0
 	isc_mem_t *mctx;
e07df0
 	dns_client_t *client;
e07df0
-	isc_boolean_t need_destroyclient = ISC_FALSE;
e07df0
 
e07df0
 	REQUIRE(transp != NULL);
e07df0
 	ctx = (reqctx_t *)*transp;
e07df0
+	*transp = NULL;
e07df0
 	REQUIRE(REQCTX_VALID(ctx));
e07df0
 	client = ctx->client;
e07df0
 	REQUIRE(DNS_CLIENT_VALID(client));
e07df0
@@ -1865,12 +1870,6 @@ dns_client_destroyreqtrans(dns_clientreqtrans_t **transp) {
e07df0
 	INSIST(ISC_LINK_LINKED(ctx, link));
e07df0
 	ISC_LIST_UNLINK(client->reqctxs, ctx, link);
e07df0
 
e07df0
-	if (client->references == 0 && ISC_LIST_EMPTY(client->resctxs) &&
e07df0
-	    ISC_LIST_EMPTY(client->reqctxs) &&
e07df0
-	    ISC_LIST_EMPTY(client->updatectxs)) {
e07df0
-		need_destroyclient = ISC_TRUE;
e07df0
-	}
e07df0
-
e07df0
 	UNLOCK(&client->lock);
e07df0
 
e07df0
 	DESTROYLOCK(&ctx->lock);
e07df0
@@ -1878,10 +1877,7 @@ dns_client_destroyreqtrans(dns_clientreqtrans_t **transp) {
e07df0
 
e07df0
 	isc_mem_put(mctx, ctx, sizeof(*ctx));
e07df0
 
e07df0
-	if (need_destroyclient)
e07df0
-		destroyclient(&client);
e07df0
-
e07df0
-	*transp = NULL;
e07df0
+	dns_client_destroy(&client);
e07df0
 }
e07df0
 
e07df0
 /*%
e07df0
@@ -2971,6 +2967,7 @@ dns_client_startupdate(dns_client_t *client, dns_rdataclass_t rdclass,
e07df0
 
e07df0
 	LOCK(&client->lock);
e07df0
 	ISC_LIST_APPEND(client->updatectxs, uctx, link);
e07df0
+	isc_refcount_increment(&client->references, NULL);
e07df0
 	UNLOCK(&client->lock);
e07df0
 
e07df0
 	*transp = (dns_clientupdatetrans_t *)uctx;
e07df0
@@ -2989,6 +2986,8 @@ dns_client_startupdate(dns_client_t *client, dns_rdataclass_t rdclass,
e07df0
 	}
e07df0
 	if (result == ISC_R_SUCCESS)
e07df0
 		return (result);
e07df0
+
e07df0
+	isc_refcount_decrement(&client->references, NULL);
e07df0
 	*transp = NULL;
e07df0
 
e07df0
  fail:
e07df0
@@ -3046,11 +3045,11 @@ dns_client_destroyupdatetrans(dns_clientupdatetrans_t **transp) {
e07df0
 	updatectx_t *uctx;
e07df0
 	isc_mem_t *mctx;
e07df0
 	dns_client_t *client;
e07df0
-	isc_boolean_t need_destroyclient = ISC_FALSE;
e07df0
 	isc_sockaddr_t *sa;
e07df0
 
e07df0
 	REQUIRE(transp != NULL);
e07df0
 	uctx = (updatectx_t *)*transp;
e07df0
+	*transp = NULL;
e07df0
 	REQUIRE(UCTX_VALID(uctx));
e07df0
 	client = uctx->client;
e07df0
 	REQUIRE(DNS_CLIENT_VALID(client));
e07df0
@@ -3071,11 +3070,6 @@ dns_client_destroyupdatetrans(dns_clientupdatetrans_t **transp) {
e07df0
 	INSIST(ISC_LINK_LINKED(uctx, link));
e07df0
 	ISC_LIST_UNLINK(client->updatectxs, uctx, link);
e07df0
 
e07df0
-	if (client->references == 0 && ISC_LIST_EMPTY(client->resctxs) &&
e07df0
-	    ISC_LIST_EMPTY(client->reqctxs) &&
e07df0
-	    ISC_LIST_EMPTY(client->updatectxs))
e07df0
-		need_destroyclient = ISC_TRUE;
e07df0
-
e07df0
 	UNLOCK(&client->lock);
e07df0
 
e07df0
 	DESTROYLOCK(&uctx->lock);
e07df0
@@ -3083,10 +3077,7 @@ dns_client_destroyupdatetrans(dns_clientupdatetrans_t **transp) {
e07df0
 
e07df0
 	isc_mem_put(mctx, uctx, sizeof(*uctx));
e07df0
 
e07df0
-	if (need_destroyclient)
e07df0
-		destroyclient(&client);
e07df0
-
e07df0
-	*transp = NULL;
e07df0
+	dns_client_destroy(&client);
e07df0
 }
e07df0
 
e07df0
 isc_mem_t *
e07df0
diff --git a/lib/dns/include/dns/view.h b/lib/dns/include/dns/view.h
e07df0
index 7cd88f8377..e383c40f0f 100644
e07df0
--- a/lib/dns/include/dns/view.h
e07df0
+++ b/lib/dns/include/dns/view.h
e07df0
@@ -191,9 +191,9 @@ struct dns_view {
e07df0
 
e07df0
 	/* Locked by themselves. */
e07df0
 	isc_refcount_t			references;
e07df0
+	isc_refcount_t			weakrefs;
e07df0
 
e07df0
 	/* Locked by lock. */
e07df0
-	unsigned int			weakrefs;
e07df0
 	unsigned int			attributes;
e07df0
 	/* Under owner's locking control. */
e07df0
 	ISC_LINK(struct dns_view)	link;
e07df0
diff --git a/lib/dns/view.c b/lib/dns/view.c
e07df0
index f53193c3ec..db09c3db6e 100644
e07df0
--- a/lib/dns/view.c
e07df0
+++ b/lib/dns/view.c
e07df0
@@ -155,9 +155,13 @@ dns_view_create(isc_mem_t *mctx, dns_rdataclass_t rdclass,
e07df0
 	view->frozen = ISC_FALSE;
e07df0
 	view->task = NULL;
e07df0
 	result = isc_refcount_init(&view->references, 1);
e07df0
-	if (result != ISC_R_SUCCESS)
e07df0
+	if (result != ISC_R_SUCCESS) {
e07df0
 		goto cleanup_fwdtable;
e07df0
-	view->weakrefs = 0;
e07df0
+	}
e07df0
+	result = isc_refcount_init(&view->weakrefs, 1);
e07df0
+	if (result != ISC_R_SUCCESS) {
e07df0
+		goto cleanup_references;
e07df0
+	}
e07df0
 	view->attributes = (DNS_VIEWATTR_RESSHUTDOWN|DNS_VIEWATTR_ADBSHUTDOWN|
e07df0
 			    DNS_VIEWATTR_REQSHUTDOWN);
e07df0
 	view->statickeys = NULL;
e07df0
@@ -167,7 +171,7 @@ dns_view_create(isc_mem_t *mctx, dns_rdataclass_t rdclass,
e07df0
 	view->matchrecursiveonly = ISC_FALSE;
e07df0
 	result = dns_tsigkeyring_create(view->mctx, &view->dynamickeys);
e07df0
 	if (result != ISC_R_SUCCESS)
e07df0
-		goto cleanup_references;
e07df0
+		goto cleanup_weakrefs;
e07df0
 	view->peers = NULL;
e07df0
 	view->order = NULL;
e07df0
 	view->delonly = NULL;
e07df0
@@ -304,6 +308,10 @@ dns_view_create(isc_mem_t *mctx, dns_rdataclass_t rdclass,
e07df0
 	if (view->dynamickeys != NULL)
e07df0
 		dns_tsigkeyring_detach(&view->dynamickeys);
e07df0
 
e07df0
+ cleanup_weakrefs:
e07df0
+	isc_refcount_decrement(&view->weakrefs, NULL);
e07df0
+	isc_refcount_destroy(&view->weakrefs);
e07df0
+
e07df0
  cleanup_references:
e07df0
 	isc_refcount_destroy(&view->references);
e07df0
 
e07df0
@@ -336,12 +344,13 @@ destroy(dns_view_t *view) {
e07df0
 	dns_dlzdb_t *dlzdb;
e07df0
 
e07df0
 	REQUIRE(!ISC_LINK_LINKED(view, link));
e07df0
-	REQUIRE(isc_refcount_current(&view->references) == 0);
e07df0
-	REQUIRE(view->weakrefs == 0);
e07df0
 	REQUIRE(RESSHUTDOWN(view));
e07df0
 	REQUIRE(ADBSHUTDOWN(view));
e07df0
 	REQUIRE(REQSHUTDOWN(view));
e07df0
 
e07df0
+	isc_refcount_destroy(&view->references);
e07df0
+	isc_refcount_destroy(&view->weakrefs);
e07df0
+
e07df0
 	if (view->order != NULL)
e07df0
 		dns_order_detach(&view->order);
e07df0
 	if (view->peers != NULL)
e07df0
@@ -536,26 +545,12 @@ destroy(dns_view_t *view) {
e07df0
 	DESTROYLOCK(&view->new_zone_lock);
e07df0
 	DESTROYLOCK(&view->lock);
e07df0
 	isc_refcount_destroy(&view->references);
e07df0
+	isc_refcount_destroy(&view->weakrefs);
e07df0
 	isc_mem_free(view->mctx, view->nta_file);
e07df0
 	isc_mem_free(view->mctx, view->name);
e07df0
 	isc_mem_putanddetach(&view->mctx, view, sizeof(*view));
e07df0
 }
e07df0
 
e07df0
-/*
e07df0
- * Return true iff 'view' may be freed.
e07df0
- * The caller must be holding the view lock.
e07df0
- */
e07df0
-static isc_boolean_t
e07df0
-all_done(dns_view_t *view) {
e07df0
-
e07df0
-	if (isc_refcount_current(&view->references) == 0 &&
e07df0
-	    view->weakrefs == 0 &&
e07df0
-	    RESSHUTDOWN(view) && ADBSHUTDOWN(view) && REQSHUTDOWN(view))
e07df0
-		return (ISC_TRUE);
e07df0
-
e07df0
-	return (ISC_FALSE);
e07df0
-}
e07df0
-
e07df0
 void
e07df0
 dns_view_attach(dns_view_t *source, dns_view_t **targetp) {
e07df0
 
e07df0
@@ -571,10 +566,10 @@ static void
e07df0
 view_flushanddetach(dns_view_t **viewp, isc_boolean_t flush) {
e07df0
 	dns_view_t *view;
e07df0
 	unsigned int refs;
e07df0
-	isc_boolean_t done = ISC_FALSE;
e07df0
 
e07df0
 	REQUIRE(viewp != NULL);
e07df0
 	view = *viewp;
e07df0
+	*viewp = NULL;
e07df0
 	REQUIRE(DNS_VIEW_VALID(view));
e07df0
 
e07df0
 	if (flush)
e07df0
@@ -613,7 +608,6 @@ view_flushanddetach(dns_view_t **viewp, isc_boolean_t flush) {
e07df0
 		if (view->catzs != NULL) {
e07df0
 			dns_catz_catzs_detach(&view->catzs);
e07df0
 		}
e07df0
-		done = all_done(view);
e07df0
 		UNLOCK(&view->lock);
e07df0
 
e07df0
 		/* Need to detach zones outside view lock */
e07df0
@@ -622,12 +616,9 @@ view_flushanddetach(dns_view_t **viewp, isc_boolean_t flush) {
e07df0
 
e07df0
 		if (rdzone != NULL)
e07df0
 			dns_zone_detach(&rdzone);
e07df0
-	}
e07df0
-
e07df0
-	*viewp = NULL;
e07df0
 
e07df0
-	if (done)
e07df0
-		destroy(view);
e07df0
+		dns_view_weakdetach(&view);
e07df0
+	}
e07df0
 }
e07df0
 
e07df0
 void
e07df0
@@ -661,9 +652,7 @@ dns_view_weakattach(dns_view_t *source, dns_view_t **targetp) {
e07df0
 	REQUIRE(DNS_VIEW_VALID(source));
e07df0
 	REQUIRE(targetp != NULL && *targetp == NULL);
e07df0
 
e07df0
-	LOCK(&source->lock);
e07df0
-	source->weakrefs++;
e07df0
-	UNLOCK(&source->lock);
e07df0
+	isc_refcount_increment(&source->weakrefs, NULL);
e07df0
 
e07df0
 	*targetp = source;
e07df0
 }
e07df0
@@ -671,30 +660,22 @@ dns_view_weakattach(dns_view_t *source, dns_view_t **targetp) {
e07df0
 void
e07df0
 dns_view_weakdetach(dns_view_t **viewp) {
e07df0
 	dns_view_t *view;
e07df0
-	isc_boolean_t done = ISC_FALSE;
e07df0
+	unsigned int weakrefs;
e07df0
 
e07df0
 	REQUIRE(viewp != NULL);
e07df0
 	view = *viewp;
e07df0
 	REQUIRE(DNS_VIEW_VALID(view));
e07df0
-
e07df0
-	LOCK(&view->lock);
e07df0
-
e07df0
-	INSIST(view->weakrefs > 0);
e07df0
-	view->weakrefs--;
e07df0
-	done = all_done(view);
e07df0
-
e07df0
-	UNLOCK(&view->lock);
e07df0
-
e07df0
 	*viewp = NULL;
e07df0
 
e07df0
-	if (done)
e07df0
+	isc_refcount_decrement(&view->weakrefs, &weakrefs);
e07df0
+	if (weakrefs == 0) {
e07df0
 		destroy(view);
e07df0
+	}
e07df0
 }
e07df0
 
e07df0
 static void
e07df0
 resolver_shutdown(isc_task_t *task, isc_event_t *event) {
e07df0
 	dns_view_t *view = event->ev_arg;
e07df0
-	isc_boolean_t done;
e07df0
 
e07df0
 	REQUIRE(event->ev_type == DNS_EVENT_VIEWRESSHUTDOWN);
e07df0
 	REQUIRE(DNS_VIEW_VALID(view));
e07df0
@@ -705,20 +686,15 @@ resolver_shutdown(isc_task_t *task, isc_event_t *event) {
e07df0
 	isc_event_free(&event);
e07df0
 
e07df0
 	LOCK(&view->lock);
e07df0
-
e07df0
 	view->attributes |= DNS_VIEWATTR_RESSHUTDOWN;
e07df0
-	done = all_done(view);
e07df0
-
e07df0
 	UNLOCK(&view->lock);
e07df0
 
e07df0
-	if (done)
e07df0
-		destroy(view);
e07df0
+	dns_view_weakdetach(&view);
e07df0
 }
e07df0
 
e07df0
 static void
e07df0
 adb_shutdown(isc_task_t *task, isc_event_t *event) {
e07df0
 	dns_view_t *view = event->ev_arg;
e07df0
-	isc_boolean_t done;
e07df0
 
e07df0
 	REQUIRE(event->ev_type == DNS_EVENT_VIEWADBSHUTDOWN);
e07df0
 	REQUIRE(DNS_VIEW_VALID(view));
e07df0
@@ -729,20 +705,15 @@ adb_shutdown(isc_task_t *task, isc_event_t *event) {
e07df0
 	isc_event_free(&event);
e07df0
 
e07df0
 	LOCK(&view->lock);
e07df0
-
e07df0
 	view->attributes |= DNS_VIEWATTR_ADBSHUTDOWN;
e07df0
-	done = all_done(view);
e07df0
-
e07df0
 	UNLOCK(&view->lock);
e07df0
 
e07df0
-	if (done)
e07df0
-		destroy(view);
e07df0
+	dns_view_weakdetach(&view);
e07df0
 }
e07df0
 
e07df0
 static void
e07df0
 req_shutdown(isc_task_t *task, isc_event_t *event) {
e07df0
 	dns_view_t *view = event->ev_arg;
e07df0
-	isc_boolean_t done;
e07df0
 
e07df0
 	REQUIRE(event->ev_type == DNS_EVENT_VIEWREQSHUTDOWN);
e07df0
 	REQUIRE(DNS_VIEW_VALID(view));
e07df0
@@ -753,14 +724,10 @@ req_shutdown(isc_task_t *task, isc_event_t *event) {
e07df0
 	isc_event_free(&event);
e07df0
 
e07df0
 	LOCK(&view->lock);
e07df0
-
e07df0
 	view->attributes |= DNS_VIEWATTR_REQSHUTDOWN;
e07df0
-	done = all_done(view);
e07df0
-
e07df0
 	UNLOCK(&view->lock);
e07df0
 
e07df0
-	if (done)
e07df0
-		destroy(view);
e07df0
+	dns_view_weakdetach(&view);
e07df0
 }
e07df0
 
e07df0
 isc_result_t
e07df0
@@ -809,6 +776,7 @@ dns_view_createresolver(dns_view_t *view,
e07df0
 	event = &view->resevent;
e07df0
 	dns_resolver_whenshutdown(view->resolver, view->task, &event);
e07df0
 	view->attributes &= ~DNS_VIEWATTR_RESSHUTDOWN;
e07df0
+	isc_refcount_increment(&view->weakrefs, NULL);
e07df0
 
e07df0
 	result = isc_mem_create(0, 0, &mctx);
e07df0
 	if (result != ISC_R_SUCCESS) {
e07df0
@@ -826,6 +794,7 @@ dns_view_createresolver(dns_view_t *view,
e07df0
 	event = &view->adbevent;
e07df0
 	dns_adb_whenshutdown(view->adb, view->task, &event);
e07df0
 	view->attributes &= ~DNS_VIEWATTR_ADBSHUTDOWN;
e07df0
+	isc_refcount_increment(&view->weakrefs, NULL);
e07df0
 
e07df0
 	result = dns_requestmgr_create(view->mctx, timermgr, socketmgr,
e07df0
 				      dns_resolver_taskmgr(view->resolver),
e07df0
@@ -840,6 +809,7 @@ dns_view_createresolver(dns_view_t *view,
e07df0
 	event = &view->reqevent;
e07df0
 	dns_requestmgr_whenshutdown(view->requestmgr, view->task, &event);
e07df0
 	view->attributes &= ~DNS_VIEWATTR_REQSHUTDOWN;
e07df0
+	isc_refcount_increment(&view->weakrefs, NULL);
e07df0
 
e07df0
 	return (ISC_R_SUCCESS);
e07df0
 }
e07df0
diff --git a/lib/isc/app_api.c b/lib/isc/app_api.c
e07df0
index 5563c7cc0b..40ec2d93b8 100644
e07df0
--- a/lib/isc/app_api.c
e07df0
+++ b/lib/isc/app_api.c
e07df0
@@ -23,6 +23,7 @@
e07df0
 static isc_mutex_t createlock;
e07df0
 static isc_once_t once = ISC_ONCE_INIT;
e07df0
 static isc_appctxcreatefunc_t appctx_createfunc = NULL;
e07df0
+static isc_mutex_t runninglock;
e07df0
 static isc_boolean_t is_running = ISC_FALSE;
e07df0
 
e07df0
 #define ISCAPI_APPMETHODS_VALID(m) ISC_MAGIC_VALID(m, ISCAPI_APPMETHODS_MAGIC)
e07df0
@@ -30,6 +31,7 @@ static isc_boolean_t is_running = ISC_FALSE;
e07df0
 static void
e07df0
 initialize(void) {
e07df0
 	RUNTIME_CHECK(isc_mutex_init(&createlock) == ISC_R_SUCCESS);
e07df0
+	RUNTIME_CHECK(isc_mutex_init(&runninglock) == ISC_R_SUCCESS);
e07df0
 }
e07df0
 
e07df0
 isc_result_t
e07df0
@@ -196,9 +198,15 @@ isc_app_run() {
e07df0
 	if (isc_bind9) {
e07df0
 		isc_result_t result;
e07df0
 
e07df0
+		RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
e07df0
+
e07df0
+		LOCK(&runninglock);
e07df0
 		is_running = ISC_TRUE;
e07df0
+		UNLOCK(&runninglock);
e07df0
 		result = isc__app_run();
e07df0
+		LOCK(&runninglock);
e07df0
 		is_running = ISC_FALSE;
e07df0
+		UNLOCK(&runninglock);
e07df0
 
e07df0
 		return (result);
e07df0
 	}
e07df0
@@ -208,7 +216,15 @@ isc_app_run() {
e07df0
 
e07df0
 isc_boolean_t
e07df0
 isc_app_isrunning() {
e07df0
-	return (is_running);
e07df0
+	isc_boolean_t running;
e07df0
+
e07df0
+	RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
e07df0
+
e07df0
+	LOCK(&runninglock);
e07df0
+	running = is_running;
e07df0
+	UNLOCK(&runninglock);
e07df0
+
e07df0
+	return (running);
e07df0
 }
e07df0
 
e07df0
 isc_result_t
e07df0
-- 
e07df0
2.21.1
e07df0