estdlib: add rest_for_one and simple_one_for_one supervisor strategies by pguyot · Pull Request #2352 · atomvm/AtomVM · GitHub
Skip to content

estdlib: add rest_for_one and simple_one_for_one supervisor strategies - #2352

Open
pguyot wants to merge 1 commit into
atomvm:release-0.7from
pguyot:w27/estdlib-supervisor-strategies
Open

pguyot wants to merge 1 commit into
atomvm:release-0.7from
pguyot:w27/estdlib-supervisor-strategies

Conversation

@pguyot

@pguyot pguyot commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later

@pguyot
pguyot force-pushed the w27/estdlib-supervisor-strategies branch from 8e83c85 to 63a942a Compare July 4, 2026 07:19
@petermm

petermm commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

@pguyot
pguyot force-pushed the w27/estdlib-supervisor-strategies branch 3 times, most recently from b5bdab9 to 68a7821 Compare August 1, 2026 06:29
@pguyot
pguyot force-pushed the w27/estdlib-supervisor-strategies branch from 68a7821 to 3e84e86 Compare September 25, 2026 15:41

@bettio bettio left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fd66292 and acefe17 replaced the {timeout, 0, Msg} restart driver with payload-free '$atomvm_*' messages sent to self(), plus restart_queue and retry_queue in the state. The new code in this PR still uses the old mechanism.

Comment thread libs/estdlib/src/supervisor.erl Outdated
ok = terminate_one_for_all(Affected1),
{ok, NewAffected} = get_restart_children(Affected1),
{noreply, State#state{children = lists:reverse(NewAffected) ++ Rest},
{timeout, 0, {restart_many_children, NewAffected}}}.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This conflicts with release-0.7 (fd66292 and acefe17).

On rebase, please switch this to the new mechanism, the same way one_for_all now does it: self() ! '$atomvm_restart_many_children' with restart_queue = restart_ids(NewAffected).

Otherwise this path brings back both problems: a call or 'EXIT' arriving mid-restart leaves the remaining children stuck in {restarting, _}, and the message carries #child{} records, so any sender can make the supervisor call an arbitrary MFA.

Comment thread libs/estdlib/src/supervisor.erl Outdated
{error, _} ->
NewChild = Child#child{pid = {restarting, Child#child.pid}},
Children = lists:keyreplace(Id, #child.id, State#state.children, NewChild),
{noreply, State#state{children = Children}, {timeout, 0, {try_again_restart, Id}}}

@bettio bettio Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

restart_one and restart_simple_one helpers return {timeout, 0, {try_again_restart, Id}}. That was the bug fixed in acefe17: the next incoming message cancels the retry, so the child stays {restarting, _} forever. Please port them to self() ! '$atomvm_try_again_restart' and append Id to retry_queue, the way the current one_for_one clause on release-0.7 does. The same applies at line 633.

case add_restart(State) of
{ok, State1} ->
case try_start(Child) of
{ok, undefined, _Result} when

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On release-0.7, the '$atomvm_try_again_restart' handler only matches #child{pid = {restarting, _}} and re-queues with retry_queue = Ids ++ [Id]. Please carry the {ok, undefined, _} when simple_one_for_one drop branch into that handler, so the case covered by test_simple_one_for_one_ignore_on_restart still works through the retry path.

Comment thread CHANGELOG.md Outdated
- Added a `hex_package` build target that builds the `atomvm` Hex package, published on
hex.pm by CI when a release is tagged, so that build tools can check a project against
the AtomVM release it targets
- Added support for `rest_for_one` and `simple_one_for_one` supervisor restart strategies

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this to 0.7.0-beta.1.

Comment thread tests/libs/estdlib/test_supervisor.erl Outdated
type => worker,
modules => [?MODULE]
},
{ok, {#{strategy => simple_one_for_one, intensity => 10, period => 60}, [Template]}};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every failed retry calls add_restart, and the retry loop runs back-to-back. With intensity 10, the supervisor can shut down before terminate_child/2 gets in, especially on SMP builds. Today the test likely passes partly because the call cancels the {timeout, 0} retry, and that changes after the rebase. Please raise the intensity (e.g. 1000), or make the worker fail a bounded number of times and then block.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

release-0.7 has test_restart_survives_messages_one_for_all and test_retry_survives_messages_one_for_one. A rest_for_one version, where a which_children call or an unrelated 'EXIT' arrives during a partial restart, would guard the rebased path above.

For simple_one_for_one, drop a dynamic child whose restart returns ignore and
let terminate_child/2 cancel a pending restart by the original pid, as OTP
does.

Signed-off-by: Paul Guyot <pguyot@kallisys.net>
@pguyot
pguyot force-pushed the w27/estdlib-supervisor-strategies branch from 3e84e86 to 35eafd7 Compare October 17, 2026 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants