benlime

benlime

Why does Task.await not stop the awaited process on timeout?

Hey, this might be kind of a noob question - but I always was under the assumption that the process which is awaited by Task.await would die as soon as the timeout is reached. Given the following code:

defmodule Greeter do
  def run do
    task = Task.async(&greeter/0)
    Task.await(task, 10_000)
    IO.puts("work is done")
  end

  defp greeter do
    Process.sleep(1000)
    IO.puts("Hello")
    greeter()
  end
end

If I run this in iex I would expect it to stop printing “Hello” to the shell after 10 seconds timed out. Instead I’m getting the error that the timeout has been reached but the greeter would still print “Hello” every second to the console. What am I missing here?

Marked As Solved

hubertlepicki

hubertlepicki

This is because iex master process is trapping exits, which means it doesn’t just crash when a linked process crashes. Instead, it receives a message, that is handled in form of error being printed to the user.

Your code will work as you’d expect if you didn’t start it from iex. Even in iex you can spawn an intermediary process to observe the behavior as you’d expect:

iex(2)> defmodule Greeter do                                                                                                                                              
...(2)>   def run do
...(2)>     task = Task.async(&greeter/0)
...(2)>     Task.await(task, 10_000)
...(2)>     IO.puts("work is done")
...(2)>   end
...(2)>
...(2)>   defp greeter do
...(2)>     Process.sleep(1000)
...(2)>     IO.puts("Hello")
...(2)>     greeter()
...(2)>   end
...(2)> end
warning: redefining module Greeter (current version defined in memory)
iex:2

{:module, Greeter,
<<70, 79, 82, 49, 0, 0, 6, 208, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 221,                                                                                           
  0, 0, 0, 23, 14, 69, 108, 105, 120, 105, 114, 46, 71, 114, 101, 101, 116,                                                                                              
  101, 114, 8, 95, 95, 105, 110, 102, 111, 95, ...>>, {:greeter, 0}}                                                                                                     
iex(3)> spawn fn -> Greeter.run() end
#PID<0.145.0>
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
iex(4)>

Also Liked

axelson

axelson

Scenic Core Team

Although if you had started the task with Task.async_no_link the task would keep running because it wouldn’t die when the other process does.

benlime

benlime

Ah that makes sense! I was assuming it’s due to iex. Big thanks for the confirmation :slight_smile:

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New

We're in Beta

About us Mission Statement