From fe5a6033ef9a7c63055a0b273bcf61a0c6bb17f6 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sat, 9 Jul 2022 14:14:48 +0000 Subject: [PATCH] Close / clean up a socket if connect fails. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3920 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- src/socket.js.c | 4 ++++ src/tests.c | 27 ++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/socket.js.c b/src/socket.js.c index 919a66a0..fdd383fe 100644 --- a/src/socket.js.c +++ b/src/socket.js.c @@ -489,6 +489,7 @@ JSValue _socket_connect(JSContext* context, JSValueConst this_val, int argc, JSV char error[256]; snprintf(error, sizeof(error), "uv_getaddrinfo: %s", uv_strerror(result)); tf_task_reject_promise(socket->_task, data->promise, JS_ThrowInternalError(context, "%s", error)); + _socket_close_internal(socket); tf_free(data); } @@ -505,6 +506,7 @@ void _socket_onResolvedForConnect(uv_getaddrinfo_t* resolver, int status, struct char error[256]; snprintf(error, sizeof(error), "uv_getaddrinfo: %s", uv_strerror(status)); tf_task_reject_promise(data->socket->_task, data->promise, JS_ThrowInternalError(tf_task_get_context(data->socket->_task), "%s", error)); + _socket_close_internal(data->socket); } else { @@ -517,6 +519,7 @@ void _socket_onResolvedForConnect(uv_getaddrinfo_t* resolver, int status, struct char error[256]; snprintf(error, sizeof(error), "uv_tcp_connect: %s", uv_strerror(connectResult)); tf_task_reject_promise(data->socket->_task, data->promise, JS_ThrowInternalError(tf_task_get_context(data->socket->_task), "%s", error)); + _socket_close_internal(data->socket); tf_free(request); } } @@ -540,6 +543,7 @@ void _socket_onConnect(uv_connect_t* request, int status) char error[256]; snprintf(error, sizeof(error), "uv_tcp_connect: %s", uv_strerror(status)); tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(tf_task_get_context(socket->_task), "%s", error)); + _socket_close_internal(socket); } } tf_free(request); diff --git a/src/tests.c b/src/tests.c index 2d63298e..44c825ae 100644 --- a/src/tests.c +++ b/src/tests.c @@ -485,7 +485,32 @@ static void _test_socket(const tf_test_options_t* options) " s2.shutdown();\n" " });\n" "}).catch(function(e) {\n" - " printf('caught');\n" + " print('caught');\n" + " print(e);\n" + "});\n" + "var s3 = new Socket();\n" + "print('connecting s3');\n" + "print('before connect', s3.isConnected);\n" + "s3.onError(function(e) {\n" + " print('error');\n" + " print(e);\n" + "});\n" + "print('noDelay', s3.noDelay);\n" + "s3.noDelay = true;\n" + "s3.connect('0.0.0.0', 443).then(function() {\n" + " print('connected');\n" + " s3.read(function(data) {\n" + " print('read', data ? data.length : null);\n" + " });\n" + " return s3.startTls();\n" + "}).then(function() {\n" + " print('ready');\n" + " print(s3.peerName);\n" + " s3.write('GET / HTTP/1.0\\r\\nConnection: close\\r\\n\\r\\n').then(function() {\n" + " s3.shutdown();\n" + " });\n" + "}).catch(function(e) {\n" + " print('caught');\n" " print(e);\n" "});\n"); fclose(file);