summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2023-10-29 11:18:32 +0800
committerruki <[email protected]>2023-10-29 11:18:32 +0800
commitd61d953b43fca1d3e68122036df4ef40277177ae (patch)
treee508f996c68db8e426190fbb94a275dea9585a3e
parenta5ed7b6144d1a6318e25e044dc60a391a317470b (diff)
fix catch build fails
-rw-r--r--xmake/actions/build/main.lua25
-rw-r--r--xmake/core/base/scheduler.lua10
-rw-r--r--xmake/modules/async/runjobs.lua27
3 files changed, 24 insertions, 38 deletions
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/modules/async/runjobs.lua b/xmake/modules/async/runjobs.lua
index 98ad63747..88f8d552f 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,11 @@ 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)
end
}
}
@@ -293,6 +296,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