diff --git a/apps/emqx/src/emqx_sys_mon.erl b/apps/emqx/src/emqx_sys_mon.erl index 1f8f04d97..1df9ec604 100644 --- a/apps/emqx/src/emqx_sys_mon.erl +++ b/apps/emqx/src/emqx_sys_mon.erl @@ -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])), diff --git a/apps/emqx/test/emqx_sys_mon_SUITE.erl b/apps/emqx/test/emqx_sys_mon_SUITE.erl index 8e4bd65db..338188c21 100644 --- a/apps/emqx/test/emqx_sys_mon_SUITE.erl +++ b/apps/emqx/test/emqx_sys_mon_SUITE.erl @@ -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.