Legion.Sandbox.Runner (Legion v0.5.0)

View Source

Runs a sandbox evaluation function in a monitored process with resource limits. Shared by every Legion.Sandbox implementation, so timeout, memory, and CPU budgets behave identically regardless of language.

limits bound what the evaluating process may take from the node:

  • :max_heap — memory budget in bytes, applied twice: the VM kills the eval when its heap and stack exceed it, and the parent kills it when the binaries it references do (those live off-heap, so the VM flag misses them). Set :infinity to disable (default: 256_000_000)
  • :max_reductions — CPU budget in reductions, enforced by polling. Set :infinity to disable (default: :infinity)
  • :priority — scheduler priority for the eval, any value Process.flag/2 accepts (default: :low, so generated code yields to the rest of the node)

Summary

Functions

Runs eval_fun in a spawned, monitored, resource-limited process.

Functions

run(eval_fun, timeout_ms, limits \\ [])

Runs eval_fun in a spawned, monitored, resource-limited process.

eval_fun must return {:ok, {value, bindings}} or {:error, reason}; that result is passed through. The runner adds {:error, :timeout}, {:error, reason} for exceeded limits, and {:error, {:process_crashed, reason}} for crashes.