PragTob

PragTob

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 elixir_playground/scripts/close_iex.exs at main · PragTob/elixir_playground · GitHub)

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: benchee/test/benchee_test.exs at main · bencheeorg/benchee · GitHub

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! :green_heart:

Showing Posts 1 to 7

garazdawi

garazdawi

Erlang Core Team

It should be possible to do Port.close/1 without seeing that crash. The crash is caused by a disagreement by group and IEx.Server.__parse__/2. group expects __parse__/2 to return {stop, eof, eof}, while it returns {stop, eof, []}. I think that in this case it is group that is wrong and it should be able to handle what __parse__/2 is doing. I’ll write a fix for this.

As a workaround you can close the shell by sending "\aq\n". i.e.

port = Port.open({:spawn, "iex"}, [:binary, :exit_status])
receive do
  {^port, {:data, "iex(1)> "}} -> :ok
end
send(port, {self, {:command, "\a"}})
send(port, {self, {:command, "q\n"}})
receive do
  {^port, {:exit_status, num}} -> num
end

Most likely this error does not exist when using Erlang/OTP < 26.

garazdawi

garazdawi

Erlang Core Team

https://github.com/erlang/otp/pull/7966

PragTob

PragTob OP

Wow, thanks a ton! Sorry for the assumption old erlang versions also had the problem - you’re right, they don’t!

@garazdawi it’s just amazing that I can ask a question here, someone from the Erlang Core team takes the time to look at it and answer it and figures out it’s actually a bug and implements a fix. All within ~3h. Absolutely amazing, tack så mycket!

The script needs some small adustments (self() and the last receive does not work/happen for me):

port = Port.open({:spawn, "iex"}, [:binary])

# wait for startup
receive do
  {^port, {:data, "iex(1)> "}} -> :ok
end

send(port, {self(), {:command, "\a"}})
send(port, {self(), {:command, "q\n"}})

@garazdawi any reference where I can learn about the commands/inputs we’re sending there? quick google wasn’t successful but probably that’s me.

garazdawi

garazdawi

Erlang Core Team

The script needs some small adustments (self() and the last receive does not work/happen for me):

I added :exit_status to the Port.open arguments to make the receive work.

@garazdawi any reference where I can learn about the commands/inputs we’re sending there? quick google wasn’t successful but probably that’s me.

“\a” is the same as typing Ctrl+G, which is ascii code 7 i.e. bell/alert. So by sending \a you enter Job Control Mode in the shell, and from there you just type “q\n” to exit the shell.

I suppose sending ":init.stop\n" would achieve almost the same thing, though it would require the shell to be in a clean state.

PragTob

PragTob OP

woops sorry :expressionless: I should read better :face_in_clouds:

Thanks a ton!

PragTob

PragTob OP

While the error is gone, something is still odd at least with exit_status it doesn’t work for me, I get notes about syntax errors instead:

port = Port.open({:spawn, "iex"}, [:binary, :exit_status])
receive do
  {^port, {:data, "iex(1)> "}} -> :ok
end
send(port, {self(), {:command, "\a"}})
send(port, {self(), {:command, "q\n"}})
receive do
  {^port, {:exit_status, num}} -> num
after
  5_000 ->
    messages = :erlang.process_info(self(), :messages)
    IO.inspect(messages, label: "messages")
end
tobi@qiqi:~/github/elixir_playground(main)$ mix run wut.exs 
messages: {:messages,
 [
   {#Port<0.6>,
    {:data,
     "Interactive Elixir (1.16.0-rc.1) - press Ctrl+C to exit (type h() ENTER for help)\n"}},
   {#Port<0.6>,
    {:data,
     "** (SyntaxError) invalid syntax found on iex:1:1:\n    error: unexpected token: alert (column 1, code point U+0007)\n    │\n  1 │ \aq\n    │ ^\n    │\n    └─ iex:1:1\n    (iex 1.16.0-rc.1) lib/iex/evaluator.ex:294: IEx.Evaluator.parse_eval_inspect/4\n    (iex 1.16.0-rc.1) lib/iex/evaluator.ex:187: IEx.Evaluator.loop/1\n    (iex 1.16.0-rc.1) lib/iex/evaluator.ex:32: IEx.Evaluator.init/5\n    (stdlib 4.3.1.3) proc_lib.erl:240: :proc_lib.init_p_do_apply/3\n"}},
   {#Port<0.6>, {:data, "iex(1)> "}}
 ]}

(both on erlang 25 and 26)

edit: Maybe I need to wait between the 2 sends ?

garazdawi

garazdawi

Erlang Core Team

Yes, most likely you do. Wait until you receive -->.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews