minhajuddin

minhajuddin

Catching stray messages which may fill up your message inbox is considered a good practice in erlang. Do we need to catch them even in a GenServer?

defmodule Ticker do
  use GenServer

  def handle_info(:tick, state) do
    schedule_tick(state)# this makes sure that we catch up if a work is delayed
    work(state)
    {:noreply, state}
  end

  # to catch stray messages?
  require Logger
  def handle_info(oops, state) do
    Logger.warn("UNEXPECTED_EVENT_IN_TICKER: #{inspect oops}")
    {:noreply, state}
  end

end

Showing Posts 1 to 10

minhajuddin

minhajuddin OP

I just looked at the code elixir/lib/elixir/lib/gen_server.ex at main · elixir-lang/elixir · GitHub and there is a default implementation which should catch all messages, However as soon as we define a handle_info the default function should be overridden. So, I guess this is needed to catch stray messages.

hubertlepicki

hubertlepicki

Yes you should define it. And your hunch is good, for exactly the reason you mention.

Elixir’s intro docs on GenServer make it clear in this section:

swelham

swelham

@minhajuddin yes you’re right. If you override handle_info you will need to have a catch all implementation as well, otherwise as you stated the messages will not be removed from the inbox.

Qqwy

Qqwy

TypeCheck Core Team

I just looked through the Elixir documentation on GenServer.handle_info, and it is not mentioned there yet. I think this would be a great clarification. I’ll open a Pull Request :slight_smile: .

Qqwy

Qqwy

TypeCheck Core Team

I made a PR and @josevalim was very quick to reply:

The GenServer always remove the message. If you don’t implement the callback, it means the GenServer will fail for unknown messages, which may actually be desired. So if you don’t want to define a catch-all clause, that’s ok.

So it seems that we arrived at an incorrect conclusion before.

swelham

swelham

Thats interesting, I didn’t realise that. Has this always been the case do you know? I based my answer from what I read in Elixir in Action and wonder if it’s just where the book as fallen out of date.

edit: actually scrap that. I just flicked back through the book to double check. It appears my memory is awful :icon_sad: it clearly states the gen server would crash if you don’t supply the match all override

minhajuddin

minhajuddin OP

Thanks for verifying it :slight_smile:

hubertlepicki

hubertlepicki

I do think the conclusion was correct (for the most part). You probably don’t want your GenServers to crash at random.

The fact that it will crash sooner than later is other story. But the result is the same, crashing and unstable GenServer. You most likely want to do catch all.

Qqwy

Qqwy

TypeCheck Core Team

I would not consider a crash when an unexpected message is received a ‘random’ crash (in contrast to an ‘inbox is full’ crash): It is a fail-fast indication that you are passing messages around that cannot be understood by the receiver. As José astutely pointed out, this is useful in certain situations.

Then again, most messages we use with GenServers are of course cast/calls, which do not use handle_info at all.

gregvaughn

gregvaughn

If you can’t explain, for your particular application, why you want to crash (and restart into a known state) when an unexpected message is received, then IMHO, you should have that catch all handle_info function that at least logs what it got. When I was first learning OTP, I got confused at messages I did and did not receive until it all clicked, and it’s only made worse if the GenServer is restarting in the middle of it all.

I think you should have the catch all handle_info until you can explain why you do not want it.

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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews