Skip to content

Fix: benign gen_server start race crash reports#2172

Open
petermm wants to merge 1 commit intoatomvm:mainfrom
petermm:fix-timer-manager-reports
Open

Fix: benign gen_server start race crash reports#2172
petermm wants to merge 1 commit intoatomvm:mainfrom
petermm:fix-timer-manager-reports

Conversation

@petermm
Copy link
Contributor

@petermm petermm commented Mar 8, 2026

Fixes #1001

This typically happens, due to timer_manager startup race for say:

:erlang.send_after(1, self(), :show_hello)
:erlang.send_after(5000, self(), :show_cat)

Or when starting up an Supervisor tree with lots of things going on:

=============
=============
CRASH REPORT
CRASH REPORT
gen_server:init_it/5: Error initializing module timer_manager under name timer_manager
gen_server:init_it/5: Error initializing module timer_manager under name timer_manager
Error: {already_started,<0.36.0>}
Error: {already_started,<0.36.0>}
Parent: <0.33.0>
Parent: <0.34.0>
Pid: <0.37.0>
Pid: <0.38.0>
Stacktrace: undefined
Stacktrace: undefined
=============
=============
=============
CRASH REPORT
gen_server:init_it/5: Error initializing module timer_manager under name timer_manager
Error: {already_started,<0.36.0>}
Parent: <0.35.0>
Pid: <0.39.0>
Stacktrace: undefined

And is very confusing! and can send one on wild goose hunts for what looks like a crash - but is normal behaviour.

I have test at hand that captures io output etc - but it seems extensive and perhaps flappy prone to have a 400 line test for this.

Avoid emitting a crash report in gen_server:init_it/5 when erlang:register/2 loses a race to another process and whereis(Name) returns an existing pid.

This keeps {error, {already_started, Pid}} behavior unchanged for callers, while preserving crash reports for genuine registration failures where the name is still undefined.

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

This typically happens, due to timer_manager startup race for say:
:erlang.send_after(1, self(), :show_hello)
:erlang.send_after(5000, self(), :show_cat)

Avoid emitting a crash report in gen_server:init_it/5 when erlang:register/2 loses a race to another process and whereis(Name) returns an existing pid.

This keeps {error, {already_started, Pid}} behavior unchanged for callers, while preserving crash reports for genuine registration failures where the name is still undefined.

Signed-off-by: Peter M <petermm@gmail.com>
@petermm petermm force-pushed the fix-timer-manager-reports branch from 0d8eff7 to 97da9c7 Compare March 8, 2026 17:10
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.

timer_manager init bug

1 participant