Hello my fellow elixir enthusiasts!
I want to start iex
in a port then shut it down and I want to do so gracefully. Here is my attempt with some things outcommented that I tried:
port = Port.open({:spawn, "iex"}, [:binary])
# wait for startup
receive do
{^port, {:data, "iex(1)> "}} -> :ok
end
# attemtps at closing iex in the port
# send(port, {:command, "^C"})
# send(port, {:command, "^C"})
# send(port, {:command, "^\\"})
# port_info = port |> Port.info() |> IO.inspect()
# os_pid = Access.get(port_info, :os_pid)
# System.cmd("kill", [to_string(os_pid)])
# attempts at closing just the port
Port.close(port)
# send(port, {self(), :close})
# receive do
# {^port, :closed} -> :ok
# end
IO.puts("script finished")
Running this leads to the following output:
tobi@qiqi:~/github/elixir_playground(main)$ mix run scripts/close_iex.exs; echo $?
script finished
Failed to write log message to stdout, trying stderr
11:22:42.395 [error] Process #PID<0.101.0> raised an exception
** (FunctionClauseError) no function clause matching in :string.trim_l/2
(stdlib 5.1.1) string.erl:884: :string.trim_l(:eof, [~c"\r\n", 9, 10, 11, 12, 13, 32, 133, 8206, 8207, 8232, 8233])
(stdlib 5.1.1) string.erl:303: :string.trim/3
(kernel 9.1) group.erl:534: :group.get_chars_apply/10
(kernel 9.1) group.erl:199: :group.io_request/6
(kernel 9.1) group.erl:125: :group.server_loop/3
Failed to write log message to stdout, trying stderr
11:22:42.406 [info] Failed to write to standard out (:epipe)
0
(you can also check it out at https://github.com/PragTob/elixir_playground/blob/main/scripts/close_iex.exs)
I can not get it to finish without any errors like these. To be clear, the command exits with 0
aka job well done.
I’m running 1.16.0-rc.1 x 26.1.2 on Linux but I’m pretty sure this happens on everything.
As for the inevitable question “why would anyone want to do this?” the script is based off a test in benchee: https://github.com/bencheeorg/benchee/blob/main/test/benchee_test.exs#L1032-L1050
It’s a test that makes sure Benchee warns when run inside iex
. The test works. It passes. However it spouts these error messages and seems to (partially) break the shell as I went down to diagnose that if the tests are initiated via Mix.Shell.IO.cmd
that then infinitely hangs which breaks ex_guard
which makes me sad (cc: @slashmili / Stopped working for benchee / only runs one command · Issue #68 · slashmili/ex_guard · GitHub ).
I’m confident though that if we solve this script, we solve that test, i.e. I’m confident it’s connected to the error Failed to write log message to stdout, trying stderr
Thanks everyone for taking a look and helping!