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
This commit is contained in:
Cory McWilliams 2024-02-06 17:42:17 +00:00
parent eecfdf482f
commit 96167c3167
2 changed files with 20 additions and 1 deletions

View File

@ -211,6 +211,7 @@ typedef struct _tf_ssb_t
bool verbose; bool verbose;
bool store_debug_messages; bool store_debug_messages;
bool shutting_down;
int messages_stored; int messages_stored;
int blobs_stored; int blobs_stored;
@ -219,6 +220,7 @@ typedef struct _tf_ssb_t
tf_ssb_connection_t* connections; tf_ssb_connection_t* connections;
int connections_count; int connections_count;
int connection_ref_count;
int request_count; int request_count;
tf_ssb_connections_t* connections_tracker; 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; connection->debug_messages[i] = NULL;
} }
if (--connection->ssb->connection_ref_count == 0 &&
connection->ssb->shutting_down)
{
tf_ssb_destroy(connection->ssb);
}
tf_free(connection); 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) void tf_ssb_destroy(tf_ssb_t* ssb)
{ {
tf_printf("tf_ssb_destroy\n"); tf_printf("tf_ssb_destroy\n");
ssb->shutting_down = true;
tf_ssb_connections_destroy(ssb->connections_tracker); tf_ssb_connections_destroy(ssb->connections_tracker);
ssb->connections_tracker = NULL; ssb->connections_tracker = NULL;
@ -2540,7 +2548,11 @@ void tf_ssb_destroy(tf_ssb_t* ssb)
tf_free((void*)ssb->db_path); tf_free((void*)ssb->db_path);
tf_free(ssb->room_name); tf_free(ssb->room_name);
ssb->room_name = NULL; ssb->room_name = NULL;
tf_free(ssb);
if (ssb->connection_ref_count == 0)
{
tf_free(ssb);
}
} }
void tf_ssb_run(tf_ssb_t* 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; JSContext* context = ssb->context;
tf_ssb_connection_t* connection = tf_malloc(sizeof(tf_ssb_connection_t)); tf_ssb_connection_t* connection = tf_malloc(sizeof(tf_ssb_connection_t));
ssb->connection_ref_count++;
memset(connection, 0, sizeof(*connection)); memset(connection, 0, sizeof(*connection));
snprintf(connection->name, sizeof(connection->name), "cli%d", s_connection_index++); snprintf(connection->name, sizeof(connection->name), "cli%d", s_connection_index++);
connection->ssb = ssb; 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; JSContext* context = ssb->context;
tf_ssb_connection_t* tunnel = tf_malloc(sizeof(tf_ssb_connection_t)); tf_ssb_connection_t* tunnel = tf_malloc(sizeof(tf_ssb_connection_t));
ssb->connection_ref_count++;
memset(tunnel, 0, sizeof(*tunnel)); memset(tunnel, 0, sizeof(*tunnel));
snprintf(tunnel->name, sizeof(tunnel->name), "tun%d", s_tunnel_index++); snprintf(tunnel->name, sizeof(tunnel->name), "tun%d", s_tunnel_index++);
tunnel->ssb = ssb; 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)); tf_ssb_connection_t* connection = tf_malloc(sizeof(tf_ssb_connection_t));
ssb->connection_ref_count++;
memset(connection, 0, sizeof(*connection)); memset(connection, 0, sizeof(*connection));
snprintf(connection->name, sizeof(connection->name), "srv%d", s_connection_index++); snprintf(connection->name, sizeof(connection->name), "srv%d", s_connection_index++);
connection->ssb = ssb; connection->ssb = ssb;

View File

@ -46,12 +46,14 @@ bool tf_tls_context_set_certificate(tf_tls_context_t* context, const char* certi
BIO_puts(bio, certificate); BIO_puts(bio, certificate);
X509* x509 = PEM_read_bio_X509(bio, 0, 0, 0); X509* x509 = PEM_read_bio_X509(bio, 0, 0, 0);
result = SSL_CTX_use_certificate(context->context, x509); result = SSL_CTX_use_certificate(context->context, x509);
X509_free(x509);
while (true) while (true)
{ {
x509 = PEM_read_bio_X509(bio, 0, 0, 0); x509 = PEM_read_bio_X509(bio, 0, 0, 0);
if (x509) if (x509)
{ {
SSL_CTX_add_extra_chain_cert(context->context, x509); SSL_CTX_add_extra_chain_cert(context->context, x509);
X509_free(x509);
} }
else 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); BIO_puts(bio, private_key);
EVP_PKEY* key = PEM_read_bio_PrivateKey(bio, 0, 0, 0); EVP_PKEY* key = PEM_read_bio_PrivateKey(bio, 0, 0, 0);
result = SSL_CTX_use_PrivateKey(context->context, key); result = SSL_CTX_use_PrivateKey(context->context, key);
EVP_PKEY_free(key);
BIO_free(bio); BIO_free(bio);
return result == 1; 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) void tf_tls_context_destroy(tf_tls_context_t* context)
{ {
SSL_CTX_free(context->context); SSL_CTX_free(context->context);
OPENSSL_cleanup();
tf_free(context); tf_free(context);
} }