Debug features for leaked promises. And then chased down some subsequent use after free issues.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3985 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2022-09-22 00:38:26 +00:00
parent ab1f47ee9a
commit 8279ec5e9e
4 changed files with 125 additions and 16 deletions

View File

@ -95,22 +95,19 @@ static void _socket_reportError(socket_t* socket, const char* error);
static void _socket_set_handler(socket_t* socket, JSValue* handler, JSValue new_value)
{
JSContext* context = tf_task_get_context(socket->_task);
bool had_handler = !JS_IsUndefined(*handler);
if (!had_handler && !JS_IsUndefined(new_value))
JSValue old_handler = *handler;
if (JS_IsUndefined(old_handler) && !JS_IsUndefined(new_value))
{
JS_DupValue(context, socket->_object);
}
if (had_handler)
{
JSValue value = *handler;
*handler = JS_UNDEFINED;
JS_FreeValue(context, value);
}
if (!JS_IsUndefined(new_value))
{
*handler = JS_DupValue(context, new_value);
}
if (had_handler && JS_IsUndefined(new_value))
*handler = JS_DupValue(context, new_value);
JS_FreeValue(context, old_handler);
if (!JS_IsUndefined(old_handler) && JS_IsUndefined(new_value))
{
JS_FreeValue(context, socket->_object);
}
@ -280,7 +277,9 @@ void _socket_reportError(socket_t* socket, const char* error)
if (JS_IsFunction(context, socket-> _onError))
{
JSValue exception = JS_ThrowInternalError(context, "%s", error);
JSValue cb_ref = JS_DupValue(context, socket->_onError);
JSValue result = JS_Call(context, socket->_onError, socket->_object, 1, &exception);
JS_FreeValue(context, cb_ref);
tf_util_report_error(context, result);
JS_FreeValue(context, exception);
JS_FreeValue(context, result);
@ -547,7 +546,7 @@ JSValue _socket_listen(JSContext* context, JSValueConst this_val, int argc, JSVa
{
socket_t* socket = JS_GetOpaque(this_val, _classId);
socket->_listening = true;
int backlog = 1;
int backlog = 16;
JS_ToInt32(context, &backlog, argv[0]);
if (JS_IsUndefined(socket->_onConnect))
{
@ -572,7 +571,9 @@ void _socket_onNewConnection(uv_stream_t* server, int status)
JSValue ref = JS_DupValue(context, socket->_object);
if (!JS_IsUndefined(socket->_onConnect))
{
JSValue cb_ref = JS_DupValue(context, socket->_onConnect);
JSValue result = JS_Call(context, socket->_onConnect, socket->_object, 0, NULL);
JS_FreeValue(context, cb_ref);
tf_util_report_error(context, result);
JS_FreeValue(context, result);
}
@ -586,6 +587,7 @@ JSValue _socket_accept(JSContext* context, JSValueConst this_val, int argc, JSVa
socket_t* client = _socket_create_internal(context);
client->_direction = kAccept;
promiseid_t promise;
JSValue ref = JS_DupValue(context, client->_object);
JSValue result = tf_task_allocate_promise(socket->_task, &promise);
int status = uv_accept((uv_stream_t*)&socket->_socket, (uv_stream_t*)&client->_socket);
if (status == 0)
@ -603,7 +605,7 @@ JSValue _socket_accept(JSContext* context, JSValueConst this_val, int argc, JSVa
{
tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(context, "uv_accept: %s", uv_strerror(status)));
}
JS_FreeValue(context, client->_object);
JS_FreeValue(context, ref);
return result;
}
@ -728,7 +730,9 @@ void _socket_onRead(uv_stream_t* stream, ssize_t readSize, const uv_buf_t* buffe
if (!JS_IsUndefined(socket->_onRead))
{
JSValue args[] = { JS_UNDEFINED };
JSValue cb_ref = JS_DupValue(context, socket->_onRead);
JSValue result = JS_Call(context, socket->_onRead, socket->_object, 1, args);
JS_FreeValue(context, cb_ref);
tf_util_report_error(context, result);
JS_FreeValue(context, result);
}
@ -784,7 +788,9 @@ void _socket_onRead(uv_stream_t* stream, ssize_t readSize, const uv_buf_t* buffe
if (!JS_IsUndefined(socket->_onRead))
{
JSValue args[] = { JS_UNDEFINED };
JSValue cb_ref = JS_DupValue(context, socket->_onRead);
JSValue result = JS_Call(context, socket->_onRead, socket->_object, 1, args);
JS_FreeValue(context, cb_ref);
tf_util_report_error(context, result);
JS_FreeValue(context, result);
}
@ -833,7 +839,9 @@ void _socket_notifyDataRead(socket_t* socket, const char* data, size_t length)
JSValue args[] = { typedArray };
if (!JS_IsUndefined(socket->_onRead))
{
JSValue cb_ref = JS_DupValue(context, socket->_onRead);
JSValue result = JS_Call(context, socket->_onRead, socket->_object, 1, args);
JS_FreeValue(context, cb_ref);
tf_util_report_error(context, result);
JS_FreeValue(context, result);
}
@ -913,6 +921,7 @@ JSValue _socket_write(JSContext* context, JSValueConst this_val, int argc, JSVal
socket_t* socket = JS_GetOpaque(this_val, _classId);
promiseid_t promise = -1;
JSValue write_result = tf_task_allocate_promise(socket->_task, &promise);
JSValue ref = JS_DupValue(context, socket->_object);
if (!JS_IsUndefined(argv[0]))
{
if (socket->_tls)
@ -950,6 +959,7 @@ JSValue _socket_write(JSContext* context, JSValueConst this_val, int argc, JSVal
{
tf_task_reject_promise(socket->_task, promise, JS_NewInt32(context, -2));
}
JS_FreeValue(context, ref);
return write_result;
}