msante

msante

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?

Showing Posts 1 to 2

eileennoonan

eileennoonan

I am getting the same error. Did you ever figure this out?

If not … bump.

msante

msante OP

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews