ashton314
Howdy! I’m a researcher trying to understand better how people use Elixir. I have a fair amount of personal experience with Elixir, but I need more than one data point.
I’m particularly interested in GenServers—particularly how they can go wrong.
Have you ever encountered a GenServer that was sensitive to the order of certain messages? E.g. imagine you had a GenServer acting as a proxy for a web service; you might need to send the server :connect to get a token, and only after that could you send it {:request, body} messages.
When have you had bugs with GenServers?
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #elixirconf-us
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
D4no0
One of the dangerous things that can go wrong with genserver is leaking memory when using large binaries. Depending on your orchestration, you can easily take the server down too if you run out of memory.
There are lot of articles on this topic, here is one of them: https://medium.com/coletiv-stories/genservers-memory-issues-ef42cc42e3e9
derek-zhou
GenServer handled messages in order; so message ordering is usually not a problem. My usual bugs with GenServer are:
al2o3cr
As @derek-zhou points out, message-ordering isn’t generally much of a problem. However, the code you described sounds like it has a different issue that’s more common: using one process when you really want a supervision tree.
Instead of a single “proxy” that tracks all the connections made through it, a “BEAMier” approach would make the initial
connectspawn a new process focused solely on that connection.On the flip-side of “too much state in one process” is “no state in the process”, the dreaded “using GenServers for code organization”. I’ll leave further discussion to the docs.
A gotcha that’s easy to miss until it becomes a headache in production: the restart threshold
max_restartsin supervisors needs to be tuned once there are many children. Very frustrating to have 3 crashes in 5s bounce yourDynamicSupervisorwith 3k childrenWhen that supervisor restarts (or on a cold system boot), you can also run into another scaling problem: the “thundering herd”, where those 3k children start up and ALL try to make a SQL query in the same 10ms. Combine those DB timeouts with a low
max_restartson the supervisor and you are now officially Having A Bad Time.ashton314
Yes, I know the BEAM guarantees that messages will be delivered in the order that they were sent from a process, but there isn’t any guarantee on the order that two different processes’ messages arrive.
More generally, I’m looking for cases where there’s some implicit protocol—maybe (hopefully!) explained in the docs—but where clients of the GenServer have to be mindful about e.g. doing some kind of set-up with the GenServer prior to sending other kinds of messages.
derek-zhou
Without knowing what kind of problem you are trying to solve, I do not know what to say. A GenServer is a stateful entity that pretends to be a collection of functions. If you require the client to be “mindful” of the state of the GenServer, then the illusion of function calls quickly fades, plus you need to duplicate part of the state at client side, which means you don’t have a single source of truth anymore. I hope the challenges that you are facing are big enough to warrant all these complexities.
ashton314
Just to clarify, I’m not trying to build anything my self per se—I’m investigating how people use GenServers and some problems they might encounter, and potential ways to help fix those problems.
It might be just as simple as remembering to perform one action before another. A lot of people seem to work hard on making “bulletproof” GenServers—which is a great thing from a software engineering perspective. Something tells me there should be some cases where it’s hard to make GenServers as bulletproof as one might like.
rvirding
As you have already mentioned there are no real guarantees in which order the messages sent from different processes will arrive. If this is really important to you then you will have to synchronise the sending processes in some way.
One thing which is guaranteed is that the GenServer process handles the messages in the order in which they arrive and it only handles one message at a time. So for example when it gets a call messages it will call the
handle_callcallback and the GenServer toploop won’t handle any messages in anyway until thehandle_callreturns.Note however if you in one of callbacks communicates with other processes, this is very very very common, then as there is only one message queue per process (NO there is no way around this) then your messages might become interspersed with more requests coming into the GenServer. It is upto to you make sure your message tagging ensures that you don’t receive any GenServer requests cause then they are gone. Also remember that any messages you leave in the message queue will be picked up by the GenServer toploop and most likely be processed in a
handle_infocallback.Remember the BEAM/Erlang/Elixir process semantics is very simple and straight forward, e.g one message queue where everything gets entered in order and there are no interrupts. Its very the KICASS principle.

owenfi
I’m a bit lost on that and would love more explanation.
lud
He’s saying that if your genserver process sends and receives messages, you must ensure that your
receiveclauses will only match expected messages, and not match messages that are intended to be handled by your handle_call/handle_cast callbacks. Otherwise, as you received the message in your custom code then your handle_call callback will not be called (just because it will not bereceive’d by theGenServermodule).rvirding
Yes. The thing to really point out is that there are no “special” messages, just messages. This is why you need to get the tagging right so that the server and you callback code don’t get mixed up.