venomnert

venomnert

How does process treat unhandled messages?

Context:

I’m reading through “Elixir in Action” by“ Saša Jurić. In section 5.2.2 he provides the following algorithm on how processes read messages:

The receive expression works as follows:

  1. Take the first message from the mailbox.
  2. Try to match it against any of the provided patterns, going from top to bottom.
  3. If a pattern matches the message, run the corresponding code.
  4. If no pattern matches, put the message back into the mailbox at the same position it originally occupied. Then try the next message.
  5. If there are no more messages in the queue, wait for a new one to arrive. When a new message arrives, start from step 1, inspecting the first message in the mailbox.
  6. If the after clause is specified and no message is matched in the given amount of time, run the code from the after block.

Scenario:

Assume that a process receives these three message in the following order:

  • message a
  • message b
  • message c

Here is the process receive statement:

receive do
  {:message_b, value} -> IO.puts "Message b is complete" 
  {:message_c, value} -> IO.puts "Message c is complete" 
end

Question:

Will the process always start from the top of the queue all the time, even if it already knows that it can’t handle the first message? In above case it always start off by trying to see if it can handle message_a (which it can’t) and then continue to proceed to the next message.

Marked As Solved

jwarlander

jwarlander

Yes, indeed it will… The result, if your process doesn’t ever flush out otherwise unhandled messages, is that it may end up sifting through millions of them just to process those that are relevant. This is something that you’ll need to be aware of; either crash on an unexpected message, or (at some point at least) throw it away.

Also Liked

al2o3cr

al2o3cr

FWIW this behavior is one of the reasons it’s usually preferable to use structured machinery around send + receive, like gen_server and friends - unexpected messages will be converted to crashes.

sb8244

sb8244

Author of Real-Time Phoenix

Here’s something you can run locally:

defmodule MyLoop do
  def loop do
    IO.puts "Entering the loop"
    
    receive do
      :msg_b ->
        IO.puts "msg b"
        loop.()
      :msg_c ->
        IO.puts "msg c"
        loop.()
    end
  end
end

pid = spawn(&MyLoop.loop/0)

send(pid, :msg_a)
send(pid, :msg_b)
send(pid, :msg_c)
send(pid, :msg_a)
send(pid, :msg_b)
send(pid, :msg_c)
Process.info(pid, :messages)

My output:

Entering the loop
iex(17)> send(pid, :msg_a)
:msg_a
iex(18)> send(pid, :msg_b)
msg b
Entering the loop
:msg_b
iex(19)> send(pid, :msg_c)
msg c
Entering the loop
:msg_c
iex(20)> send(pid, :msg_a)
:msg_a
iex(21)> send(pid, :msg_b)
msg b
Entering the loop
:msg_b
iex(22)> send(pid, :msg_c)
msg c
:msg_c
Entering the loop
iex(26)> Process.info(pid, :messages) 
{:messages, [:msg_a, :msg_a]}

You can see that msg_a will pile up in the process queue, as @jwarlander mentions

dom

dom

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement