diff --git a/lib/kino/terminator.ex b/lib/kino/terminator.ex index e1ddaa8c..55fa0071 100644 --- a/lib/kino/terminator.ex +++ b/lib/kino/terminator.ex @@ -30,8 +30,16 @@ defmodule Kino.Terminator do end if pid do + terminator = cross_node_name() + Kino.Bridge.reference_object(pid, parent) - Kino.Bridge.monitor_object(pid, cross_node_name(), {:terminate, pid}, ack?: true) + + with {:request_error, :terminated} <- + Kino.Bridge.monitor_object(pid, terminator, {:terminate, pid}, ack?: true) do + # If the group leader terminated, it is not going to monitor + # the process as we expect, so we terminate it immediately + send(terminator, {:terminate, pid}) + end end resp @@ -75,6 +83,11 @@ defmodule Kino.Terminator do {:noreply, Map.delete(state, pid)} end + def handle_info({:terminate, pid}, state) do + DynamicSupervisor.terminate_child(Kino.DynamicSupervisor, pid) + {:noreply, Map.delete(state, pid)} + end + @impl true def handle_info({:DOWN, _, _, pid, reason}, state) do {parent, state} = Map.pop(state, pid)