]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Convert netmgr handle detach to synchronous callback
authorOndřej Surý <ondrej@isc.org>
Thu, 23 Mar 2023 07:55:29 +0000 (08:55 +0100)
committerOndřej Surý <ondrej@isc.org>
Fri, 24 Mar 2023 06:58:52 +0000 (07:58 +0100)
Instead of calling isc__nmhandle_detach calling
nmhandle_detach_cb() asynchronously when there's closehandle_cb
initialized, convert the closehandle_cb to use isc_job, and make the
isc__nmhandle_detach() to be fully synchronous.

lib/isc/netmgr/netmgr-int.h
lib/isc/netmgr/netmgr.c

index 3432604a2d731d1045250d1e0e57b3575376ac9a..6f7fc10a5644567c6e4974494afe21867f6dd267 100644 (file)
@@ -274,8 +274,6 @@ typedef enum isc__netievent_type {
 
        netievent_tcplisten,
        netievent_tcpstop,
-
-       netievent_detach,
 } isc__netievent_type;
 
 typedef union {
@@ -1728,7 +1726,6 @@ NETIEVENT_SOCKET_REQ_TYPE(tlssend);
 
 NETIEVENT_SOCKET_REQ_RESULT_TYPE(sendcb);
 
-NETIEVENT_SOCKET_HANDLE_TYPE(detach);
 NETIEVENT_SOCKET_HANDLE_TYPE(udpcancel);
 
 NETIEVENT_SOCKET_QUOTA_TYPE(tcpaccept);
@@ -1760,7 +1757,6 @@ NETIEVENT_SOCKET_REQ_DECL(tlssend);
 NETIEVENT_SOCKET_REQ_RESULT_DECL(sendcb);
 
 NETIEVENT_SOCKET_HANDLE_DECL(udpcancel);
-NETIEVENT_SOCKET_DECL(detach);
 
 NETIEVENT_SOCKET_QUOTA_DECL(tcpaccept);
 
index ecbc7b3c720658c64ce578bf4c9ddd9320975b48..a632a1f715ff84d00bd6a5e777b500895249a5c5 100644 (file)
@@ -22,6 +22,7 @@
 #include <isc/buffer.h>
 #include <isc/condition.h>
 #include <isc/errno.h>
+#include <isc/job.h>
 #include <isc/list.h>
 #include <isc/log.h>
 #include <isc/loop.h>
@@ -129,16 +130,10 @@ nmhandle_free(isc_nmsocket_t *sock, isc_nmhandle_t *handle);
 static void
 process_netievent(void *arg);
 
-static void
-isc__nm_async_detach(isc__networker_t *worker, isc__netievent_t *ev0);
-
 /*%<
  * Issue a 'handle closed' callback on the socket.
  */
 
-static void
-nmhandle_detach_cb(isc_nmhandle_t **handlep FLARG);
-
 static void
 shutdown_walk_cb(uv_handle_t *handle, void *arg);
 
@@ -465,8 +460,6 @@ process_netievent(void *arg) {
 
                NETIEVENT_CASE(settlsctx);
                NETIEVENT_CASE(sockstop);
-
-               NETIEVENT_CASE(detach);
        default:
                UNREACHABLE();
        }
@@ -508,8 +501,6 @@ NETIEVENT_SOCKET_HTTP_EPS_DEF(httpendpoints);
 
 NETIEVENT_SOCKET_REQ_DEF(tlssend);
 
-NETIEVENT_SOCKET_DEF(detach);
-
 NETIEVENT_SOCKET_QUOTA_DEF(tcpaccept);
 
 NETIEVENT_SOCKET_DEF(streamdnsread);
@@ -1123,60 +1114,19 @@ nmhandle_deactivate(isc_nmsocket_t *sock, isc_nmhandle_t *handle) {
        }
 }
 
-void
-isc__nmhandle_detach(isc_nmhandle_t **handlep FLARG) {
-       isc_nmsocket_t *sock = NULL;
-       isc_nmhandle_t *handle = NULL;
-
-       REQUIRE(handlep != NULL);
-       REQUIRE(VALID_NMHANDLE(*handlep));
+static void
+isc__nm_closehandle_job(void *arg) {
+       isc_nmsocket_t *sock = arg;
 
-       handle = *handlep;
-       *handlep = NULL;
+       sock->closehandle_cb(sock);
 
-       /*
-        * If the closehandle_cb is set, it needs to run asynchronously to
-        * ensure correct ordering of the isc__nm_process_sock_buffer().
-        */
-       sock = handle->sock;
-       if (sock->tid == isc_tid() && sock->closehandle_cb == NULL) {
-               nmhandle_detach_cb(&handle FLARG_PASS);
-       } else {
-               isc__netievent_detach_t *event =
-                       isc__nm_get_netievent_detach(sock->worker, sock);
-               /*
-                * we are using implicit "attach" as the last reference
-                * need to be destroyed explicitly in the async callback
-                */
-               event->handle = handle;
-               FLARG_IEVENT_PASS(event);
-               isc__nm_enqueue_ievent(sock->worker, (isc__netievent_t *)event);
-       }
+       isc__nmsocket_detach(&sock);
 }
 
 static void
-nmhandle_detach_cb(isc_nmhandle_t **handlep FLARG) {
-       isc_nmsocket_t *sock = NULL;
-       isc_nmhandle_t *handle = NULL;
-
-       REQUIRE(handlep != NULL);
-       REQUIRE(VALID_NMHANDLE(*handlep));
-
-       handle = *handlep;
-       *handlep = NULL;
-
-       NETMGR_TRACE_LOG("isc__nmhandle_detach():%p->references = %" PRIuFAST32
-                        "\n",
-                        handle, isc_refcount_current(&handle->references) - 1);
-
-       if (isc_refcount_decrement(&handle->references) > 1) {
-               return;
-       }
-
-       /* We need an acquire memory barrier here */
-       (void)isc_refcount_current(&handle->references);
+nmhandle_destroy(isc_nmhandle_t *handle) {
+       isc_nmsocket_t *sock = handle->sock;
 
-       sock = handle->sock;
        handle->sock = NULL;
 
        if (handle->doreset != NULL) {
@@ -1199,13 +1149,35 @@ nmhandle_detach_cb(isc_nmhandle_t **handlep FLARG) {
        /*
         * The handle is gone now. If the socket has a callback configured
         * for that (e.g., to perform cleanup after request processing),
-        * call it now..
+        * call it now asynchronously.
         */
        if (sock->closehandle_cb != NULL) {
-               sock->closehandle_cb(sock);
+               isc_job_run(sock->worker->netmgr->loopmgr,
+                           isc__nm_closehandle_job, sock);
+       } else {
+               isc___nmsocket_detach(&sock FLARG_PASS);
        }
+}
 
-       isc___nmsocket_detach(&sock FLARG_PASS);
+void
+isc__nmhandle_detach(isc_nmhandle_t **handlep FLARG) {
+       isc_nmhandle_t *handle = NULL;
+
+       REQUIRE(handlep != NULL);
+       REQUIRE(VALID_NMHANDLE(*handlep));
+
+       handle = *handlep;
+       *handlep = NULL;
+
+       REQUIRE(handle->sock->tid == isc_tid());
+
+       NETMGR_TRACE_LOG("isc__nmhandle_detach():%p->references = %" PRIuFAST32
+                        "\n",
+                        handle, isc_refcount_current(&handle->references) - 1);
+
+       if (isc_refcount_decrement(&handle->references) == 1) {
+               nmhandle_destroy(handle);
+       }
 }
 
 void *
@@ -2087,20 +2059,6 @@ isc__nm_sendcb(isc_nmsocket_t *sock, isc__nm_uvreq_t *uvreq,
        isc_job_run(sock->worker->netmgr->loopmgr, isc__nm_sendcb_job, uvreq);
 }
 
-void
-isc__nm_async_detach(isc__networker_t *worker, isc__netievent_t *ev0) {
-       isc__netievent_detach_t *ievent = (isc__netievent_detach_t *)ev0;
-       FLARG_IEVENT(ievent);
-
-       REQUIRE(VALID_NMSOCK(ievent->sock));
-       REQUIRE(VALID_NMHANDLE(ievent->handle));
-       REQUIRE(ievent->sock->tid == isc_tid());
-
-       UNUSED(worker);
-
-       nmhandle_detach_cb(&ievent->handle FLARG_PASS);
-}
-
 static void
 reset_shutdown(uv_handle_t *handle) {
        isc_nmsocket_t *sock = uv_handle_get_data(handle);