diff --git a/lib/elixir/lib/kernel/cli.ex b/lib/elixir/lib/kernel/cli.ex index f1a67e14e6b..08e18c462eb 100644 --- a/lib/elixir/lib/kernel/cli.ex +++ b/lib/elixir/lib/kernel/cli.ex @@ -451,6 +451,7 @@ defmodule Kernel.CLI do :exit, {kind, exit} when kind in [:exception, :signal] -> Process.exit(self(), exit) + Process.sleep(:infinity) else :ok -> :ok {kind, reason, stack} -> :erlang.raise(kind, reason, stack) diff --git a/lib/elixir/lib/process.ex b/lib/elixir/lib/process.ex index ac6b158f636..4242aab6807 100644 --- a/lib/elixir/lib/process.ex +++ b/lib/elixir/lib/process.ex @@ -205,10 +205,10 @@ defmodule Process do Exit behavior differs based on the value of `reason`: - - If `:normal`, `pid` will not exit unless it is the calling process, in - which case it will exit with the reason `:normal`. If it is trapping exits, - the exit signal is transformed into a message `{:EXIT, from, :normal}` and - delivered to its message queue. + - If `:normal`, `pid` will not exit. If it is trapping exits, the exit + signal is transformed into a message `{:EXIT, from, :normal}` and + delivered to its message queue. This also applies when `pid` is the + calling process. - If `:kill`, which occurs when `Process.exit(pid, :kill)` is called, an untrappable exit signal is sent to `pid` which will unconditionally exit @@ -221,6 +221,13 @@ defmodule Process do transformed into a message `{:EXIT, from, reason}` and delivered to its message queue. + The exit signal is always sent asynchronously, even when `pid` is the + calling process. This means `Process.exit(self(), reason)` returns and + the calling process may continue executing before the signal is handled. + Prior to Elixir v1.21, the calling process would exit before this function + returned whenever the signal terminated it, and `:normal` would terminate + it when not trapping exits. Use `Kernel.exit/1` to stop the current process. + Inlined by the compiler. > #### Differences to `Kernel.exit/1` {: .info } @@ -240,7 +247,7 @@ defmodule Process do """ @spec exit(pid, term) :: true - defdelegate exit(pid, reason), to: :erlang + defdelegate exit(pid, reason), to: :erlang, as: :exit_signal @doc """ Sleeps the current process for the given `timeout`. diff --git a/lib/elixir/src/elixir_rewrite.erl b/lib/elixir/src/elixir_rewrite.erl index bfa2f3154cb..23e2d2dd9b3 100644 --- a/lib/elixir/src/elixir_rewrite.erl +++ b/lib/elixir/src/elixir_rewrite.erl @@ -189,7 +189,7 @@ inner_inline(ex_to_erl, ?list, to_atom, 1) -> {erlang, list_to_atom}; ?inline(?process, cancel_timer, 2, erlang, cancel_timer); ?inline(?process, demonitor, 1, erlang, demonitor); ?inline(?process, demonitor, 2, erlang, demonitor); -?inline(?process, exit, 2, erlang, exit); +?inline(?process, exit, 2, erlang, exit_signal); ?inline(?process, flag, 2, erlang, process_flag); ?inline(?process, flag, 3, erlang, process_flag); ?inline(?process, get, 0, erlang, get); diff --git a/lib/elixir/test/elixir/process_test.exs b/lib/elixir/test/elixir/process_test.exs index db60db627f0..7e52ea190f8 100644 --- a/lib/elixir/test/elixir/process_test.exs +++ b/lib/elixir/test/elixir/process_test.exs @@ -139,11 +139,16 @@ defmodule ProcessTest do refute Process.alive?(pid) end - test "exit(self(), :normal) causes the calling process to exit" do - Process.flag(:trap_exit, true) - pid = spawn_link(fn -> Process.exit(self(), :normal) end) - assert_receive {:EXIT, ^pid, :normal} - refute Process.alive?(pid) + test "exit(self(), :normal) does not cause the calling process to exit" do + parent = self() + + pid = + spawn(fn -> + Process.exit(self(), :normal) + send(parent, {self(), :alive}) + end) + + assert_receive {^pid, :alive} end describe "alias/0, alias/1, and unalias/1" do diff --git a/lib/ex_unit/lib/ex_unit/assertions.ex b/lib/ex_unit/lib/ex_unit/assertions.ex index 45add356a88..ed53d715814 100644 --- a/lib/ex_unit/lib/ex_unit/assertions.ex +++ b/lib/ex_unit/lib/ex_unit/assertions.ex @@ -952,7 +952,7 @@ defmodule ExUnit.Assertions do with `Process.flag/2` and assert the exit message with `assert_receive/2`. Process.flag(:trap_exit, true) - pid = spawn_link(fn -> Process.exit(self(), :normal) end) + pid = spawn_link(fn -> exit(:normal) end) assert_receive {:EXIT, ^pid, :normal} """ diff --git a/lib/ex_unit/test/ex_unit/callbacks_test.exs b/lib/ex_unit/test/ex_unit/callbacks_test.exs index 6b3b37eaa8b..a3939be30a8 100644 --- a/lib/ex_unit/test/ex_unit/callbacks_test.exs +++ b/lib/ex_unit/test/ex_unit/callbacks_test.exs @@ -183,7 +183,11 @@ defmodule ExUnit.CallbacksTest do use ExUnit.Case, async: false test "ok" do - on_exit(fn -> Process.exit(self(), :kill) end) + on_exit(fn -> + Process.exit(self(), :kill) + Process.sleep(:infinity) + end) + :ok end end