diff --git a/CHANGELOG.md b/CHANGELOG.md index 766145a..aa8153e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ All notable milestones for the nera project are documented in this file. +## Process startup deadlines + +- `proc_lib:start_link/5` now kills an unacknowledged child when its finite + timeout expires and returns `{error, timeout}`, including to callers that + do not trap exits. Zero polls immediately; `infinity` remains unbounded. +- Timeout cleanup waits for child termination and removes only that child's + startup monitor and exit signals, preserving unrelated mailbox messages. + ## TCP receive deadlines - `gen_tcp:recv/3` now returns `{error, timeout}` when its finite timeout diff --git a/COMPATIBILITY.md b/COMPATIBILITY.md index 677c172..3c22f37 100644 --- a/COMPATIBILITY.md +++ b/COMPATIBILITY.md @@ -73,7 +73,41 @@ rm -rf "$beam_dir" The fixture must reach `proc_lib_init_crash_ok`; the native test runner checks this completion marker, and BEAM prints it. This is a scoped regression, not a successful Ranch or Cowboy startup: neither integration was rerun for this change. -`start_link/5` still ignores finite timeouts and spawn options. +`start_link/5` now honors finite timeouts; spawn options remain ignored. +The timeout regression below covers that separate correction. + +## proc_lib startup timeout regression + +`tests/proc_lib_start_timeout.erl` checks finite and zero startup timeouts, +child termination even when it traps exits, caller survival in both exit modes, +unrelated mailbox messages and startup-signal cleanup. It also checks finite +and infinite successful acknowledgement with the link preserved, and a child +crashing before a finite deadline. Its required completion marker is +`proc_lib_start_timeout_ok`. + +Reproduce with a freshly built runtime and compiler: + +```sh +./target/release/nera test proc_lib_start_timeout -j 1 --timeout 30 +beam_dir=$(mktemp -d) +erlc -o "$beam_dir" tests/proc_lib_start_timeout.erl +erl -noshell -pa "$beam_dir" -eval 'ok = proc_lib_start_timeout:test(), halt().' +rm -rf "$beam_dir" +``` + +On Linux x86_64 with Rust 1.100.0 and OTP 27, the repaired implementation +passes ten consecutive runs with both default native schedulers and +`NERA_SCHEDULERS=1`; the same fixture passes ten times on BEAM. Nera's own +library also passes ten times on BEAM when renamed to avoid replacing OTP's +core module. Each run reaches the completion marker and confirms the child is +dead before startup returns. + +The original implementation fails this fixture's bounded startup watchdog +natively and as a renamed library on BEAM. Native execution reports +`start_link_still_waiting` before the runner's outer timeout. OTP 27 returns +`{error, timeout}` and terminates the unacknowledged child. +This fixture does not establish Ranch/Cowboy integration, other native +platforms or spawn-option support. ## Cowboy analysis — 2026-10-20 diff --git a/FEATURES.md b/FEATURES.md index 731f3ca..0c07d9e 100644 --- a/FEATURES.md +++ b/FEATURES.md @@ -706,7 +706,7 @@ optimization as counters — on par with BEAM performance. | gen_server | ✅ | start_link, call, cast, info, reply; `{error, Reason}` when init/1 fails, `ignore` when it ignores; stops with its parent when it traps exits | | supervisor (one_for_one) | ✅ | start_link, child specs, restart logic with retries; OTP start results: `{error, Reason}` when init/1 fails, `failed_to_start_child`, `ignore`; stops with its parent and stops its children in reverse start order by their shutdown setting | | application | ✅ | start/stop, app_name → app_name_app | -| proc_lib | ✅ | spawn, spawn_link, init_ack; start_link/3,5 returns `{error, Reason}` to trapping callers when the child dies before acknowledgement; finite timeouts and spawn options ignored | +| proc_lib | ✅ | spawn, spawn_link, init_ack; start_link/3,5 returns `{error, Reason}` to trapping callers when the child dies before acknowledgement; finite timeouts kill unacknowledged children and return `{error, timeout}`; spawn options ignored | | error_logger | ✅ | info/error/warning with colored output | | gen_event | ❌ | | | gen_statem | ❌ | | diff --git a/docs/KNOWN_LIMITS.md b/docs/KNOWN_LIMITS.md index 7c82743..19040a9 100644 --- a/docs/KNOWN_LIMITS.md +++ b/docs/KNOWN_LIMITS.md @@ -73,7 +73,7 @@ platform and scope. `{start_spec, {invalid_child_spec, Spec}}`. `start_child/2`, `terminate_child/2` and `which_children/1` are missing. - **`proc_lib`:** `start_link/3,5` reports a child dying before `init_ack`, - but `start_link/5` still ignores its timeout and spawn-option arguments. + and `start_link/5` honors finite timeouts, but still ignores spawn options. - **`application`:** `get_env/1` is missing. - **ETS:** `ets:match/2` and `ets:select/2` are missing. `ets:new/2` without `named_table` returns an integer where BEAM returns a reference. diff --git a/erlang/stdlib/proc_lib.erl b/erlang/stdlib/proc_lib.erl index 9da37bb..7cf15b3 100644 --- a/erlang/stdlib/proc_lib.erl +++ b/erlang/stdlib/proc_lib.erl @@ -91,8 +91,8 @@ init_ack(Parent, Return) -> start_link(Mod, Fun, Args) -> start_link(Mod, Fun, Args, infinity, []). -%% start_link/5 — timeout and spawn options are not yet supported. -start_link(Mod, Fun, Args, _Timeout, _Opts) -> +%% start_link/5 — spawn options are not yet supported. +start_link(Mod, Fun, Args, Timeout, _Opts) -> Parent = self(), %% Monitor from creation: the child can die before the caller runs again. {Pid, MRef} = erlang:spawn_opt(fun() -> @@ -109,6 +109,14 @@ start_link(Mod, Fun, Args, _Timeout, _Opts) -> erlang:unlink(Pid), receive {'EXIT', Pid, _} -> ok after 0 -> ok end, {error, Reason} + after Timeout -> + %% Unlink before killing so a non-trapping caller survives the timeout. + %% Await DOWN even for a trapping child: return only after it is dead. + erlang:unlink(Pid), + erlang:exit(Pid, kill), + receive {'DOWN', MRef, process, Pid, _} -> ok end, + receive {'EXIT', Pid, _} -> ok after 0 -> ok end, + {error, timeout} end. start_init(Parent, Mod, Fun, Args) -> diff --git a/tests/README.md b/tests/README.md index 94f810d..3e17984 100644 --- a/tests/README.md +++ b/tests/README.md @@ -501,6 +501,7 @@ These are automatically skipped by the runner and show up as SKIP in output. |--------------------------|----------------------------------------|------------| | `gen_server_basic.erl` | gen_server start_link/call/cast/info | ~12 | | `supervisor_basic.erl` | Supervisor one_for_one restart | ~8 | +| `proc_lib_start_timeout.erl` | Finite/zero proc_lib:start_link/5 timeouts, child termination despite trap_exit, caller survival, unrelated mailbox preservation and startup signal cleanup; finite/infinite acknowledgement and finite pre-ack crash; same fixture on BEAM | Real processes | | `proc_lib_init_crash.erl` | proc_lib:start_link/3,5: error/exit/throw before init_ack returns BEAM's `{error, Reason}` to trapping callers and exits non-trapping linked callers; normal pre-ack termination, no startup EXIT/DOWN residue, unrelated messages preserved, both init_ack forms, metadata and post-ack links; same fixture on BEAM | Table-driven | | `gen_server_init_crash.erl` | gen_server:start_link/3,4, start/3 and supervisor:start_link/2,3 when init/1 raises, exits, throws, returns `{error, Reason}` or a bad value, when supervisor flags are invalid and when the name is taken: `{error, Reason}` for a caller trapping exits, no leftover `'EXIT'`/`'DOWN'`, name freed, a linked caller that does not trap exits exits with Reason; init/1 returning or throwing `ignore` gives `ignore`; also run on BEAM | 64 | | `supervisor_child_start.erl` | supervisor:start_link/2,3 when a child's start returns `{error, Reason}`, a value that is not a start result, raises, exits, throws or is a gen_server whose init/1 crashes: `{error, {shutdown, {failed_to_start_child, Id, Reason}}}` with OTP's Reason, later children not started, earlier ones stopped, name freed, no leftover `'EXIT'`/`'DOWN'`, a linked caller that does not trap exits exits with it; a child returning or throwing `ignore` and a child supervisor returning `ignore` do not fail the start, and the children after them start; a failed restart tried again and counted against the restart limit, then `shutdown`; a restart returning `ignore` keeps the supervisor running; also run on BEAM | 48 | diff --git a/tests/proc_lib_start_timeout.erl b/tests/proc_lib_start_timeout.erl new file mode 100644 index 0000000..3499e20 --- /dev/null +++ b/tests/proc_lib_start_timeout.erl @@ -0,0 +1,86 @@ +%% EXPECT: proc_lib_start_timeout_ok +%% Intent: finite proc_lib:start_link/5 timeouts must stop an unacknowledged +%% child and return {error, timeout}, as BEAM does, without killing the caller. +-module(proc_lib_start_timeout). +-export([test/0, init/2]). + +test() -> + lists:foreach(fun(Trap) -> + in_caller(Trap, fun() -> + self() ! unrelated, + OtherRef = make_ref(), + self() ! {'DOWN', OtherRef, process, self(), unrelated}, + self() ! {'EXIT', self(), unrelated}, + {error, timeout} = proc_lib:start_link(?MODULE, init, [self(), wait], 250, []), + receive {child, Child} -> + false = is_process_alive(Child) + after 5000 -> error(child_never_started) + end, + receive unrelated -> ok after 0 -> error(lost_message) end, + receive {'DOWN', OtherRef, process, _, unrelated} -> ok + after 0 -> error(lost_unrelated_down) + end, + receive {'EXIT', _, unrelated} -> ok + after 0 -> error(lost_unrelated_exit) + end, + no_messages(), + io:format("timeout trap=~p: returned error, child dead, mailbox preserved~n", [Trap]) + end), + in_caller(Trap, fun() -> + {error, timeout} = proc_lib:start_link(?MODULE, init, [self(), silent], 0, []), + no_messages(), + io:format("zero timeout trap=~p: caller survived, no startup signals~n", [Trap]) + end) + end, [true, false]), + lists:foreach(fun(Timeout) -> + in_caller(true, fun() -> + {ok, Child} = proc_lib:start_link(?MODULE, init, [self(), ack], Timeout, []), + true = is_process_alive(Child), + Child ! stop, + receive {'EXIT', Child, normal} -> ok + after 5000 -> error(link_not_preserved) + end, + no_messages(), + io:format("ack timeout=~p: returned ok, link preserved~n", [Timeout]) + end) + end, [5000, infinity]), + in_caller(true, fun() -> + {error, before_ack} = proc_lib:start_link(?MODULE, init, [self(), crash], 5000, []), + no_messages() + end), + io:format("proc_lib_start_timeout_ok~n"), + ok. + +init(Parent, wait) -> + process_flag(trap_exit, true), + Parent ! {child, self()}, + receive never -> ok end; +init(_Parent, silent) -> + receive never -> ok end; +init(_Parent, crash) -> exit(before_ack); +init(_Parent, ack) -> + proc_lib:init_ack({ok, self()}), + receive stop -> ok end. + +in_caller(Trap, Fun) -> + Parent = self(), + {Pid, Ref} = spawn_monitor(fun() -> + process_flag(trap_exit, Trap), + Fun(), + Parent ! {completed, self()} + end), + receive + {'DOWN', Ref, process, Pid, Reason} -> + normal = Reason, + receive {completed, Pid} -> ok + after 0 -> error(caller_did_not_complete) + end + after 10000 -> + exit(Pid, kill), + error(start_link_still_waiting) + end. + +no_messages() -> + receive Msg -> error({unexpected_message, Msg}) + after 0 -> ok + end.