From d1c8950ed05dff15f84fb1c317ff99797185433d Mon Sep 17 00:00:00 2001 From: Nathan Monteleone Date: Tue, 16 Jun 2026 12:58:36 -0500 Subject: [PATCH] Close a window of opportunity in ElsaRegistry -- this should take care the random startup crashes we've had. --- lib/elsa/dynamic_process_manager.ex | 7 +++++-- lib/elsa/elsa_registry.ex | 17 +++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/elsa/dynamic_process_manager.ex b/lib/elsa/dynamic_process_manager.ex index 94fba24..34fa322 100644 --- a/lib/elsa/dynamic_process_manager.ex +++ b/lib/elsa/dynamic_process_manager.ex @@ -140,11 +140,14 @@ defmodule Elsa.DynamicProcessManager do end defp whereis({:via, registry_module, lookup}) do - registry_module.whereis_name(lookup) + case registry_module.whereis_name(lookup) do + :undefined -> nil + pid -> pid + end end defp wait_for(name) do - case Process.whereis(name) do + case whereis(name) do nil -> Process.sleep(200) wait_for(name) diff --git a/lib/elsa/elsa_registry.ex b/lib/elsa/elsa_registry.ex index 3f57a36..0c716da 100644 --- a/lib/elsa/elsa_registry.ex +++ b/lib/elsa/elsa_registry.ex @@ -35,8 +35,21 @@ defmodule Elsa.ElsaRegistry do @spec whereis_name({atom(), term()}) :: pid() | :undefined def whereis_name({registry, key}) do case :ets.lookup(registry, key) do - [{^key, pid}] -> pid - [] -> :undefined + [{^key, pid}] -> + # The process was found in the registry, but an additional Process.alive? check is still necessary. + # Otherwise there's a window of opportunity between when the process dies, + # and when the registry receives the EXIT message to remove the key from ETS. + if Process.alive?(pid) do + # Process was found in the registry and is alive + pid + else + # Process was found in the registry, but it's dead + :undefined + end + + [] -> + # Process was not found in the registry + :undefined end end