msante

msante

Getting random error: erl_child_setup: failed with error 32 on line 282 (using Port)

Sorry if my question is silly, I am relatively new to Elixir.

I made a small module to handle the execution of external programs. This is my code:

defmodule Abn.Shell do
  @default_timeout 2000
  @default_retry 2
  @default_error_log_file "/tmp/abn_errors/"

  use Abn.Lib
  alias Abn.Log

  ###########################################################################3
  ## Module API

  def run(command, timeout \\ @default_timeout, retry \\ @default_retry) do
    random_filename = if (@log_errors), do: :erlang.unique_integer([:positive]), else: ""
    script = """
    #!/usr/bin/env setsid /bin/bash
    #{command}   
    """
    try do
      port = Port.open({:spawn, script}, [:binary])
      monitor = Port.monitor(port)
      {:os_pid, ospid} = Port.info(port, :os_pid)
      output = get_output({port, monitor}, timeout, ospid)
      kill(ospid)
      Port.demonitor(monitor, [:flush])
      send(port, :close) # just in case...
      if (output == :timeout and retry > 1) do
        run(command, timeout, retry - 1)
      else
        output
      end
    rescue
      e ->
        Log.log(:error, "[SHELL]: #{inspect e}")
        :error
    end
  end

  ###########################################################################3
  ## Private Tools

  defp get_output({ port, monitor }, timeout, ospid, output \\ "") do
    receive do
      {:DOWN, ^monitor, :port, ^port, _} ->
        output
      {^port, {:data, data}} ->
        get_output({ port, monitor }, timeout, ospid, output <> data)
      msg ->
        Log.log(:warning, "[SHELL]: Port #{inspect port}. Breaking loop, unknown 'get_output' message (#{inspect msg})")
        output
      after timeout ->
        Log.log(:warning, "[SHELL]: Process exceed timeout, killing process...")
        :timeout
    end
  end

  defp kill(ospid) do
    System.shell("kill -9 #{ospid}")
  end
 
end

This module allows me to run external processes with a timeout (and n retries) for those cases in which the process takes too long.

The issue is that randomly, every so often I get the error ‘erl_child_setup: failed with error 32 on line 282’ which completely kills the process that invoked Shell.run and the supervisor of that process doesn’t even get an EXIT notification. I have tried to replicate the error but have not been able to.

The context in which this module is used is in a data collector that runs about 30 processes which simultaneously use the Shell module to run external processes.

Versions that I use

Erlang/OTP 27 [erts-15.1]

IEx 1.18.0-dev (a4adaa8) (compiled with Erlang/OTP 27)

Any idea where to start looking for the problem?

Most Liked

msante

msante

Well, I’m not sure I can say I discovered the source of the problem, but I did manage to stop it happening again.
If you look at my code you will see that there is a part enclosed in try/rescue. Well, I simply removed the try/rescue (which by the way I’m not sure why I put it there in the first place). From then on it didn’t happen again.

Sorry if I can’t be of more help.

Where Next?

Popular in Questions Top

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
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
Tee
can someone please explain to me how Enum.reduce works with maps
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
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

Other popular topics Top

vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
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
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
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
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New

We're in Beta

About us Mission Statement