Quitting an iex session - any difference between System.halt/1 & CTRL-C?

Generally when I want to quit an iex session I use the double control-c dance.
For unknown reasons this refused to work earlier today but I am not getting into that.
Because of it though I tried System.halt/1 to exit the session and that worked.

Is there a difference between the 2 methods?

Else I could add something like this to my .iex.exs

defmodule IexExtra do
  def exit, do: System.halt()
end
import IexExtra

Then I can end my sessions with a plain and simple exit

I often use control-\

That combination seems to be bound to tmux, it jumps back to the last pane.

Maybe the normal close (ctlr double c) uses stop instead, which is meant to be less aggressive than System.halt.

There may have been something preventing the sequential shutdown of the runtime?

Definitely not System.stop, when I do that I see a lot of errors/warnings I do not see with the ctrl-c nor System.halt

WHOA! I had no idea that worked.

1 Like

Here’s another tip courtesy of the System module.

You know “that” error…?

** (RuntimeError) could not compile application: my_app.

You must restart your server after changing the following files:

  • config/dev.exs

    (phoenix 1.7.12) lib/phoenix/code_reloader/server.ex:236: Phoenix.CodeReloader.Server.mix_compile_unless_stale_config/4

Just do as it says… :lol:

iex> System.restart()

It’ll recompile and reload everything for you.

5 Likes

The first CTRL-C gets you into the break menu which gives you a few diagnostic options. When you then press q or a and ENTER, or simply another CTRL-C you are effectively doing a System.halt(0) call.

CTRL-\ bypasses the break menu altogether and quits.

System.stop(0) is more graceful than System.halt(0). System.stop(0) signals :sigstop to the Erlang VM. It’s the same as calling :init.stop(0). From the documentation:

All applications are taken down smoothly, all code is unloaded, and all ports are closed before the system terminates by calling halt(Status).

System.halt(0) signals a :sigquit to the VM, which is equal to calling :erlang.halt(0). It abruptly shuts down everything (no apps are gracefully stopped or ports properly closed.)

You’ll notice that System.stop(0) takes a moment to shut down and returns an :ok atom if successful. System.halt(0) is essentially immediate.

5 Likes

I always assumed that System.halt was more aggressive in that sense. I mean, System.stop takes longer, so it must be exiting more gracefully!

That being said, I always exit by double-pressing Ctrl + C, and I’ve never had any issues with ports being left open, or any bad side effects like that.

Would love to get this cleared up, though, simply for the sake of “best practices”.

There are scenarios where CTRL-C twice could take down daemons running on a remote node. Not much harm if you are just messing around locally. However, if we are talking best practices, you’d probably want CTRL-G to bring up the User switch command menu, followed by q. Try h to see the options available.

6 Likes

I use zellij

I learned ctrl-g q from an Erlanger early in my Elixir career and taught it to my fingers.

6 Likes

If Elixir ever gets a magazine it should be called ctrl-GQ.

I’ll see myself out.

6 Likes

Nice, but I don’t see the relevance to the discussion

Hah, never knew about that combo, thx