jswny

jswny

Why does this simple GenServer timeout?

I’ve condensed the problem I found while playing around with GenServers (I don’t know much about OTP yet) into the following script:

defmodule Test do
  use GenServer

  def handle_call({:work, item}, _from, _state) do
    IO.puts "Processing item #{item}..."
    :timer.sleep(3000)
    {:reply, "Done processing item #{item}!", []}
  end
end

GenServer.start_link(Test, [], name: Test)

Task.start(fn -> GenServer.call(Test, {:work, 1}, 5000) end)
Task.start(fn -> GenServer.call(Test, {:work, 2}, 5000) end)

:timer.sleep(15000)

When I run this, I get the following:

C:\Users\Joe\test>elixir script.exs
Processing item 1...
Processing item 2...

12:17:24.056 [error] Task #PID<0.77.0> started from #PID<0.69.0> terminating
** (stop) exited in: GenServer.call(Test, {:work, 2}, 5000)
    ** (EXIT) time out
    (elixir) lib/gen_server.ex:737: GenServer.call/3
    (elixir) lib/task/supervised.ex:85: Task.Supervised.do_apply/2
    (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Function: #Function<1.117183472 in file:script.exs>
    Args: []

It seems to me that this should never timeout because handle_call only sleeps for 3000 milliseconds, and the call is allotted 5000 milliseconds of timeout time. So then, why do I get a timeout in the second call?

Most Liked

peerreynders

peerreynders

Based on GenServer.reply/2:

defmodule Test do
  use GenServer

  def handle_call({:work, item}, from, state) do
    IO.puts "Processing item #{item}..."
    Process.send_after self(), {:reply, from, item}, 3_000
    {:noreply, state}
  end

  def handle_info({:reply, from, item}, state) do
    GenServer.reply from, "Done processing item #{item}!"
    {:noreply, state}
  end

end

GenServer.start_link(Test, [], name: Test)

Task.start(fn -> IO.puts (GenServer.call Test, {:work, 1}, 5_000) end)
Task.start(fn -> IO.puts (GenServer.call Test, {:work, 2}, 5_000) end)

:timer.sleep(15_000)

.

$ elixir test.exs
Processing item 1...
Processing item 2...
Done processing item 1!
Done processing item 2!
$

So GenServer doesn’t have to reply to a call immediately. It can store “intermediate results” in its own state - for example when it’s still “waiting” for results from other processes while not being blocked so that it can accept new requests even before the other work is completely done.

The one big drawback to GenServer.call/3 is that it blocks the client process. So when processes “co-operate” it isn’t that uncommon to use GenServer.cast/2 instead by simply including a return pid (or from) in the request message proper, for the eventual result message - that way none of the processes ever have to be blocked - they just process the (cast) messages (and results) as they come in.

PS: version that gets the GenServer state actively involved:

defmodule Test do
  use GenServer

  def handle_call({:work, item}, from, old_state) do
    state = add_result old_state, from, item
    IO.puts "Processing item #{item}..."
    Process.send_after self(), {:reply, from}, 3_000
    {:noreply, state}
  end

  def handle_info({:reply, from}, old_state) do
    {item, state} = pop_result old_state, from
    GenServer.reply from, "Done processing item #{item}!"
    {:noreply, state}
  end

  defp add_result(state, from, item),
    do: Map.put state, from, item

  defp pop_result(state, from) do
    item = state[from]
    new_state = Map.delete state, from
    {item, new_state}
  end

end

GenServer.start_link(Test, %{}, name: Test)

Task.start(fn -> IO.puts (GenServer.call Test, {:work, 1}, 5_000) end)
Task.start(fn -> IO.puts (GenServer.call Test, {:work, 2}, 5_000) end)

:timer.sleep(15_000)

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
vegabook
I'm brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31107 143
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement