diff --git a/lib/membrane/core/parent/child_life_controller.ex b/lib/membrane/core/parent/child_life_controller.ex index 1c3abab30..4a21eb7d2 100644 --- a/lib/membrane/core/parent/child_life_controller.ex +++ b/lib/membrane/core/parent/child_life_controller.ex @@ -500,33 +500,40 @@ defmodule Membrane.Core.Parent.ChildLifeController do def handle_remove_children(children_or_children_groups, state) do children_or_children_groups = Bunch.listify(children_or_children_groups) - all_children_and_children_groups = + refs = state.children - |> Enum.flat_map(fn {ref, %{group: group}} -> [ref, group] end) + |> Enum.filter(fn {child_ref, child_entry} -> + child_ref in children_or_children_groups or + child_entry.group in children_or_children_groups + end) + |> Enum.map(fn {ref, _child_entry} -> ref end) + + removed_children_and_children_groups = + Enum.map(refs, &ChildrenModel.get_child_data!(state, &1)) + |> Enum.flat_map(&[&1.name, &1.group]) |> Enum.uniq() |> List.delete(nil) children_or_children_groups - |> Enum.find(&(&1 not in all_children_and_children_groups)) + |> Enum.find(&(&1 not in removed_children_and_children_groups)) |> case do nil -> :ok child_ref -> + children_groups = + Map.values(state.children) + |> Enum.map(& &1.group) + |> Enum.uniq() + |> List.delete(nil) + raise Membrane.ParentError, """ Trying to remove child #{inspect(child_ref)}, while such a child or children group does not exist. - Existing children and children groups are: #{inspect(all_children_and_children_groups, pretty: true)} + Existing children are: #{inspect(Map.keys(state.children), pretty: true)} + Existing children groups are: #{inspect(children_groups, pretty: true)} """ end - refs = - state.children - |> Enum.filter(fn {child_ref, child_entry} -> - child_ref in children_or_children_groups or - child_entry.group in children_or_children_groups - end) - |> Enum.map(fn {ref, _child_entry} -> ref end) - Membrane.Logger.debug("Removing children: #{inspect(refs)}") state =