From 395618eb84cada02875670aec6c3e8f9d923b1f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Thu, 18 Jan 2024 14:46:37 +0100 Subject: Repeat attempts to obtain the remote pid Sometimes the function would find two pids instead of one, likely due to timing issues related to reusing port numbers. In these cases we retry up to 5 times with a short timeout in between. --- src/ct_helper.erl | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/ct_helper.erl b/src/ct_helper.erl index 5659f47..3723095 100644 --- a/src/ct_helper.erl +++ b/src/ct_helper.erl @@ -129,14 +129,26 @@ get_parent_pid(Pid) -> get_remote_pid_tcp(Socket) when is_port(Socket) -> get_remote_pid_tcp(inet:sockname(Socket)); get_remote_pid_tcp(SockName) -> + get_remote_pid_tcp(SockName, 5). + +get_remote_pid_tcp(SockName, 0) -> + AllPorts = [{P, erlang:port_info(P), (catch inet:peername(P))} || P <- erlang:ports()], + error({missing_or_duplicate_ports, SockName, AllPorts}); +get_remote_pid_tcp(SockName, Attempts) -> AllPorts = [{P, erlang:port_info(P)} || P <- erlang:ports()], - [Pid] = [ + Result = [ proplists:get_value(connected, I) || {P, I} <- AllPorts, I =/= undefined, proplists:get_value(name, I) =:= "tcp_inet", inet:peername(P) =:= SockName], - Pid. + case Result of + [Pid] -> + Pid; + _ -> + timer:sleep(10), + get_remote_pid_tcp(SockName, Attempts - 1) + end. %% @doc Find the pid of the remote end of a TLS socket. %% -- cgit v1.2.3