benonymus

benonymus

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

Showing Posts 1 to 10

kokolegorille

kokolegorille

Where is your timeout defined?

benonymus

benonymus OP

The 50 I believe

kokolegorille

kokolegorille

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}

benonymus

benonymus OP

kokolegorille

kokolegorille

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.

kokolegorille

kokolegorille

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…

benonymus

benonymus OP

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.

benonymus

benonymus OP

I think I was on the right path based on this: gen_server — OTP 29.0.2 (stdlib 8.0.1)
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

amnu3387

amnu3387

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.

benonymus

benonymus OP

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

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews