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
Trending in Questions
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
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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
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
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
minhajuddin
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_infothe default function should be overridden. So, I guess this is needed to catch stray messages.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
@minhajuddin yes you’re right. If you override
handle_infoyou will need to have a catch all implementation as well, otherwise as you stated the messages will not be removed from the inbox.Qqwy
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
.
Qqwy
I made a PR and @josevalim was very quick to reply:
So it seems that we arrived at an incorrect conclusion before.
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
it clearly states the gen server would crash if you don’t supply the match all override
minhajuddin
Thanks for verifying it
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
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
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_infofunction 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_infountil you can explain why you do not want it.