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.

First 2 of 2 Posts Switch mode

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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement