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

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
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
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
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

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

We're in Beta

About us Mission Statement