Skip to content

DynamicSupervisor max_children is not actually the max number of children #7655

@sunboshan

Description

@sunboshan

$ elixir -v
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Elixir 1.6.5 (compiled with OTP 20)

Mac OS X 10.12.6

DynamicSupervisor use internal state dynamic to track how many children has been started.

  • The value increase by 1 when start_child/2 has been called. code
  • The value decreased by 1 when delete_child/2 has been called. code

However, if a permanent child crashed, the value is decreased, which will make the dynamic value less than the actually number of children(since the child got restarted, however the dynamic value has decreased by 1).

Repro:

iex(1)> {:ok,dsup} = DynamicSupervisor.start_link(strategy: :one_for_one, max_children: 1)
{:ok, #PID<0.135.0>}
iex(2)> {:ok,child}=DynamicSupervisor.start_child(dsup,Foo)
{:ok, #PID<0.137.0>}
iex(3)> DynamicSupervisor.start_child(dsup,Foo)
{:error, :max_children}
iex(4)> Process.exit(child,1)
true
iex(5)> DynamicSupervisor.start_child(dsup,Foo)
{:ok, #PID<0.142.0>}
iex(6)> :sys.get_state(dsup)
%DynamicSupervisor{
  ...
  children: %{
    #PID<0.140.0> => {{Foo, :start_link, [[]]}, :permanent, 5000, :worker,
     [Foo]},
    #PID<0.142.0> => {{Foo, :start_link, [[]]}, :permanent, 5000, :worker,
     [Foo]}
  },
  dynamic: 1,
  ...
}

As shown, there are 2 children even given max_children as 1 for our dynamic supervisor.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions