safe_exec_env v0.2.0 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
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
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