RustySnek

RustySnek

Clearing out process mailbox - is using :c.flush() a good idea?

I’m working on a project that awaits an :ok signal while also trapping and awaiting any exits that happen midway. Looks something like this:

  Process.flag(:trap_exit, true)
  # Call a process that will send() back :ok after some time
  receive do
    :ok -> :nice
    {:EXIT, pid, reason} -> :bad
    {:EXIT, reason} -> :bad
  after
    10_000 ->
      :timeout
  end

The issue is that if there is an :EXIT that happened prior to running this function, it will instantly return :bad. So I was thinking about adding something like :c.flush() to clear out the mailbox before we start the receive, however I’m not really sure if this is really a good idea. Or perhaps I’m going down some dark path that I shouldn’t go into…

Would love to hear your opinion :skull:

Marked As Solved

zachallaun

zachallaun

You can write a flush_exits like so:

defp flush_exits do
  receive do
    {:EXIT, _, _} -> flush_exits()
    {:EXIT, _} -> flush_exits()
  after
    0 -> :ok
  end
end

This will selectively remove exits from the mailbox, returning when there are none left. :slight_smile:

Also Liked

zachallaun

zachallaun

Do you want to ignore the first exit signal if it comes before entering the receive block, or do you want to ensure that you don’t miss the ok that’s already in the mailbox but after the exit?

RustySnek

RustySnek

Oh right, this way just exits get cleared out, should be less destructive…

I guess that’s solved, thank you very much.

Where Next?

Popular in Questions Top

qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement