diff options
| author | ruki <[email protected]> | 2023-10-29 02:33:17 -0500 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-10-29 02:33:17 -0500 |
| commit | 042c51144c1a918d75493a94094e29aeafd924ff (patch) | |
| tree | b166d465daf344c90edd79f22c48b943d9475591 | |
| parent | a5ed7b6144d1a6318e25e044dc60a391a317470b (diff) | |
| parent | 8233ee232a9f6884e17579058959e1a4e88d774a (diff) | |
Merge pull request #4333 from xmake-io/runjobs
fix catch build fails
| m--------- | core/src/tbox/tbox | 0 | ||||
| -rw-r--r-- | core/src/xmake/engine.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/io/socket_kill.c | 55 | ||||
| -rw-r--r-- | xmake/actions/build/main.lua | 25 | ||||
| -rw-r--r-- | xmake/core/base/scheduler.lua | 10 | ||||
| -rw-r--r-- | xmake/core/base/socket.lua | 14 | ||||
| -rw-r--r-- | xmake/modules/async/runjobs.lua | 36 |
7 files changed, 105 insertions, 37 deletions
diff --git a/core/src/tbox/tbox b/core/src/tbox/tbox -Subproject d5c53e891e59a21445510bd16c43d615c18b357 +Subproject 2af728a23ec4c16c5178047ce59ce0cd11accb3 diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index 6992b6443..f1b0b381b 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -162,6 +162,7 @@ tb_int_t xm_io_socket_sendto(lua_State* lua); tb_int_t xm_io_socket_sendfile(lua_State* lua); tb_int_t xm_io_socket_recv(lua_State* lua); tb_int_t xm_io_socket_recvfrom(lua_State* lua); +tb_int_t xm_io_socket_kill(lua_State* lua); tb_int_t xm_io_socket_close(lua_State* lua); // the io/pipe functions @@ -401,6 +402,7 @@ static luaL_Reg const g_io_functions[] = , { "socket_sendfile", xm_io_socket_sendfile } , { "socket_recv", xm_io_socket_recv } , { "socket_recvfrom", xm_io_socket_recvfrom } +, { "socket_kill" , xm_io_socket_kill } , { "socket_close", xm_io_socket_close } , { "pipe_open", xm_io_pipe_open } , { "pipe_openpair", xm_io_pipe_openpair } diff --git a/core/src/xmake/io/socket_kill.c b/core/src/xmake/io/socket_kill.c new file mode 100644 index 000000000..46cddf5cf --- /dev/null +++ b/core/src/xmake/io/socket_kill.c @@ -0,0 +1,55 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015-present, TBOOX Open Source Group. + * + * @author ruki + * @file socket_kill.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "socket_kill" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +// local sock = io.socket_kill(sock) +tb_int_t xm_io_socket_kill(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // is pointer? + if (!xm_lua_ispointer(lua, 1)) + return 0; + + // get socket + tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1); + tb_check_return_val(sock, 0); + + // kill socket + tb_socket_kill(sock, TB_SOCKET_KILL_RW); + return 0; +} + diff --git a/xmake/actions/build/main.lua b/xmake/actions/build/main.lua index 43897578a..fe7fcc771 100644 --- a/xmake/actions/build/main.lua +++ b/xmake/actions/build/main.lua @@ -110,32 +110,10 @@ function _do_build(targetname, group_pattern) end end --- on exit -function _on_exit(ok, errors) - - -- since we call it in both os.atexit and catch block, - -- we need to avoid duplicate execution. - local handled = false - local exited = _g.exited - if not exited then - exited = true - handled = true - _g.exited = exited - end - - -- we just handle the build failure - if handled and not ok then - check_targets(targetname, {build_failure = true}) - end -end - -- build targets function build_targets(targetnames, opt) opt = opt or {} - -- register exit callbacks - os.atexit(_on_exit) - local group_pattern = opt.group_pattern try { @@ -159,9 +137,8 @@ function build_targets(targetnames, opt) { function (errors) - -- maybe it's unreachable when building fails, so we also need os.atexit() -- @see https://github.com/xmake-io/xmake/issues/3401 - _on_exit(false, errors) + check_targets(targetnames, {build_failure = true}) -- do rules after building _do_project_rules("build_after", {errors = errors}) diff --git a/xmake/core/base/scheduler.lua b/xmake/core/base/scheduler.lua index e89c8c604..52a896728 100644 --- a/xmake/core/base/scheduler.lua +++ b/xmake/core/base/scheduler.lua @@ -970,20 +970,16 @@ function scheduler:runloop() if eventfunc then ok, errors = eventfunc(self, obj, objevents) if not ok then - -- TODO - -- -- This causes a direct exit from the entire runloop and -- a quick escape from nested try-catch blocks and coroutines groups. -- -- So some try-catch cannot catch these errors, such as when a build fails (in build group). -- @see https://github.com/xmake-io/xmake/issues/3401 -- - -- In theory, we should handle it better. For example, if there is a group that is waiting, - -- we should notify the other concurrent threads to exit quickly and then let the group concurrent threads to throw the error. - -- That way the outside try-catch can continue to catch it. + -- We should catch it in coroutines and re-throw it outside scheduler, + -- it will avoid co_resume to get and return this error. -- - -- But implementing it is more complicated and I haven't come up with a solution to let other concurrent processes exit quickly, - -- especially if the child process is waiting and we need to notify it of the end quickly as well. + -- e.g. we can see runjobs.lua implementation break end end diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua index 12b3661f5..8976faed5 100644 --- a/xmake/core/base/socket.lua +++ b/xmake/core/base/socket.lua @@ -641,6 +641,20 @@ function _instance:wait(events, timeout) return result, errors end +-- kill socket +function _instance:kill() + + -- ensure opened + local ok, errors = self:_ensure_opened() + if not ok then + return false, errors + end + + -- kill it + io.socket_kill(self:cdata()) + return true +end + -- close socket function _instance:close() diff --git a/xmake/modules/async/runjobs.lua b/xmake/modules/async/runjobs.lua index 98ad63747..8812251ac 100644 --- a/xmake/modules/async/runjobs.lua +++ b/xmake/modules/async/runjobs.lua @@ -158,6 +158,8 @@ function main(name, jobs, opt) local priority_prev = 0 local priority_curr = 0 local job_pending = nil + local abort = false + local abort_errors while index < total do scheduler.co_group_begin(group_name, function (co_group) local freemax = comax - #co_group @@ -218,6 +220,9 @@ function main(name, jobs, opt) try { function() + if stop then + return + end if distcc then local co_running = scheduler.co_running() if co_running then @@ -247,13 +252,22 @@ function main(name, jobs, opt) progress_helper:stop() end - -- do exit callback - if opt.on_exit then - opt.on_exit(errors) + -- we need re-throw this errors outside scheduler + abort = true + if abort_errors == nil then + abort_errors = errors end - -- re-throw this errors and abort scheduler - raise(errors) + -- kill all waited objects in this group + local waitobjs = scheduler.co_group_waitobjs(group_name) + if waitobjs:size() > 0 then + for _, obj in waitobjs:keys() do + -- TODO, kill pipe is not supported now + if obj.kill then + obj:kill() + end + end + end end } } @@ -293,6 +307,16 @@ function main(name, jobs, opt) -- do exit callback if opt.on_exit then - opt.on_exit() + opt.on_exit(abort_errors) + end + + -- re-throw abort errors + -- + -- @note we cannot throw it in coroutine, + -- because his causes a direct exit from the entire runloop and + -- a quick escape from nested try-catch blocks and coroutines groups. + -- so we can not catch runjobs errors, e.g. build fails + if abort then + raise(abort_errors) end end |
