Skip to content

Commit

Permalink
Call off start_child when the group leader terminates
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatanklosko committed Sep 19, 2024
1 parent 54ba485 commit 59f7237
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/kino/terminator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,17 @@ defmodule Kino.Terminator do
end

if pid do
Kino.Bridge.reference_object(pid, parent)
Kino.Bridge.monitor_object(pid, cross_node_name(), {:terminate, pid}, ack?: true)
terminator = cross_node_name()

with :ok <- Kino.Bridge.reference_object(pid, parent),
:ok <- Kino.Bridge.monitor_object(pid, terminator, {:terminate, pid}, ack?: true) do
:ok
else
# If we fail to reference the object, it means the group
# leader terminated, so we immediately terminate the started
# process
_ -> send(terminator, {:terminate, pid})
end
end

resp
Expand Down Expand Up @@ -75,6 +84,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)
Expand Down

0 comments on commit 59f7237

Please sign in to comment.