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

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics 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
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
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
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
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
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

We're in Beta

About us Mission Statement