Skip to content

Commit

Permalink
erts: Make sure to flush potential exit message
Browse files Browse the repository at this point in the history
  • Loading branch information
garazdawi committed Aug 16, 2016
1 parent 020d38d commit cad0812
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
16 changes: 10 additions & 6 deletions lib/kernel/src/os.erl
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,11 @@ get_data(Port, MonRef, Eot, Sofar) ->
Last ->
Port ! {self(), close},
flush_until_closed(Port),
flush_exit(Port),
iolist_to_binary([Sofar, Last])
end;
{'DOWN', MonRef, _, _ , _} ->
receive
{'EXIT', Port, _} ->
ok
after 1 -> % force context switch
ok
end,
flush_exit(Port),
iolist_to_binary(Sofar)
end.

Expand All @@ -307,3 +303,11 @@ flush_until_closed(Port) ->
{Port, closed} ->
true
end.

flush_exit(Port) ->
receive
{'EXIT', Port, _} ->
ok
after 1 -> % force context switch
ok
end.
22 changes: 20 additions & 2 deletions lib/kernel/test/os_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
-export([space_in_cwd/1, quoting/1, cmd_unicode/1, space_in_name/1, bad_command/1,
find_executable/1, unix_comment_in_command/1, deep_list_command/1,
large_output_command/1, background_command/0, background_command/1,
perf_counter_api/1]).
message_leak/1, perf_counter_api/1]).

-include_lib("common_test/include/ct.hrl").

Expand All @@ -36,7 +36,8 @@ suite() ->
all() ->
[space_in_cwd, quoting, cmd_unicode, space_in_name, bad_command,
find_executable, unix_comment_in_command, deep_list_command,
large_output_command, background_command, perf_counter_api].
large_output_command, background_command, message_leak,
perf_counter_api].

groups() ->
[].
Expand Down Expand Up @@ -284,6 +285,23 @@ background_command(_Config) ->
%% longer then the 5 second timeout
os:cmd("sleep 10&").

%% Test that message does not leak to the calling process
message_leak(_Config) ->
process_flag(trap_exit, true),

os:cmd("echo hello"),
[] = receive_all(),

case os:type() of
{unix, _} ->
os:cmd("while true; do echo hello; done&"),
[] = receive_all();
_ ->
ok % Cannot background on non-unix
end,

process_flag(trap_exit, false).

%% Test that the os:perf_counter api works as expected
perf_counter_api(_Config) ->

Expand Down

0 comments on commit cad0812

Please sign in to comment.