Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/elixir/lib/kernel/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 12 additions & 5 deletions lib/elixir/lib/process.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }
Expand All @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion lib/elixir/src/elixir_rewrite.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 10 additions & 5 deletions lib/elixir/test/elixir/process_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/ex_unit/lib/ex_unit/assertions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}

"""
Expand Down
6 changes: 5 additions & 1 deletion lib/ex_unit/test/ex_unit/callbacks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading