Skip to content

Commit

Permalink
fix: prevent pmap from getting stuck forever (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
savonarola authored Jun 10, 2023
1 parent fd5adf4 commit d5d978b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/minirest_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pmap_exec(CallerPid, Fun, El, Timeout) ->
ExecResult =
receive
{result, Pid, {normal, Result}} -> Result;
{result, Pid, {crash, {C, E, ST}}} -> erlang:raise(C, E, ST);
{result, Pid, {crash, {C, E, ST}}} -> {error, {El, {C, E, ST}}};
{'DOWN', Ref, process, Pid, Reason} -> {error, {El, Reason}}
after Timeout ->
true = erlang:exit(Pid, kill),
Expand Down
38 changes: 38 additions & 0 deletions test/minirest_util_SUITE.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
%% Copyright (c) 2013-2023 EMQ Technologies Co., Ltd. All Rights Reserved.
%%
%% 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.

-module(minirest_util_SUITE).

-compile(export_all).
-compile(nowarn_export_all).

-include_lib("stdlib/include/assert.hrl").

all() ->
[
t_pmap_exception
].

t_pmap_exception(_Config) ->
process_flag(trap_exit, true),
ct:timetrap({seconds, 2}),

?assertMatch(
[
{error, {a, {error, a, _}}},
{error, {b, {error, b, _}}},
{error, {c, {error, c, _}}}
],
minirest_util:pmap(fun(X) -> error(X) end, [a, b, c], 1000)
).

0 comments on commit d5d978b

Please sign in to comment.