]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
WiP: different refcounting, backtraces
authorWitold Kręcicki <wpk@isc.org>
Tue, 9 Jun 2020 20:04:31 +0000 (22:04 +0200)
committerWitold Kręcicki <wpk@isc.org>
Thu, 18 Jun 2020 08:04:10 +0000 (10:04 +0200)
lib/isc/netmgr/netmgr-int.h
lib/isc/netmgr/netmgr.c
lib/isc/netmgr/tcp.c
lib/isc/netmgr/tcpdns.c

index 9f26369715d92bd12fc10834859cc6c789c096f8..7131873a8aa6206ada08eedde216c337acc9d29c 100644 (file)
@@ -371,6 +371,8 @@ struct isc_nmsocket {
        isc_nmsocket_t *parent;
        /*% Listener socket this connection was accepted on */
        isc_nmsocket_t *listener;
+       /*% Self, for self-contained unreferenced sockets (tcpdns) */
+       isc_nmsocket_t *self;
 
        /*%
         * quota is the TCP client, attached when a TCP connection
index e9e23a457faf91515d2e4fde7b04d9de400e770f..5603d6c813943da656594e187018449bfdb0fa14 100644 (file)
 
 #include "netmgr-int.h"
 #include "uv-compat.h"
+#include <execinfo.h>
+
+#define BACKTRACE() do { void *arr[4]; backtrace_symbols_fd(arr, backtrace(arr, 4), 2); fprintf(stderr, "\n"); } while(0)
+
 
 /*%
  * How many isc_nmhandles and isc_nm_uvreqs will we be
@@ -719,6 +723,7 @@ fprintf(stderr, "attach sock %p (%s) (%p) to %lu\n", sock, isc__nm_socket_type(s
 fprintf(stderr, "attach sock %p (%s) to %lu\n", sock, isc__nm_socket_type(sock->type), sock->references+1);
                isc_refcount_increment0(&sock->references);
        }
+       BACKTRACE();
 
        *target = sock;
 }
@@ -764,6 +769,7 @@ nmsocket_cleanup(isc_nmsocket_t *sock, bool dofree) {
        if (sock->outerhandle != NULL) {
                isc_nmhandle_unref(sock->outerhandle);
 fprintf(stderr, "sock %p outerhandle %p, clearing?\n", sock, sock->outerhandle);
+       BACKTRACE();
                sock->outerhandle = NULL;
        }
 
@@ -927,6 +933,7 @@ fprintf(stderr, "detach sock %p (%s) (%p) to %lu\n", sock, isc__nm_socket_type(s
 fprintf(stderr, "detach sock %p (%s) to %lu\n", sock, isc__nm_socket_type(sock->type), sock->references-1);
                rsock = sock;
        }
+       BACKTRACE();
 
        if (isc_refcount_decrement(&rsock->references) == 1) {
                isc__nmsocket_prep_destroy(rsock);
@@ -955,6 +962,7 @@ isc__nmsocket_init(isc_nmsocket_t *sock, isc_nm_t *mgr, isc_nmsocket_type type,
 
        family = iface->addr.type.sa.sa_family;
 fprintf(stderr, "init sock %p (%s)\n", sock, isc__nm_socket_type(type));
+       BACKTRACE();
 
        *sock = (isc_nmsocket_t){ .type = type,
                                  .iface = iface,
@@ -1140,6 +1148,7 @@ isc_nmhandle_ref(isc_nmhandle_t *handle) {
        REQUIRE(VALID_NMHANDLE(handle));
 
 fprintf(stderr, "ref %p (%p %s) to %lu\n", handle, handle->sock, isc__nm_socket_type(handle->sock->type), handle->references+1);
+       BACKTRACE();
        isc_refcount_increment(&handle->references);
 }
 
@@ -1200,6 +1209,7 @@ isc_nmhandle_unref(isc_nmhandle_t *handle) {
        REQUIRE(VALID_NMHANDLE(handle));
 
 fprintf(stderr, "unref %p (%p %s) to %lu\n", handle, handle->sock, isc__nm_socket_type(handle->sock->type), handle->references-1);
+       BACKTRACE();
        if (isc_refcount_decrement(&handle->references) > 1) {
                return;
        }
@@ -1214,6 +1224,7 @@ fprintf(stderr, "unref %p (%p %s) to %lu\n", handle, handle->sock, isc__nm_socke
        sock = handle->sock;
        handle->sock = NULL;
 fprintf(stderr, "handle %p sock %p set to NULL\n", handle, sock);
+       BACKTRACE();
 
        if (handle->doreset != NULL) {
                handle->doreset(handle->opaque);
index d879fceeafb2fb6eaf7a020fe6b905726a495086..f2b487d2962ec3f03fdbbdcabf5cf02e44ead3d3 100644 (file)
@@ -511,7 +511,9 @@ readtimeout_cb(uv_timer_t *handle) {
        if (sock->quota) {
                isc_quota_detach(&sock->quota);
        }
-       sock->rcb.recv(sock->tcphandle, NULL, sock->rcbarg);
+       if (sock->rcb.recv != NULL) {
+               sock->rcb.recv(sock->tcphandle, NULL, sock->rcbarg);
+       }
 }
 
 isc_result_t
index b10b8daa42199de23f3f119b1cb8ec91f8c6e6ad..fa63be24942dd6c2d44e7a72440c120091597afb 100644 (file)
@@ -82,7 +82,9 @@ static void
 timer_close_cb(uv_handle_t *handle) {
        isc_nmsocket_t *sock = (isc_nmsocket_t *)uv_handle_get_data(handle);
        INSIST(VALID_NMSOCK(sock));
-fprintf(stderr, "timer close cb, sock %p\n", sock);
+       fprintf(stderr, "timer close cb, sock %p\n", sock);
+       atomic_store(&sock->closed, true);
+       tcpdns_close_direct(sock);
 }
 
 static void
@@ -92,7 +94,9 @@ dnstcp_readtimeout(uv_timer_t *timer) {
 
        REQUIRE(VALID_NMSOCK(sock));
        REQUIRE(sock->tid == isc_nm_tid());
-       tcpdns_close_direct(sock);
+       if (sock->self != NULL) {
+               isc__nmsocket_detach(&sock->self);
+       };
 }
 
 /*
@@ -125,6 +129,8 @@ fprintf(stderr, "dnslisten_acceptcb %p %s\n", handle, isc_result_totext(result))
        dnssock->extrahandlesize = dnslistensock->extrahandlesize;
        isc__nmsocket_attach(dnslistensock, &dnssock->listener);
 
+       isc__nmsocket_attach(dnssock, &dnssock->self);
+
        dnssock->outerhandle = handle;
        isc_nmhandle_ref(dnssock->outerhandle);
 
@@ -139,12 +145,13 @@ fprintf(stderr, "dnslisten_acceptcb %p %s\n", handle, isc_result_totext(result))
        dnssock->timer_initialized = true;
        uv_timer_start(&dnssock->timer, dnstcp_readtimeout,
                       dnssock->read_timeout, 0);
-
+       fprintf(stderr, "dnssock->refs %lu\n", dnssock->references);
        isc_nmhandle_ref(handle);
        result = isc_nm_read(handle, dnslisten_readcb, dnssock);
        if (result != ISC_R_SUCCESS) {
                isc_nmhandle_unref(handle);
        }
+       isc__nmsocket_detach(&dnssock);
 }
 
 /*
@@ -196,11 +203,6 @@ processbuffer(isc_nmsocket_t *dnssock, isc_nmhandle_t **handlep) {
                                dnssock->buf_len);
                }
 
-               /*
-                * dnssock is now attached to dnshandle
-                */
-               isc__nmsocket_detach(&dnssock);
-
                *handlep = dnshandle;
                return (ISC_R_SUCCESS);
        }
@@ -226,7 +228,10 @@ dnslisten_readcb(isc_nmhandle_t *handle, isc_region_t *region, void *arg) {
 fprintf(stderr, "dnslisten_readcb %p (reg %p), sock %p (%s)\n", handle, handle->sock, region, handle->sock ?  isc__nm_socket_type(handle->sock->type) : "nil");
        if (region == NULL) {
                /* Connection closed */
-               isc__nm_tcpdns_close(dnssock);
+               isc_nmhandle_unref(handle);
+               if (dnssock->self != NULL) {
+                       isc__nmsocket_detach(&dnssock->self);
+               };
                return;
        }
 
@@ -430,6 +435,7 @@ fprintf(stderr, "processed sequential sock %p with handle %p, result %s\n", sock
                        isc_nmhandle_unref(handle);
                } else if (sock->outerhandle != NULL) {
                        isc_nm_resumeread(sock->outerhandle->sock);
+                       
                }
 
                return;
@@ -509,6 +515,7 @@ isc__nm_tcpdns_send(isc_nmhandle_t *handle, isc_region_t *region,
        isc_mem_attach(sock->mgr->mctx, &t->mctx);
        t->orighandle = handle;
        isc_nmhandle_ref(t->orighandle);
+       isc_nmhandle_ref(t->handle);
 
        t->region = (isc_region_t){ .base = isc_mem_get(t->mctx,
                                                        region->length + 2),
@@ -524,7 +531,7 @@ static void
 tcpdns_close_direct(isc_nmsocket_t *sock) {
        REQUIRE(sock->tid == isc_nm_tid());
 
-fprintf(stderr, "close sock %p (tcpdns)\n", sock);
+fprintf(stderr, "close sock %p (tcpdns) %lu\n", sock, sock->references);
        /* We don't need atomics here, it's all in single network thread */
        if (sock->timer_initialized) {
                /*
@@ -537,6 +544,8 @@ fprintf(stderr, "close sock %p (tcpdns)\n", sock);
 fprintf(stderr, "closing timer\n");
                uv_close((uv_handle_t *)&sock->timer, timer_close_cb);
 fprintf(stderr, "...done\n");
+       } else if (sock->self != NULL) {
+               isc__nmsocket_detach(&sock->self);
        } else {
                /*
                 * At this point we're certain that there are no external
@@ -546,15 +555,15 @@ fprintf(stderr, "...done\n");
 fprintf(stderr, "clearing outerhandle\n");
                        sock->outerhandle->sock->rcb.recv = NULL;
                        isc_nmhandle_unref(sock->outerhandle);
-                       sock->outerhandle = NULL;
+                       sock->outerhandle = NULL;
                }
                if (sock->listener != NULL) {
 fprintf(stderr, "clearing listener\n");
                        isc__nmsocket_detach(&sock->listener);
                }
                atomic_store(&sock->closed, true);
-fprintf(stderr, "prep socket destruction\n");
                isc__nmsocket_prep_destroy(sock);
+fprintf(stderr, "prep socket destruction\n");
        }
 fprintf(stderr, "closed\n");
 }