Merge pull request #7108 from thalesmg/fix-sys-mon-portinfo

fix(sys_mon): guard before calling `erlang:port_info`
This commit is contained in:
zhongwencool 2022-02-25 17:38:24 +08:00 committed by GitHub
commit 8d26ba6e9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View File

@ -202,7 +202,12 @@ get_proc_lib_initial_call(Pid) ->
end.
portinfo(Port) ->
[{port, Port} | erlang:port_info(Port)].
PortInfo =
case is_port(Port) andalso erlang:port_info(Port) of
L when is_list(L) -> L;
_ -> []
end,
[{port, Port} | PortInfo].
safe_publish(Event, WarnMsg) ->
Topic = emqx_topic:systop(lists:concat(['sysmon/', Event])),

View File

@ -35,6 +35,11 @@
{self(), busy_port,
fmt("busy_port warning: suspid = ~p, port = ~p",
[self(), ?FAKE_PORT]), ?FAKE_PORT},
%% for the case when the port is missing, for some
%% reason.
{self(), busy_port,
fmt("busy_port warning: suspid = ~p, port = ~p",
[self(), []]), []},
{self(), busy_dist_port,
fmt("busy_dist_port warning: suspid = ~p, port = ~p",
[self(), ?FAKE_PORT]), ?FAKE_PORT},
@ -120,6 +125,16 @@ t_sys_mon(_Config) ->
validate_sys_mon_info(PidOrPort, SysMonName, ValidateInfo, InfoOrPort)
end, ?INPUTINFO).
%% Existing port, but closed.
t_sys_mon_dead_port(_Config) ->
process_flag(trap_exit, true),
Port = dead_port(),
{PidOrPort, SysMonName, ValidateInfo, InfoOrPort} =
{self(), busy_port,
fmt("busy_port warning: suspid = ~p, port = ~p",
[self(), Port]), Port},
validate_sys_mon_info(PidOrPort, SysMonName, ValidateInfo, InfoOrPort).
t_sys_mon2(_Config) ->
?SYSMON ! {timeout, ignored, reset},
?SYSMON ! {ignored},
@ -151,3 +166,8 @@ some_function(Parent, _Arg2) ->
stop ->
ok
end.
dead_port() ->
Port = erlang:open_port({spawn, "ls"}, []),
exit(Port, kill),
Port.