From bd81b2acf5240c6f5743aa726c903a34f4ee38e0 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sat, 22 Jan 2022 20:47:10 +0000 Subject: [PATCH] More bugs. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3785 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- core/ssb.js | 2 +- src/ssb.c | 5 +++-- src/ssb.tests.c | 38 ++++++++++++++++++++++++++++++++++++++ src/task.c | 7 ++++++- 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/core/ssb.js b/core/ssb.js index b1789e58..e8b71649 100644 --- a/core/ssb.js +++ b/core/ssb.js @@ -204,7 +204,7 @@ function ebtReplicateSendClock(request, have) { var message = {}; var last_sent = request.connection.sent_clock || {}; var ids = followingDeep(g_database, [me], 2).concat([request.connection.id]); - if (!last_sent) { + if (!Object.keys(last_sent).length) { for (let id of ids) { message[id] = get_latest_sequence_for_author(id); } diff --git a/src/ssb.c b/src/ssb.c index 995e5bee..3a05efb1 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -1219,6 +1219,7 @@ void tf_ssb_connection_destroy(tf_ssb_connection_t* connection) } if (!JS_IsUndefined(connection->object)) { + JS_SetOpaque(connection->object, NULL); JS_FreeValue(ssb->context, connection->object); connection->object = JS_UNDEFINED; } @@ -1884,6 +1885,7 @@ tf_ssb_connection_t* tf_ssb_connection_create(tf_ssb_t* ssb, const char* host, c uv_async_init(ssb->loop, &connection->async, _tf_ssb_connection_process_message_async); connection->object = JS_NewObjectClass(ssb->context, _connection_class_id); + JS_SetOpaque(connection->object, connection); JS_SetPropertyStr(context, connection->object, "send_json", JS_NewCFunction(context, _tf_ssb_connection_send_json, "send_json", 2)); char public_key_str[k_id_base64_len] = { 0 }; if (tf_ssb_id_bin_to_str(public_key_str, sizeof(public_key_str), public_key)) @@ -1891,7 +1893,6 @@ tf_ssb_connection_t* tf_ssb_connection_create(tf_ssb_t* ssb, const char* host, c JS_SetPropertyStr(context, connection->object, "id", JS_NewString(context, public_key_str)); JS_SetPropertyStr(context, connection->object, "is_client", JS_TRUE); } - JS_SetOpaque(connection->object, connection); memcpy(connection->serverpub, public_key, sizeof(connection->serverpub)); @@ -1901,7 +1902,7 @@ tf_ssb_connection_t* tf_ssb_connection_create(tf_ssb_t* ssb, const char* host, c if (result) { printf("uv_tcp_connect(%s): %s\n", host, uv_strerror(result)); - JS_FreeValue(ssb->context, connection->object); + tf_ssb_connection_destroy(connection); } else { diff --git a/src/ssb.tests.c b/src/ssb.tests.c index 3b301f3f..e5f60676 100644 --- a/src/ssb.tests.c +++ b/src/ssb.tests.c @@ -3,6 +3,7 @@ #include "ssb.db.h" #include "ssb.js.h" #include "tests.h" +#include "util.js.h" #include #include @@ -10,6 +11,8 @@ #include #include +#include "quickjs-libc.h" + void tf_ssb_test_id_conversion(const tf_test_options_t* options) { printf("Testing id conversion.\n"); @@ -74,6 +77,30 @@ static void _message_added(tf_ssb_t* ssb, const char* id, void* user_data) ++*(int*)user_data; } +static void _ssb_test_idle(uv_idle_t* idle) +{ + tf_ssb_t* ssb = idle->data; + JSRuntime* runtime = JS_GetRuntime(tf_ssb_get_context(ssb)); + while (JS_IsJobPending(runtime)) + { + JSContext* context = NULL; + int r = JS_ExecutePendingJob(runtime, &context); + JSValue result = JS_GetException(context); + if (context) + { + tf_util_report_error(context, result); + } + if (r < 0) + { + js_std_dump_error(context); + } + else if (r == 0) + { + break; + } + } +} + void tf_ssb_test_ssb(const tf_test_options_t* options) { printf("Testing SSB.\n"); @@ -91,6 +118,14 @@ void tf_ssb_test_ssb(const tf_test_options_t* options) tf_ssb_t* ssb1 = tf_ssb_create(&loop, NULL, db1, NULL); tf_ssb_register(tf_ssb_get_context(ssb1), ssb1); + uv_idle_t idle0 = { .data = ssb0 }; + uv_idle_init(&loop, &idle0); + uv_idle_start(&idle0, _ssb_test_idle); + + uv_idle_t idle1 = { .data = ssb1 }; + uv_idle_init(&loop, &idle1); + uv_idle_start(&idle1, _ssb_test_idle); + test_t test = { .ssb0 = ssb0, .ssb1 = ssb1, @@ -181,6 +216,9 @@ void tf_ssb_test_ssb(const tf_test_options_t* options) tf_ssb_send_close(ssb1); + uv_close((uv_handle_t*)&idle0, NULL); + uv_close((uv_handle_t*)&idle1, NULL); + uv_run(&loop, UV_RUN_DEFAULT); tf_ssb_destroy(ssb0); diff --git a/src/task.c b/src/task.c index 6b59efd5..ae999780 100644 --- a/src/task.c +++ b/src/task.c @@ -1382,8 +1382,10 @@ tf_task_t* tf_task_create() uv_unref((uv_handle_t*)&task->trace_timer); task->idle.data = task; uv_idle_init(&task->_loop, &task->idle); + uv_unref((uv_handle_t*)&task->idle); task->prepare.data = task; uv_prepare_init(&task->_loop, &task->prepare); + uv_unref((uv_handle_t*)&task->prepare); uv_idle_start(&task->idle, _tf_task_run_jobs_idle); return task; } @@ -1487,7 +1489,10 @@ void tf_task_activate(tf_task_t* task) void tf_task_run(tf_task_t* task) { - uv_run(&task->_loop, UV_RUN_DEFAULT); + do + { + uv_run(&task->_loop, UV_RUN_DEFAULT); + } while (_tf_task_run_jobs(task)); } void tf_task_set_trusted(tf_task_t* task, bool trusted)