]> git.meshlink.io Git - meshlink/commitdiff
Fix memory leaks from timers.
authorGuus Sliepen <guus@meshlink.io>
Fri, 4 Oct 2019 19:06:33 +0000 (21:06 +0200)
committerGuus Sliepen <guus@meshlink.io>
Fri, 4 Oct 2019 19:07:57 +0000 (21:07 +0200)
src/event.c
src/meshlink.c
src/net.c
src/protocol.c

index 245e39ecb45f34a62d848071a7b251880a821e6d..f88112ff6dfea4f8a03e8a72d4090014e8b59f56 100644 (file)
@@ -129,6 +129,15 @@ void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timeval *tv) {
        if(!splay_insert_node(&loop->timeouts, &timeout->node)) {
                abort();
        }
+
+       loop->deletion = true;
+}
+
+static void timeout_disable(event_loop_t *loop, timeout_t *timeout) {
+       splay_unlink_node(&loop->timeouts, &timeout->node);
+       timeout->tv = (struct timeval) {
+               0, 0
+       };
 }
 
 void timeout_del(event_loop_t *loop, timeout_t *timeout) {
@@ -136,13 +145,12 @@ void timeout_del(event_loop_t *loop, timeout_t *timeout) {
                return;
        }
 
-       loop->deletion = true;
+       if(timerisset(&timeout->tv)) {
+               timeout_disable(loop, timeout);
+       }
 
-       splay_unlink_node(&loop->timeouts, &timeout->node);
-       timeout->cb = 0;
-       timeout->tv = (struct timeval) {
-               0, 0
-       };
+       timeout->cb = NULL;
+       loop->deletion = true;
 }
 
 static int signal_compare(const signal_t *a, const signal_t *b) {
@@ -173,6 +181,16 @@ static void pipe_init(event_loop_t *loop) {
        }
 }
 
+static void pipe_exit(event_loop_t *loop) {
+       io_del(loop, &loop->signalio);
+
+       close(loop->pipefd[0]);
+       close(loop->pipefd[1]);
+
+       loop->pipefd[0] = -1;
+       loop->pipefd[1] = -1;
+}
+
 void signal_trigger(event_loop_t *loop, signal_t *sig) {
        uint8_t signum = sig->signum;
        write(loop->pipefd[1], &signum, 1);
@@ -207,6 +225,10 @@ void signal_del(event_loop_t *loop, signal_t *sig) {
 
        splay_unlink_node(&loop->signals, &sig->node);
        sig->cb = NULL;
+
+       if(!loop->signals.count && loop->pipefd[0] != -1) {
+               pipe_exit(loop);
+       }
 }
 
 void idle_set(event_loop_t *loop, idle_cb_t cb, void *data) {
@@ -218,7 +240,6 @@ bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) {
        fd_set readable;
        fd_set writable;
 
-
        while(loop->running) {
                gettimeofday(&loop->now, NULL);
                struct timeval diff, it, *tv = NULL;
@@ -228,11 +249,8 @@ bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) {
                        timersub(&timeout->tv, &loop->now, &diff);
 
                        if(diff.tv_sec < 0) {
+                               timeout_disable(loop, timeout);
                                timeout->cb(loop, timeout->data);
-
-                               if(timercmp(&timeout->tv, &loop->now, <)) {
-                                       timeout_del(loop, timeout);
-                               }
                        } else {
                                tv = &diff;
                                break;
index bee8eee3b44ab94d7acacd6db6ce2dc73b2ec96f..a9988855e9493a77492852559e4ad4df4affc002 100644 (file)
@@ -3512,7 +3512,7 @@ void meshlink_set_dev_class_timeouts(meshlink_handle_t *mesh, dev_class_t devcla
 void handle_network_change(meshlink_handle_t *mesh, bool online) {
        (void)online;
 
-       if(!mesh->connections) {
+       if(!mesh->connections || !mesh->loop.running) {
                return;
        }
 
index 9dd42dd5acf678eeffd08e28f93a96404760b044..c7c6723c27d3eef7e9e797e8c8991b551617b141 100644 (file)
--- a/src/net.c
+++ b/src/net.c
@@ -706,15 +706,17 @@ int main_loop(meshlink_handle_t *mesh) {
        mesh->datafromapp.signum = 0;
        signal_add(&(mesh->loop), &(mesh->datafromapp), (signal_cb_t)meshlink_send_from_queue, mesh, mesh->datafromapp.signum);
 
-       if(!event_loop_run(&(mesh->loop), &(mesh->mesh_mutex))) {
+       if(!event_loop_run(&mesh->loop, &mesh->mesh_mutex)) {
                logger(mesh, MESHLINK_ERROR, "Error while waiting for input: %s", strerror(errno));
                abort();
+               signal_del(&mesh->loop, &mesh->datafromapp);
                timeout_del(&mesh->loop, &mesh->periodictimer);
                timeout_del(&mesh->loop, &mesh->pingtimer);
 
                return 1;
        }
 
+       signal_del(&mesh->loop, &mesh->datafromapp);
        timeout_del(&mesh->loop, &mesh->periodictimer);
        timeout_del(&mesh->loop, &mesh->pingtimer);
 
index fd180f4faafab84fd4c3bfedf0bf465dbf96b13c..92418f179c12165432d535a75031c7d9fc49e786 100644 (file)
@@ -200,10 +200,11 @@ static void age_past_requests(event_loop_t *loop, void *data) {
                logger(mesh, MESHLINK_DEBUG, "Aging past requests: deleted %d, left %d", deleted, left);
        }
 
-       if(left)
+       if(left) {
                timeout_set(&mesh->loop, &mesh->past_request_timeout, &(struct timeval) {
-               10, rand() % 100000
-       });
+                       10, rand() % 100000
+               });
+       }
 }
 
 bool seen_request(meshlink_handle_t *mesh, const char *request) {
@@ -216,16 +217,23 @@ bool seen_request(meshlink_handle_t *mesh, const char *request) {
                new = xmalloc(sizeof(*new));
                new->request = xstrdup(request);
                new->firstseen = mesh->loop.now.tv_sec;
+
+               if(!mesh->past_request_tree->head) {
+                       timeout_set(&mesh->loop, &mesh->past_request_timeout, &(struct timeval) {
+                               10, rand() % 100000
+                       });
+               }
+
                splay_insert(mesh->past_request_tree, new);
-               timeout_add(&mesh->loop, &mesh->past_request_timeout, age_past_requests, NULL, &(struct timeval) {
-                       10, rand() % 100000
-               });
                return false;
        }
 }
 
 void init_requests(meshlink_handle_t *mesh) {
        mesh->past_request_tree = splay_alloc_tree((splay_compare_t) past_request_compare, (splay_action_t) free_past_request);
+       timeout_add(&mesh->loop, &mesh->past_request_timeout, age_past_requests, NULL, &(struct timeval) {
+               0, 0
+       });
 }
 
 void exit_requests(meshlink_handle_t *mesh) {