Skip to content

fix(stdlib): read values thrown by gen_server callbacks as returns - #106

Merged
mremond merged 1 commit into
mainfrom
infinitary-batch/de6ac741
Dec 3, 2026
Merged

mremond merged 1 commit into
mainfrom
infinitary-batch/de6ac741

Conversation

@mremond

@mremond mremond commented Dec 3, 2026

Copy link
Copy Markdown
Member

Why

Erlang programs build long-running servers on gen_server, which calls the program's own functions (callbacks) for each request; standard Erlang lets a callback hand back its answer with throw instead of return, but Nera's built-in gen_server crashed the server when a callback did that.

What changed

  • A server whose handle_call/3 throws its answer, such as {reply, Reply, State}, now answers the caller and keeps running, as in standard Erlang.
  • A server whose handle_cast/2 or handle_info/2 throws {noreply, State} keeps running with its new state, and one that throws {stop, ...} shuts down cleanly through its terminate/2 function.
  • A value thrown by terminate/2 no longer changes the reason the server gives for stopping.
  • gen_server:call/2,3 no longer leaves a stray 'DOWN' notice in the caller's mailbox when the server stops right after answering.
  • A real error in a callback still crashes the server, as before.

Before and after

Before: Natively, a server crashed whenever a callback threw its answer, so callers got a crash instead of the answer and later requests failed: 0 of 13 such cases in a per-case probe behaved as in standard Erlang (BEAM OTP 29).

After: All 13 cases now behave as in standard Erlang; the one difference left in the probe, terminate/2 not running after a real error, predates this change.

The change is in the built-in server library, which has no screen; native and BEAM transcripts show it.

Checked

  • New gen_server_thrown_return test, natively with the default scheduler count and with one scheduler, and on BEAM OTP 29: 10 of 10 passes in each setting
  • The new test against the previous gen_server: Fails at its first thrown answer; with only the stray-notice fix removed it fails on the leftover 'DOWN' in 40 of 40 runs
  • Per-case probe of thrown answers in call, cast, message and terminate callbacks: 13 of 13 cases match BEAM OTP 29, against 0 of 13 before
  • Full local check run: ordered release builds, clippy lint, Rust tests, allocator check, end-to-end suite, stack trace order: All passed: 2303 Rust tests, 558 end-to-end tests passed and 2 skipped for missing external modules
  • Independent review of the commit: No defects found; differences from standard Erlang that predate this change are tracked separately
How it was checked

nera test gen_server_thrown_return, 10 runs with default schedulers and 10 with NERA_SCHEDULERS=1

erlc tests/gen_server_thrown_return.erl and gen_server_thrown_return:test() on BEAM OTP 29, 10 runs

cargo build --release --locked -p nera-runtime, then -p nera, then the workspace

cargo clippy --workspace --all-targets --locked -- -D warnings

cargo test --workspace --locked --no-fail-fast

python3 scripts/test-allocator-config.py

./target/release/nera test -j 1 --timeout 30

python3 scripts/test-stacktrace-order.py

Risk

Risk above low: Blast radius, medium — Every gen_server request, message and stop now runs through the changed path

Full risk assessment
Dimension Level Why
Blast radius medium Every gen_server request, message and stop now runs through the changed path
Behavior low A thrown answer that crashed the server now works as in standard Erlang
Undo low Revert the commit; no saved data or settings change
Evidence low New test and per-case probe shown natively and on BEAM, plus the full suite
Sensitivity low No sign-ins, keys, saved data or releases involved
Novelty low Small try wrappers around existing callback calls, as standard Erlang has
Dependencies low No packages added or updated
Not done

Four older differences from standard Erlang remain and are tracked separately: terminate/2 after a real error, the bad_return_value reason, whether a stop answer comes before terminate/2, and call timeout reasons.

A server whose handle_call/3 throws its answer, such as {reply, Reply, State}, now answers the caller and keeps running, as in standard Erlang.

A server whose handle_cast/2 or handle_info/2 throws {noreply, State} keeps running with its new state, and one that throws {stop, ...} shuts down cleanly through its terminate/2 function.

A value thrown by terminate/2 no longer changes the reason the server gives for stopping.

gen_server:call/2,3 no longer leaves a stray 'DOWN' notice in the caller's mailbox when the server stops right after answering.

A real error in a callback still crashes the server, as before.
@mremond
mremond merged commit 7679bed into main Dec 3, 2026
21 checks passed
@mremond
mremond deleted the infinitary-batch/de6ac741 branch December 3, 2026 19:07
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.

1 participant