From 9614d03bef4827e594a717aefe63f94a97563ccf Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Wed, 30 Oct 2024 19:32:24 -0400 Subject: [PATCH] ssb: Fix a timer leak I observed trying to wrap up 0.0.24. --- src/ssb.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/ssb.c b/src/ssb.c index b0f0b1b6..06efd6a4 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -2450,6 +2450,20 @@ static void _tf_ssb_on_handle_close(uv_handle_t* handle) static void _tf_ssb_on_timer_close(uv_handle_t* handle) { + tf_ssb_timer_t* timer = handle->data; + for (int i = 0; i < timer->ssb->timers_count; i++) + { + if (timer->ssb->timers[i] == timer) + { + timer->ssb->timers[i] = timer->ssb->timers[--timer->ssb->timers_count]; + break; + } + } + if (timer->ssb->shutting_down && !timer->ssb->timers_count) + { + tf_free(timer->ssb->timers); + timer->ssb->timers = NULL; + } tf_free(handle->data); } @@ -2503,14 +2517,11 @@ void tf_ssb_destroy(tf_ssb_t* ssb) { uv_close((uv_handle_t*)&ssb->timers[i]->timer, _tf_ssb_on_timer_close); } - ssb->timers_count = 0; - tf_free(ssb->timers); - ssb->timers = NULL; tf_printf("Waiting for closes.\n"); while (ssb->broadcast_listener.data || ssb->broadcast_sender.data || ssb->broadcast_timer.data || ssb->broadcast_cleanup_timer.data || ssb->trace_timer.data || - ssb->server.data || ssb->ref_count || ssb->request_activity_timer.data) + ssb->server.data || ssb->ref_count || ssb->request_activity_timer.data || ssb->timers_count) { uv_run(ssb->loop, UV_RUN_ONCE); } @@ -4197,19 +4208,16 @@ static void _tf_ssb_scheduled_timer(uv_timer_t* handle) { tf_ssb_timer_t* timer = handle->data; timer->callback(timer->ssb, timer->user_data); - for (int i = 0; i < timer->ssb->timers_count; i++) - { - if (timer->ssb->timers[i] == timer) - { - timer->ssb->timers[i] = timer->ssb->timers[--timer->ssb->timers_count]; - break; - } - } uv_close((uv_handle_t*)handle, _tf_ssb_on_timer_close); } void tf_ssb_schedule_work(tf_ssb_t* ssb, int delay_ms, void (*callback)(tf_ssb_t* ssb, void* user_data), void* user_data) { + if (ssb->shutting_down) + { + return; + } + ssb->timers = tf_resize_vec(ssb->timers, sizeof(uv_timer_t*) * (ssb->timers_count + 1)); tf_ssb_timer_t* timer = tf_malloc(sizeof(tf_ssb_timer_t)); *timer = (tf_ssb_timer_t)