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

View File

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