Merge pull request #8248 from emqx/0616-try-not-to-exit-abnormal-as-much-as-possible

fix(pmap): try not to exit abnormal as much as possible
This commit is contained in:
Zaiming (Stone) Shi 2022-06-16 21:47:41 +01:00 committed by GitHub
commit 6d830beb5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 5 deletions

View File

@ -473,7 +473,14 @@ do_parallel_map(Fun, List) ->
fun(Item) ->
erlang:spawn_link(
fun() ->
Parent ! {self(), Fun(Item)}
Res =
try
{normal, Fun(Item)}
catch
C:E:St ->
{exception, {C, E, St}}
end,
Parent ! {self(), Res}
end
)
end,
@ -482,8 +489,10 @@ do_parallel_map(Fun, List) ->
lists:foldr(
fun(Pid, Acc) ->
receive
{Pid, Result} ->
[Result | Acc]
{Pid, {normal, Result}} ->
[Result | Acc];
{Pid, {exception, {C, E, St}}} ->
erlang:raise(C, E, St)
end
end,
[],

View File

@ -194,8 +194,8 @@ t_pmap_timeout(_) ->
).
t_pmap_exception(_) ->
?assertExit(
{foobar, _},
?assertError(
foobar,
emqx_misc:pmap(
fun
(error) -> error(foobar);