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
This commit is contained in:
Cory McWilliams 2022-07-09 14:14:48 +00:00
parent ff2a0f0c3f
commit fe5a6033ef
2 changed files with 30 additions and 1 deletions

View File

@ -489,6 +489,7 @@ JSValue _socket_connect(JSContext* context, JSValueConst this_val, int argc, JSV
char error[256]; char error[256];
snprintf(error, sizeof(error), "uv_getaddrinfo: %s", uv_strerror(result)); snprintf(error, sizeof(error), "uv_getaddrinfo: %s", uv_strerror(result));
tf_task_reject_promise(socket->_task, data->promise, JS_ThrowInternalError(context, "%s", error)); tf_task_reject_promise(socket->_task, data->promise, JS_ThrowInternalError(context, "%s", error));
_socket_close_internal(socket);
tf_free(data); tf_free(data);
} }
@ -505,6 +506,7 @@ void _socket_onResolvedForConnect(uv_getaddrinfo_t* resolver, int status, struct
char error[256]; char error[256];
snprintf(error, sizeof(error), "uv_getaddrinfo: %s", uv_strerror(status)); 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)); 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 else
{ {
@ -517,6 +519,7 @@ void _socket_onResolvedForConnect(uv_getaddrinfo_t* resolver, int status, struct
char error[256]; char error[256];
snprintf(error, sizeof(error), "uv_tcp_connect: %s", uv_strerror(connectResult)); 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)); 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); tf_free(request);
} }
} }
@ -540,6 +543,7 @@ void _socket_onConnect(uv_connect_t* request, int status)
char error[256]; char error[256];
snprintf(error, sizeof(error), "uv_tcp_connect: %s", uv_strerror(status)); 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)); tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(tf_task_get_context(socket->_task), "%s", error));
_socket_close_internal(socket);
} }
} }
tf_free(request); tf_free(request);

View File

@ -485,7 +485,32 @@ static void _test_socket(const tf_test_options_t* options)
" s2.shutdown();\n" " s2.shutdown();\n"
" });\n" " });\n"
"}).catch(function(e) {\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" " print(e);\n"
"});\n"); "});\n");
fclose(file); fclose(file);