From 626fa4f27b49ad827e185c90f9572ebd0bff518d Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sat, 21 Oct 2023 19:47:47 +0000 Subject: [PATCH] Address some lifetime issues in the single-process case. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4565 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- src/task.c | 56 ++++++++++++++++++++++++++++------------------- src/taskstub.js.c | 10 ++++++++- 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/task.c b/src/task.c index ffee9df9..d824d2fa 100644 --- a/src/task.c +++ b/src/task.c @@ -1118,7 +1118,16 @@ void tf_task_on_receive_packet(int packetType, const char* begin, size_t length, } break; case kKill: - exit(1); + if (to->_one_proc) + { + to->_killed = true; + tf_packetstream_close(tf_taskstub_get_stream(from)); + uv_stop(&to->_loop); + } + else + { + exit(1); + } break; case kSetImports: { @@ -1752,7 +1761,7 @@ void tf_task_run(tf_task_t* task) do { uv_run(&task->_loop, UV_RUN_DEFAULT); - } while (_tf_task_run_jobs(task)); + } while (!task->_killed && _tf_task_run_jobs(task)); } bool tf_task_get_one_proc(tf_task_t* task) @@ -1787,24 +1796,6 @@ static void _tf_task_on_handle_close(uv_handle_t* handle) void tf_task_destroy(tf_task_t* task) { - for (int i = 0; i < task->_import_count; i++) - { - JS_FreeValue(task->_context, task->_imports[i]->_function); - tf_free(task->_imports[i]); - } - tf_free(task->_imports); - task->_imports = NULL; - task->_import_count = 0; - - for (int i = 0; i < task->_export_count; i++) - { - JS_FreeValue(task->_context, task->_exports[i]->_function); - tf_free(task->_exports[i]); - } - tf_free(task->_exports); - task->_exports = NULL; - task->_export_count = 0; - while (task->_children) { task_child_node_t* node = task->_children; @@ -1820,8 +1811,18 @@ void tf_task_destroy(tf_task_t* task) { tf_task_reject_promise(task, task->_promises[task->_promise_count - 1].id, JS_NULL); } - tf_free(task->_promises); - task->_promises = NULL; + + for (int i = 0; i < task->_export_count; i++) + { + JS_FreeValue(task->_context, task->_exports[i]->_function); + } + tf_free(task->_imports); + tf_free(task->_exports); + task->_imports = NULL; + task->_exports = NULL; + task->_import_count = 0; + task->_export_count = 0; + JS_FreeValue(task->_context, task->_loadedFiles); if (task->_ssb) @@ -1831,6 +1832,17 @@ void tf_task_destroy(tf_task_t* task) JS_FreeContext(task->_context); JS_FreeRuntime(task->_runtime); + tf_free(task->_promises); + task->_promises = NULL; + + tf_free(task->_imports); + task->_imports = NULL; + task->_import_count = 0; + + tf_free(task->_exports); + task->_exports = NULL; + task->_export_count = 0; + if (task->_db) { sqlite3_close(task->_db); diff --git a/src/taskstub.js.c b/src/taskstub.js.c index ef304d31..c5f9d9c5 100644 --- a/src/taskstub.js.c +++ b/src/taskstub.js.c @@ -67,6 +67,7 @@ static void _tf_taskstub_run_sandbox_thread(void* data) { uv_file fd = (uv_file)(intptr_t)data; tf_task_t* task = tf_task_create(); + tf_task_set_one_proc(task, true); tf_task_configure_from_fd(task, fd); /* The caller will trigger tf_task_activate with a message. */ tf_task_run(task); @@ -455,11 +456,18 @@ static JSValue _taskstub_execute(JSContext* context, JSValueConst this_val, int static JSValue _taskstub_kill(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { tf_taskstub_t* stub = JS_GetOpaque(this_val, _classId); + JSValue result = JS_UNDEFINED; if (!tf_task_get_one_proc(stub->_owner)) { uv_process_kill(&stub->_process, SIGTERM); } - return JS_UNDEFINED; + else + { + promiseid_t promise = -1; + result = tf_task_allocate_promise(stub->_owner, &promise); + tf_task_send_promise_message(stub->_owner, stub, kKill, promise, JS_UNDEFINED); + } + return result; } void tf_taskstub_destroy(tf_taskstub_t* stub)