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?