safe_exec_env v0.1.1 SafeExecEnv

It is often desirable to run natively compiled code (C, C++, Rust, ..) from a BEAM application via a binding. The motivation may be better performance or simply to access an external library that already exists. The problem is that if that native code crashes then, unlike with native BEAM processest, he entire VM will crash, taking your application along with it,

SafeExecEnv provides a safe(r) way to run natively compiled code from a BEAM application.

If the native code in question can be reasonably expected to never crash, then this precaution is unecessary, but if native code that might crash is being run then this library provides a layer of insulation between the code being executed and the virtual machine the main application is being run on.

Using SafeExecEnv is easy; simply add it to a Supervisor:

defmodule MyApplication do
  def start(_type, _args) do
    children = [SafeExecEnv]
    opts = [strategy: :one_for_one, name: MyApplication.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

Then you may run functions safely by calling SafeExecEnv::exec with the function. Captured, anonymous, and Module / Function / Arguments (MFA) style function passing is supported.

SafeExecEnv works by spawning a second BEAM VM to run the functions in. If that VM crashes then the SafeExecEnv server will also crash. When supervised, this will cause the SafeExenv to be restarted, and the external VM will be started again. Calls to SafeExecEnv may fail during that time, and will need to be tried again once available.

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor.

Executes a function in the safe executable environment and returns the result. The SafeExecEnv server is presumed to be started.

Executes a function with the provided argument (in usual "MFA" form) in the safe executable environment and returns the result. The SafeExecEnv server is presumed to be started.

Returns the name of the BEAM node being used as the safe exec environment

Returns true if the node is running and reachable, otherwise false

Link to this section Functions

Link to this function

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

Specs

exec(fun :: function()) :: any() | {:error, reason :: String.t()}

Executes a function in the safe executable environment and returns the result. The SafeExecEnv server is presumed to be started.

Link to this function

exec(module, fun, args)

Specs

exec(module :: atom(), fun :: atom(), args :: list()) ::
  any() | {:error, reason :: String.t()}

Executes a function with the provided argument (in usual "MFA" form) in the safe executable environment and returns the result. The SafeExecEnv server is presumed to be started.

Specs

get() :: String.t()

Returns the name of the BEAM node being used as the safe exec environment

Specs

is_alive?() :: boolean()

Returns true if the node is running and reachable, otherwise false