From b6a249baa9a03a77d549028101c2a4e46838aa43 Mon Sep 17 00:00:00 2001 From: Andrew Mayorov Date: Fri, 14 Jun 2024 17:32:49 +0200 Subject: [PATCH] feat(cth-peer): add brutal `kill/1` facility --- apps/emqx/test/emqx_cth_peer.erl | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/apps/emqx/test/emqx_cth_peer.erl b/apps/emqx/test/emqx_cth_peer.erl index b3849739a..9db595d73 100644 --- a/apps/emqx/test/emqx_cth_peer.erl +++ b/apps/emqx/test/emqx_cth_peer.erl @@ -22,6 +22,7 @@ -export([start/2, start/3, start/4]). -export([start_link/2, start_link/3, start_link/4]). -export([stop/1]). +-export([kill/1]). start(Name, Args) -> start(Name, Args, []). @@ -66,6 +67,32 @@ stop(Node) when is_atom(Node) -> ok end. +%% @doc Kill a node abruptly, through mechanisms provided by OS. +%% Relies on POSIX `kill`. +kill(Node) -> + try erpc:call(Node, os, getpid, []) of + OSPid -> + Pid = whereis(Node), + _ = is_pid(Pid) andalso unlink(Pid), + Result = kill_os_process(OSPid), + %% Either ensure control process stops, or try to stop if not killed. + _ = is_pid(Pid) andalso catch peer:stop(Pid), + Result + catch + error:{erpc, _} = Reason -> + {error, Reason} + end. + +kill_os_process(OSPid) -> + Cmd = "kill -SIGKILL " ++ OSPid, + Port = erlang:open_port({spawn, Cmd}, [binary, exit_status, hide]), + receive + {Port, {exit_status, 0}} -> + ok; + {Port, {exit_status, EC}} -> + {error, EC} + end. + parse_node_name(NodeName) -> case string:tokens(atom_to_list(NodeName), "@") of [Name, Host] ->