From 8ecc07452e8cbe239cd3a0055c7e0669e43be08b Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sat, 5 Feb 2022 17:06:51 +0000 Subject: [PATCH] Fix socket leaks. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3818 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- src/socket.js.c | 69 +++++++++++++++++++++++++++++++++++-------------- src/ssb.c | 4 +-- 2 files changed, 52 insertions(+), 21 deletions(-) diff --git a/src/socket.js.c b/src/socket.js.c index 5c089483..da471a7d 100644 --- a/src/socket.js.c +++ b/src/socket.js.c @@ -169,28 +169,49 @@ JSValue _socket_create(JSContext* context, JSValueConst this_val, int argc, JSVa return _socket_create_internal(context)->_object; } +void _socket_close_internal(socket_t* socket) +{ + if (!JS_IsUndefined(socket->_onRead)) + { + JSValue value = socket->_onRead; + socket->_onRead = JS_UNDEFINED; + JS_FreeValue(tf_task_get_context(socket->_task), value); + } + if (!JS_IsUndefined(socket->_onError)) + { + JSValue value = socket->_onError; + socket->_onError = JS_UNDEFINED; + JS_FreeValue(tf_task_get_context(socket->_task), value); + } + if (!JS_IsUndefined(socket->_onConnect)) + { + JSValue value = socket->_onConnect; + socket->_onConnect = JS_UNDEFINED; + JS_FreeValue(tf_task_get_context(socket->_task), value); + } + if (socket->_tls) + { + tf_tls_session_destroy(socket->_tls); + socket->_tls = NULL; + } + if (!uv_is_closing((uv_handle_t*)&socket->_socket)) + { + uv_close((uv_handle_t*)&socket->_socket, _socket_onClose); + } + + if (!socket->_socket.data && + JS_IsUndefined(socket->_object)) + { + --_count; + free(socket); + } +} + void _socket_finalizer(JSRuntime *runtime, JSValue value) { socket_t* socket = JS_GetOpaque(value, _classId); - --_count; - free(socket); -} - -void _socket_close_internal(socket_t* socket) -{ - if (!uv_is_closing((uv_handle_t*)&socket->_socket)) - { - JS_FreeValue(tf_task_get_context(socket->_task), socket->_onRead); - socket->_onRead = JS_UNDEFINED; - JS_FreeValue(tf_task_get_context(socket->_task), socket->_onError); - socket->_onError = JS_UNDEFINED; - if (socket->_tls) - { - tf_tls_session_destroy(socket->_tls); - socket->_tls = NULL; - } - uv_close((uv_handle_t*)&socket->_socket, _socket_onClose); - } + socket->_object = JS_UNDEFINED; + _socket_close_internal(socket); } void _socket_reportError(socket_t* socket, const char* error) @@ -522,6 +543,7 @@ JSValue _socket_accept(JSContext* context, JSValueConst this_val, int argc, JSVa snprintf(error, sizeof(error), "uv_accept: %s", uv_strerror(status)); tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(context, error)); } + JS_FreeValue(context, client->_object); return result; } @@ -885,6 +907,7 @@ void _socket_onClose(uv_handle_t* handle) { --_open_count; socket_t* socket = handle->data; + handle->data = NULL; if (socket->_closePromise != -1) { promiseid_t promise = socket->_closePromise; @@ -892,6 +915,14 @@ void _socket_onClose(uv_handle_t* handle) socket->_connected = false; tf_task_resolve_promise(socket->_task, promise, JS_UNDEFINED); } + if (socket->_startTlsPromise != -1) + { + promiseid_t promise = socket->_startTlsPromise; + socket->_startTlsPromise = -1; + socket->_connected = false; + tf_task_resolve_promise(socket->_task, promise, JS_UNDEFINED); + } + _socket_close_internal(socket); } void _socket_onShutdown(uv_shutdown_t* request, int status) diff --git a/src/ssb.c b/src/ssb.c index dbbe7b3f..50bf0b3a 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -260,7 +260,7 @@ static void _tf_ssb_connection_on_tcp_alloc(uv_handle_t* handle, size_t suggeste { tf_ssb_connection_t* connection = handle->data; size_t malloc_size = sizeof(connection->recv_buffer) - connection->recv_size; - buf->base = malloc(malloc_size); + buf->base = malloc_size ? malloc(malloc_size) : NULL; buf->len = malloc_size; } @@ -2200,7 +2200,7 @@ static void _tf_ssb_on_broadcast_listener_alloc(uv_handle_t* handle, size_t sugg { tf_ssb_connection_t* connection = handle->data; size_t malloc_size = sizeof(connection->recv_buffer) - connection->recv_size; - buf->base = malloc(malloc_size); + buf->base = malloc_size ? malloc(malloc_size) : NULL; buf->len = malloc_size; }