Raees678

Raees678

I have a two part question concerning a module like so:

defmodule AsyncAwait do
  defp long_running_task(id) do
    Process.sleep(5_000)
    IO.puts("Completed #{id}")
    {:ok, "Completed #{id}"}
  end

  def perform_tasks(task_ids \\ [0, 1, 2]) do
    task_ids
    |> Enum.map(fn id ->
      Task.async(fn ->
        long_running_task(id)
      end)
    end)
    |> Enum.map(fn task ->
      IO.inspect(self())
      Task.await(task, 1_000)
    end)
  end
end

When I launch the iex shell and run AsyncAwait.perform_tasks(), I recieve a timeout and an exit as expected, at the await:

** (exit) exited in: Task.await(%Task{owner: #PID<0.151.0>, pid: #PID<0.153.0>, ref: #Reference<0.3029894714.2600271873.129496>}, 1000)
    ** (EXIT) time out
    (elixir 1.13.4) lib/task.ex:809: Task.await/2
    (elixir 1.13.4) lib/enum.ex:1593: Enum."-map/2-lists^map/1-0-"/2

I would expect that this kills the caller process, and hence also all the associated task processes that were spawned during the async call. This is what the docs say:

A timeout, in milliseconds or :infinity, can be given with a default value of 5000. If the timeout is exceeded, then the caller process will exit. If the task process is linked to the caller process which is the case when a task is started with async, then the task process will also exit. If the task process is trapping exits or not linked to the caller process, then it will continue to run - Task — Elixir v1.13.4

  1. Normally when the caller process exits, I expected it would kill the iex shell since they exist in the same process. I can verify that they exist in the same process since I call IO.inspect(self()) in the anonymous function that executes the Task.await, and when comparing it to the output of calling self() in iex, they are both indeed the same.

However the iex prompt numbers do not reset after the error, and its pid does not change either. Manually killing the current process using Process.exit(self(), :exit) resets the iex prompt number and the pid of the iex process, so I suspect that the caller process is not actually being killed. If it isn’t then what process actually exits due to the timeout?

  1. After 5 seconds I still see the output from the tasks in the iex shell:
Completed 0
Completed 1
Completed 2

This indicates that the async tasks that were spawned are still running.

How could this be happening? Assuming the caller processes did actually exit this means the associated task processes did not exit, which contradict the docs above.

Showing Posts 1 to 2

josevalim

josevalim

Creator of Elixir

Check out the docs for async: Task — Elixir v1.13.4

IEx traps exits (so it can report them).

tiagodavi

tiagodavi

Hey Jose,

I noticed that the same behavior occurs during a normal Phoenix request. If my request finishes while I have a task running, the task doesn’t terminate along with the request—it continues running afterward. Does this mean the request is trapping exits?

Additionally, I observed during tests that Task.async seems to automatically take ownership of a database connection, whereas a traditional spawn function does not. I’ve been searching through the documentation to confirm this behavior.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews