From 96167c3167245c44816f873c2cee9e0d37c55066 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Tue, 6 Feb 2024 17:42:17 +0000 Subject: [PATCH] Fix more memory leaks and ssb shutdown order issues. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4825 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- src/ssb.c | 17 ++++++++++++++++- src/tls.c | 4 ++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/ssb.c b/src/ssb.c index 001acec2..ce4da015 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -211,6 +211,7 @@ typedef struct _tf_ssb_t bool verbose; bool store_debug_messages; + bool shutting_down; int messages_stored; int blobs_stored; @@ -219,6 +220,7 @@ typedef struct _tf_ssb_t tf_ssb_connection_t* connections; int connections_count; + int connection_ref_count; int request_count; tf_ssb_connections_t* connections_tracker; @@ -1921,6 +1923,11 @@ static void _tf_ssb_connection_destroy(tf_ssb_connection_t* connection, const ch connection->debug_messages[i] = NULL; } + if (--connection->ssb->connection_ref_count == 0 && + connection->ssb->shutting_down) + { + tf_ssb_destroy(connection->ssb); + } tf_free(connection); } } @@ -2368,6 +2375,7 @@ static void _tf_ssb_on_timer_close(uv_handle_t* handle) void tf_ssb_destroy(tf_ssb_t* ssb) { tf_printf("tf_ssb_destroy\n"); + ssb->shutting_down = true; tf_ssb_connections_destroy(ssb->connections_tracker); ssb->connections_tracker = NULL; @@ -2540,7 +2548,11 @@ void tf_ssb_destroy(tf_ssb_t* ssb) tf_free((void*)ssb->db_path); tf_free(ssb->room_name); ssb->room_name = NULL; - tf_free(ssb); + + if (ssb->connection_ref_count == 0) + { + tf_free(ssb); + } } void tf_ssb_run(tf_ssb_t* ssb) @@ -2590,6 +2602,7 @@ tf_ssb_connection_t* tf_ssb_connection_create(tf_ssb_t* ssb, const char* host, c JSContext* context = ssb->context; tf_ssb_connection_t* connection = tf_malloc(sizeof(tf_ssb_connection_t)); + ssb->connection_ref_count++; memset(connection, 0, sizeof(*connection)); snprintf(connection->name, sizeof(connection->name), "cli%d", s_connection_index++); connection->ssb = ssb; @@ -2665,6 +2678,7 @@ tf_ssb_connection_t* tf_ssb_connection_tunnel_create(tf_ssb_t* ssb, const char* JSContext* context = ssb->context; tf_ssb_connection_t* tunnel = tf_malloc(sizeof(tf_ssb_connection_t)); + ssb->connection_ref_count++; memset(tunnel, 0, sizeof(*tunnel)); snprintf(tunnel->name, sizeof(tunnel->name), "tun%d", s_tunnel_index++); tunnel->ssb = ssb; @@ -2768,6 +2782,7 @@ static void _tf_ssb_on_connection(uv_stream_t* stream, int status) } tf_ssb_connection_t* connection = tf_malloc(sizeof(tf_ssb_connection_t)); + ssb->connection_ref_count++; memset(connection, 0, sizeof(*connection)); snprintf(connection->name, sizeof(connection->name), "srv%d", s_connection_index++); connection->ssb = ssb; diff --git a/src/tls.c b/src/tls.c index 013f6568..7f09bb20 100644 --- a/src/tls.c +++ b/src/tls.c @@ -46,12 +46,14 @@ bool tf_tls_context_set_certificate(tf_tls_context_t* context, const char* certi BIO_puts(bio, certificate); X509* x509 = PEM_read_bio_X509(bio, 0, 0, 0); result = SSL_CTX_use_certificate(context->context, x509); + X509_free(x509); while (true) { x509 = PEM_read_bio_X509(bio, 0, 0, 0); if (x509) { SSL_CTX_add_extra_chain_cert(context->context, x509); + X509_free(x509); } else { @@ -69,6 +71,7 @@ bool tf_tls_context_set_private_key(tf_tls_context_t* context, const char* priva BIO_puts(bio, private_key); EVP_PKEY* key = PEM_read_bio_PrivateKey(bio, 0, 0, 0); result = SSL_CTX_use_PrivateKey(context->context, key); + EVP_PKEY_free(key); BIO_free(bio); return result == 1; } @@ -106,6 +109,7 @@ tf_tls_session_t* tf_tls_context_create_session(tf_tls_context_t* context) void tf_tls_context_destroy(tf_tls_context_t* context) { SSL_CTX_free(context->context); + OPENSSL_cleanup(); tf_free(context); }