]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
session2: generalize uv_count to count also iter_ctx refs
authorLukáš Ondráček <lukas.ondracek@nic.cz>
Tue, 23 Jul 2024 14:33:47 +0000 (16:33 +0200)
committerLukáš Ondráček <lukas.ondracek@nic.cz>
Tue, 23 Jul 2024 15:13:07 +0000 (17:13 +0200)
daemon/session2.c
daemon/session2.h

index 798d6f4d43aa5a6e1f5565b737c878878f685518..22a0902d547a1200c0bfca74cd55bd8484aa3173 100644 (file)
@@ -422,6 +422,7 @@ static int protolayer_iter_ctx_finish(struct protolayer_iter_ctx *ctx, int ret)
 
        mm_ctx_delete(&ctx->pool);
        free(ctx);
+       session2_unhandle(s);
 
        return ret;
 }
@@ -588,6 +589,8 @@ static int session2_submit(
 {
        if (session->closing)
                return kr_error(ECANCELED);
+       if (session->ref_count >= INT_MAX)
+               return kr_error(ETOOMANYREFS);
        if (kr_fails_assert(session->proto < KR_PROTO_COUNT))
                return kr_error(EFAULT);
 
@@ -622,6 +625,7 @@ static int session2_submit(
                .finished_cb = cb,
                .finished_cb_baton = baton
        };
+       session->ref_count++;
        if (had_comm_param) {
                struct comm_addr_storage *addrst = &ctx->comm_addr_storage;
                if (comm->src_addr) {
@@ -843,7 +847,7 @@ struct session2 *session2_new(enum session2_transport_type transport_type,
        ret = uv_timer_init(uv_default_loop(), &s->timer);
        kr_require(!ret);
        s->timer.data = s;
-       s->uv_count++; /* Session owns the timer */
+       s->ref_count++; /* Session owns the timer */
 
        /* Initialize the layer's session data */
        for (size_t i = 0; i < grp->num_layers; i++) {
@@ -885,13 +889,13 @@ static void session2_free(struct session2 *s)
 
 void session2_unhandle(struct session2 *s)
 {
-       if (kr_fails_assert(s->uv_count > 0)) {
+       if (kr_fails_assert(s->ref_count > 0)) {
                session2_free(s);
                return;
        }
 
-       s->uv_count--;
-       if (s->uv_count <= 0)
+       s->ref_count--;
+       if (s->ref_count <= 0)
                session2_free(s);
 }
 
index f10ed387b6f81fbfd25439589d8c46d64afe4066..fdcc1fc3ee3ab6fa3364f769114893a27b20a6ef 100644 (file)
@@ -799,8 +799,8 @@ struct session2 {
        struct wire_buf wire_buf;
        uint32_t log_id; /**< Session ID for logging. */
 
-       int uv_count; /**< Number of unclosed libUV handles owned by this
-                      * session. */
+       int ref_count; /**< Number of unclosed libUV handles owned by this
+                      * session + iteration contexts referencing the session. */
 
        /** Communication information. Typically written into by one of the
         * first layers facilitating transport protocol processing.
@@ -900,7 +900,7 @@ static inline struct session2 *session2_new_io(uv_handle_t *handle,
                        layer_param, layer_param_count, outgoing);
        s->transport.io.handle = handle;
        handle->data = s;
-       s->uv_count++; /* Session owns the handle */
+       s->ref_count++; /* Session owns the handle */
        return s;
 }