samuelventura

samuelventura

How to keep the Code.eval_string environment on async execution

What is the proper way to evaluate a string and get a stack trace that honors the provided file: script.exs environment across all the code defined in that script regardless of whether it is executed in the current process or deferred to an async task?

The first two samples refer to the provided file: script.exs on the stack trace and the last one loses it.

Sample 1 keeps the reference to script.exs in its stacktrace

iex(1)> Code.eval_string("""
try do
  raise "RAISE"
rescue
  e -> {e, __STACKTRACE__}
end
""", [], file: "script.exs")

{{%RuntimeError{message: "RAISE"},
[
  {:elixir_eval, :__FILE__, 1, [file: ~c"script.exs", line: 2]},
  {:elixir, :eval_external_handler, 3,
   [file: ~c"src/elixir.erl", line: 386, error_info: %{module: Exception}]},
   ...
]}, []}

Sample 2 keeps the reference to script.exs in its stacktrace

iex(2)> Code.eval_string("""
fun = fn -> raise "RAISE" end
try do
  fun.()
rescue
  e -> {e, __STACKTRACE__}
end
""", [], file: "script.exs") 
{{%RuntimeError{message: "RAISE"},
[
  {:elixir_eval, :__FILE__, 1, [file: ~c"script.exs", line: 1]},
  {:elixir, :eval_external_handler, 3,
   [file: ~c"src/elixir.erl", line: 386, error_info: %{module: Exception}]},
  ...
]}, [fun: #Function<43.81571850/0 in :erl_eval.expr/6>]}

Sample 3 loses the reference to script.exs in its stacktrace

iex(3)> Code.eval_string("""
fun = fn -> raise "RAISE" end
Task.async(fn -> 
  try do
    fun.()
  rescue
    e -> {e, __STACKTRACE__}
  end
end) |> Task.await(:infinity)
""", [], file: "script.exs")
{{%RuntimeError{message: "RAISE"},
[
  {:elixir, :eval_external_handler, 3,
   [file: ~c"src/elixir.erl", line: 386, error_info: %{module: Exception}]},
  {:erl_eval, :do_apply, 7, [file: ~c"erl_eval.erl", line: 919]},
  {:erl_eval, :try_clauses, 10, [file: ~c"erl_eval.erl", line: 1233]},
  {Task.Supervised, :invoke_mfa, 2,
   [file: ~c"lib/task/supervised.ex", line: 101]}
]}, [fun: #Function<43.81571850/0 in :erl_eval.expr/6>]}

Need this to implement scripting with accurate tracing of unhandled exceptions in async code.

Things looking into at the moment:

  1. Process.info(self(), :current_stacktrace)
  2. Last call optimisation

First Post!

josevalim

josevalim

Creator of Elixir

What happens in the last example if you call List.flatten([]) or any similar example, so the last instruction is not raise, but something else to prevent last call optimization on raise?

Most Liked

samuelventura

samuelventura

You mean like sample below? Apparently the reference to script.exs is lost as well.

iex(4)> Code.eval_string(
  """
  fun = fn ->
    raise "RAISE"
    List.flatten([])
  end

  Task.async(fn ->
    try do
      fun.()
    rescue
      e -> {e, __STACKTRACE__}
    end
  end) |> Task.await(:infinity)
  """,
  [],
  file: "script.exs"
)

{{%RuntimeError{message: "RAISE"},
[
  {:elixir, :eval_external_handler, 3,
   [file: ~c"src/elixir.erl", line: 386, error_info: %{module: Exception}]},
  {:erl_eval, :do_apply, 7, [file: ~c"erl_eval.erl", line: 919]},
  {:erl_eval, :exprs, 6, [file: ~c"erl_eval.erl", line: 271]},
  {:erl_eval, :try_clauses, 10, [file: ~c"erl_eval.erl", line: 1233]},
  {Task.Supervised, :invoke_mfa, 2,
   [file: ~c"lib/task/supervised.ex", line: 101]}
]}, [fun: #Function<43.81571850/0 in :erl_eval.expr/6>]}
josevalim

josevalim

Creator of Elixir

Oh, it works on Erlang AST rather than BEAM. :smiley: you can find it on erl_eval.

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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement