Genserver.call with timeout doesn't end up with :timeout message

Hey there,

I created a small genserver and it works great but I want to add a timeout to a call, and to test it in the handle_call/3 I am doing Process.sleep/1 with a longer time then the timeout, but instead of getting a :timeout message to catch in handle_info/2 I am just getting an error that says:

** (exit) exited in: GenServer.call(Checksum.Checksum, :calculate, 50)
** (EXIT) time out

here is the call:

  def calculate_checksum() do
    GenServer.call(__MODULE__, :calculate, 50)
  end

here is the handle:

  @impl GenServer
  def handle_call(:calculate, _, state) do
    Process.sleep(60)
    result = 0

    {:reply, {:ok, result}, state}
  end

this is where I would expect to catch the timeout:

  @impl GenServer
  def handle_info(:timeout, state) do
    {:reply, {:error, :timeout}, state}
  end

What am I missing? :smile:

Thanks

Where is your timeout defined?

The 50 I believe

No, it’s should not be like that…

It’s when You return from any call that You specify timeout.

like {:reply, state, state, timeout}

But what about this?
https://hexdocs.pm/elixir/GenServer.html#call/3

It is not the same timeout. One is the client timeout, one is the server timeout.

Please note server handlers should also return timeout, even init.

2 Likes

Here is an example…

  @timeout 5 * 60 * 1_000

  @impl GenServer
  def handle_call(:get_state, _from, state), do: {:reply, state, state, @timeout}

  # Timeout handler
  @impl GenServer
  def handle_info(:timeout, state), do: stop_and_clean(state, {:shutdown, :timeout})

stop and clean is a custom function…

1 Like

And how can I make this timeout?

What I actually want to use is to show a different response if the timeout happens.
Now I added the timeout to where you said, and I am getting the :timeout message.
But where I am making the call to the genserver I am still getting the result.

with {:ok, checksum} <- Checksum.calculate_checksum() do
      conn
      |> put_status(200)
      |> put_view(ChecksumView)
      |> render("checksum.json", checksum: checksum, message: "Checksum calculated successfully!")
    else
      _ ->
        conn
        |> put_status(488)
        |> put_view(ChecksumView)
        |> render("408.json")
    end

As you saw from my handle_call for :calculate I am returning {:ok, result}, but I am also seeing the :timeout message being handled.

  @impl GenServer
  def handle_info(:timeout, state) do
    {:noreply, state}
  end

What would be ideal if there is a timeout instead of getting {:ok, result} I could get something like {:error, :timeout} but If I do reply with this error and state in the handle_info then it is a bad return value.

I think I was on the right path based on this: Erlang -- gen_server
And I think I have this problem:
https://groups.google.com/g/elixir-lang-talk/c/7tS9tX7fLpg

I tried to put the genserver call in try do but no success yet

When you do a call with a timeout this is the timeout the caller process will wait before it throws an exception. You need to rescue this exception at the call site.

The timeout in a gen_server (the one you can set in the callback of any handle) is an internal timeout that sets an amount of time the gen_server can be without receiving any message, which upon it elapsing triggers an internal message of :timeout that you can handle. This timeout is disabled whenever a message arrives in the gen_servers mailbox and needs to be set again if you wish it to trigger again.

1 Like

Hey, thanks for the response!

I am trying this:

  def calculate_checksum() do
    try do
      GenServer.call(__MODULE__, :calculate, 15)
    rescue
      _ -> {:error, :timeout}
    end
  end

but no success

Ups, it’s probably catch that you need.

That doesn’t work either :confused:

I tried to exact same with catch instead of rescue and the result is the same error:

** (exit) exited in: GenServer.call(Checksum.Checksum, :calculate, 15)
    ** (EXIT) time out

You need to catch the exit properly: try, catch, and rescue - The Elixir programming language

4 Likes

Oh thanks!

Now the problem is that your calling process will still receive a message with the response, because the GenServer will send it, as it will have received the call.

So what you can do is wrap your call in a task:

def calculate_checksum() do
    Task.async(fn ->
      try do
        GenServer.call(__MODULE__, :calculate, 15)
      catch
        :exit, {:timeout, {GenServer, :call, _}} -> {:error, :timeout}
      end
    end)
    |> Task.await()
  end

Note that Task.await also has a timeout, and like GenServer.call/3 the default is 5000, You don’t care as you are targetting 15ms here. But you can set it to :infinity it that makes sense.

The task process is linked to the caller, so if you have an error/exit that is not the timeout, it will still propagate to your calling process, no risk of missing errors.

The task will return with the checksum or {:error, :timeout}. If there is a timeout, as I have said the GenServer will still send the response. But it will send it to the task process, which will by dead by then. And sending a message to a non-existent process is fine in Elixir. That is the way to safely ignore the response.

Finally, as you handle a short timeout and ignore the response, keep in mind that the GenServer will still have to calculate the checksum, it will still do the requested work to send the response which will be ignored.

In practice it does not seem like a problem that the calling process receives the response later, because I already sent the response by the time the result would arrive.

Do you think it is still worth it to wrap it in a Task?

I really depends. What do you mean by “I already sent the response”? What is the context of your calling process?

I call the calculate_checksum from a with where I expect it to be {:ok, result}, and I have an else in the with where I send a different response if I got the timeout error.

Like this

    with {:ok, checksum} <- Checksum.calculate_checksum(String.to_integer(params["timeout"])) do
      conn
      |> put_status(200)
      |> put_view(ChecksumView)
      |> render("checksum.json", checksum: checksum, message: "Checksum calculated successfully!")
    else
      _ ->
        conn
        |> put_status(408)
        |> put_view(ChecksumView)
        |> render("408.json")
    end

So the context is a HTTP request handler / Phoenix controller. Then I guess you don’t care, indeed, though I am not sure.

1 Like