Supervision for task within a child task

I’ve a Task which is started under supervision, within this Task, I spawn a few additional Tasks, I was wondering how supervision will be for the additional Tasks spawned within the Task under supervision.

def validate_sim_phone_status(msisdn) do

task1 = Task.async(fn -> is_4G_phone(msisdn) end)
task2 = Task.async(fn -> is_4G_sim?(msisdn) end)
task3 = Task.async(fn -> is_4G_phone_alt?(msisdn) end)

task1 = Task.await(task1)
task2 = Task.await(task2)
task3 = Task.await(task3)

case task1 do
  {:ok, true} -> task2
  {:ok, _} -> false
  {:error, _reason} ->  task3 && task2


def get_failed_4G(table) do
    query = from(m in table, select: %{msisdn: m.msisdn_2})
    items =, timeout: :infinity, max_rows: 70)

    stream = Task.Supervisor.async_stream_nolink(Lte.TaskSupervisor, items, fn item -> 
       status =  Lte.validate_sim_phone_status(item.msisdn)
       if status, do: :do_something

    end, timeout: 50_000)

    Repo.transaction(fn ->
    end, timeout: :infinity)

I was wondering if the tasks defined with the validate_sim_phone_status will be supervised, is there a better implementation.

Thank you.

you have to be explicit to get them supervised; you should probably start them with Task.Supervised.async

I was thinking since the Tasks are being run from a supervised Task, a crash in any of them should lead to a crash in the supervised Task which will then be restarted leading to the restarting of the Tasks.


Note that tasks are not restarted by default.

Yeah, I’ll mark it as transient. Can you confirm if my assertion on the supervision is correct.
I just run a test, when the grand child task crashes, it’s propagated to the parent task which also crashes since they are linked, this forces the Task Supervisor to restart the parent Task, which then starts the children Task. The gotcha is that the even if one grand child Task crashes, all grand children are restarted; I will have to put those Task under their own supervision tree.
@ityonemo & @idiot - Thank you

This supposition is correct, but supervision trees are not only for handling restarts, they exist to manage process lifecycle. If you put your task in the supervision tree, you are ensuring that the task, among other things, is discoverable by walking down the supervision tree all the way down from application top supervisor.