Skip to content

Commit 8cc6938

Browse files
committed
Improve docs for start_link on supervisor and genserver, closes #15232
1 parent 0f8baa3 commit 8cc6938

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

lib/elixir/lib/gen_server.ex

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,9 +567,12 @@ defmodule GenServer do
567567
`Supervisor`. Likely this approach involves calling `Supervisor.restart_child/2`
568568
after a delay to attempt a restart.
569569
570-
Returning `{:stop, reason}` will cause `start_link/3` to return
571-
`{:error, reason}` and the process to exit with reason `reason` without
572-
entering the loop or calling `c:terminate/2`.
570+
Returning `{:error, reason}` will cause `start_link/3` to return
571+
`{:error, reason}`.
572+
573+
Returning `{:stop, reason}` will the process to exit with reason `reason`,
574+
without entering the loop or calling `c:terminate/2`. `start_link/3` will
575+
return `{:error, reason}`, but only if the caller is trapping exits.
573576
"""
574577
@callback init(init_arg :: term) ::
575578
{:ok, state}

lib/elixir/lib/supervisor.ex

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -556,19 +556,29 @@ defmodule Supervisor do
556556
557557
Developers typically invoke `Supervisor.init/2` at the end of their
558558
init callback to return the proper supervision flags.
559+
560+
See `start_link/2` for more information.
559561
"""
560562
@callback init(init_arg :: term) ::
561563
{:ok,
562564
{sup_flags(), [child_spec() | (old_erlang_child_spec :: :supervisor.child_spec())]}}
563565
| :ignore
564566

565-
@typedoc "Return values of `start_link/2` and `start_link/3`."
567+
@typedoc """
568+
Return values of `start_link/2` and `start_link/3`.
569+
570+
See the documentation for those functions for more information.
571+
"""
566572
@type on_start ::
567573
{:ok, pid}
568574
| :ignore
569575
| {:error, {:already_started, pid} | {:shutdown, term} | term}
570576

571-
@typedoc "Return values of `start_child/2`."
577+
@typedoc """
578+
Return values of `start_child/2`.
579+
580+
See the documentation for those functions for more information.
581+
"""
572582
@type on_start_child ::
573583
{:ok, child}
574584
| {:ok, child, info :: term}
@@ -698,15 +708,18 @@ defmodule Supervisor do
698708
If the supervisor and all child processes are successfully spawned
699709
(if the start function of each child process returns `{:ok, child}`,
700710
`{:ok, child, info}`, or `:ignore`), this function returns
701-
`{:ok, pid}`, where `pid` is the PID of the supervisor. If the supervisor
702-
is given a name and a process with the specified name already exists,
703-
the function returns `{:error, {:already_started, pid}}`, where `pid`
704-
is the PID of that process.
711+
`{:ok, pid}`, where `pid` is the PID of the supervisor.
712+
713+
If the supervisor is given a name and a process with the specified name
714+
already exists, the function returns `{:error, {:already_started, pid}}`,
715+
where `pid` is the PID of that process.
705716
706-
If the start function of any of the child processes fails or returns an error
707-
tuple or an erroneous value, the supervisor first terminates with reason
708-
`:shutdown` all the child processes that have already been started, and then
709-
terminates itself and returns `{:error, {:shutdown, reason}}`.
717+
If the start function of any of the child processes fails or returns
718+
an error tuple or an erroneous value, the supervisor first terminates
719+
with reason `:shutdown` all the child processes that have already been
720+
started, and then terminates itself. If the caller process is trapping
721+
exits, this function then returns
722+
`{:error, {:shutdown, {:failed_to_start_child, id, reason}}}`.
710723
711724
Note that a supervisor started with this function is linked to the parent
712725
process and exits not only on crashes but also if the parent process exits
@@ -939,9 +952,17 @@ defmodule Supervisor do
939952
940953
If the `c:init/1` callback returns `:ignore`, this function returns
941954
`:ignore` as well and the supervisor terminates with reason `:normal`.
942-
If it fails or returns an incorrect value, this function returns
943-
`{:error, term}` where `term` is a term with information about the
944-
error, and the supervisor terminates with reason `term`.
955+
956+
If the `c:init/1` callback fails or returns an incorrect value, the supervisor
957+
will shutdown with reason `term`, where `term` contains information
958+
about the error. If the caller process is trapping exits, this function then
959+
returns `{:error, term()}`.
960+
961+
If the start function of any of the child processes returned by `c:init/1`
962+
fail or return an error tuple or an erroneous value, the supervisor first
963+
terminates with reason `:shutdown` all the child processes that have already
964+
been started, and then terminates itself. If the caller process is trapping
965+
exits, this function then returns `{:error, {:shutdown, {:failed_to_start_child, id, reason}}}`.
945966
946967
The `:name` option can also be given in order to register a supervisor
947968
name, the supported values are described in the "Name registration"

0 commit comments

Comments
 (0)