From e3c93a6a79676bf4145ac4aa6c82f25556ba1dd7 Mon Sep 17 00:00:00 2001 From: George Zhao Date: Sat, 19 Aug 2023 17:56:08 +0800 Subject: [PATCH] chore: not exit when error occurs in callback --- src/lhandle.c | 2 +- src/loop.c | 2 +- src/lreq.c | 2 +- src/luv.h | 4 ++++ src/thread.c | 2 +- src/work.c | 2 +- tests/test-prepare-check-idle-async.lua | 11 +++++++++++ tests/test-timer.lua | 8 ++++++++ 8 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/lhandle.c b/src/lhandle.c index 1c4cc800..f7e30ac4 100644 --- a/src/lhandle.c +++ b/src/lhandle.c @@ -98,7 +98,7 @@ static void luv_call_callback(lua_State* L, luv_handle_t* data, luv_callback_id lua_insert(L, -1 - nargs); } - ctx->cb_pcall(L, nargs, 0, 0); + ctx->cb_pcall(L, nargs, 0, LUVF_CALLBACK_FLAGS); } } diff --git a/src/loop.c b/src/loop.c index ffdb345d..b234c477 100644 --- a/src/loop.c +++ b/src/loop.c @@ -99,7 +99,7 @@ static void luv_walk_cb(uv_handle_t* handle, void* arg) { lua_pushvalue(L, 1); // Copy the function luv_find_handle(L, data); // Get the userdata - data->ctx->cb_pcall(L, 1, 0, 0); // Call the function + data->ctx->cb_pcall(L, 1, 0, LUVF_CALLBACK_FLAGS); // Call the function } static int luv_walk(lua_State* L) { diff --git a/src/lreq.c b/src/lreq.c index 2322dd0e..e3846960 100644 --- a/src/lreq.c +++ b/src/lreq.c @@ -63,7 +63,7 @@ static void luv_fulfill_req(lua_State* L, luv_req_t* data, int nargs) { if (nargs) { lua_insert(L, -1 - nargs); } - data->ctx->cb_pcall(L, nargs, 0, 0); + data->ctx->cb_pcall(L, nargs, 0, LUVF_CALLBACK_FLAGS); } } diff --git a/src/luv.h b/src/luv.h index ad023291..a9362a27 100644 --- a/src/luv.h +++ b/src/luv.h @@ -67,6 +67,10 @@ #define LUVF_CALLBACK_NOTRACEBACK 0x02 // Don't traceback when error #define LUVF_CALLBACK_NOERRMSG 0x04 // Don't output err message +#ifndef LUVF_CALLBACK_FLAGS +#define LUVF_CALLBACK_FLAGS LUVF_CALLBACK_NOEXIT +#endif + /* Prototype of external callback routine. * The caller and the implementer exchanges data by the lua vm stack. * The caller push a lua function and nargs values onto the stack, then call it. diff --git a/src/thread.c b/src/thread.c index 7defe35c..8d6a5b78 100644 --- a/src/thread.c +++ b/src/thread.c @@ -283,7 +283,7 @@ static void luv_thread_cb(void* varg) { //push parameter for real thread function int i = luv_thread_arg_push(L, &thd->args, LUVF_THREAD_SIDE_CHILD); - ctx->thrd_pcall(L, i, 0, 0); + ctx->thrd_pcall(L, i, 0, LUVF_CALLBACK_NOEXIT); luv_thread_arg_clear(L, &thd->args, LUVF_THREAD_SIDE_CHILD); } else { fprintf(stderr, "Uncaught Error in thread: %s\n", lua_tostring(L, -1)); diff --git a/src/work.c b/src/work.c index b51bfd11..85c7e4af 100644 --- a/src/work.c +++ b/src/work.c @@ -170,7 +170,7 @@ static void luv_after_work_cb(uv_work_t* req, int status) { lua_rawgeti(L, LUA_REGISTRYINDEX, ctx->after_work_cb); i = luv_thread_arg_push(L, &work->rets, LUVF_THREAD_SIDE_MAIN); - lctx->cb_pcall(L, i, 0, 0); + lctx->cb_pcall(L, i, 0, LUVF_CALLBACK_FLAGS); //ref down to ctx, up in luv_queue_work() luaL_unref(L, LUA_REGISTRYINDEX, work->ref); diff --git a/tests/test-prepare-check-idle-async.lua b/tests/test-prepare-check-idle-async.lua index 389c2633..ea1d4d94 100644 --- a/tests/test-prepare-check-idle-async.lua +++ b/tests/test-prepare-check-idle-async.lua @@ -46,4 +46,15 @@ return require('lib/tap')(function (test) uv.async_send(async) end) + test("error occurs in idle callback", function (print, p, expect, uv) + local idle = uv.new_idle() + uv.idle_start(idle, expect(function () + p("idle", idle) + uv.idle_stop(idle) + uv.close(idle, expect(function () + end)) + error("Error in idle callback") + end)) + end) + end) diff --git a/tests/test-timer.lua b/tests/test-timer.lua index 1d6faca2..83d3865c 100644 --- a/tests/test-timer.lua +++ b/tests/test-timer.lua @@ -115,4 +115,12 @@ return require('lib/tap')(function (test) assert(huge_timer:get_due_in()==0xffff) end, "1.40.0") + test("timer init", function(print, p, expect, uv) + local timer = uv.new_timer() + timer:start(10, 0, function() + timer:stop() + timer:close() + error('Error in timeout callback') + end) + end) end)