Skip to content

Commit 17d9408

Browse files
authored
fix(runtime): delete owned ETS tables before exit signals
Native process exits delete owned ETS tables before delivering linked EXIT or monitored DOWN signals. The same ordering covers linked-exit cascades, allowing immediate named-table recreation.
1 parent 560a2e9 commit 17d9408

5 files changed

Lines changed: 246 additions & 11 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
All notable milestones for the nera project are documented in this file.
44

5+
## Process exits release ETS tables before exit signals
6+
7+
- A dead process's ETS tables are deleted before linked processes receive
8+
`EXIT` or monitoring processes receive `DOWN`, as on BEAM. This also covers
9+
processes killed by a linked process's exit.
10+
- A child restarted by its supervisor can immediately create the same named
11+
table without racing the previous owner's cleanup.
12+
- The `ets_freed_before_signal` regression checks five exit paths and repeated
13+
supervised restarts, unchanged natively and on BEAM.
14+
515
## Stored bitstrings keep their bytes and exact bit length
616

717
- ETS and `persistent_term` now own copies of non-byte-aligned bitstrings,

‎crates/nera-runtime/src/ets.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3701,9 +3701,9 @@ mod tests {
37013701
}
37023702

37033703
// Regression: a process exit releases the names of the named tables it
3704-
// owned, and only those. The Erlang fixture ets_named_table_return cannot
3705-
// check this deterministically: natively the monitor message is sent
3706-
// before the exiting process's tables are deleted.
3704+
// owned, and only those. The scheduler's exit_deletes_ets_before_any_signal
3705+
// test owns the ordering relative to exit signals; this test owns isolation
3706+
// between named and unnamed tables belonging to different processes.
37073707
#[test]
37083708
fn owner_exit_releases_only_its_named_table() {
37093709
let name = global_atom_table().intern("test_owner_exit_named");

‎crates/nera-runtime/src/scheduler.rs‎

Lines changed: 106 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -636,16 +636,17 @@ impl Scheduler {
636636
// EXIT / DOWN signal propagation
637637
// ---------------------------------------------------------------------------
638638

639-
/// Free a dead process's registered name, then propagate EXIT signals to
639+
/// Free a dead process's registered name and ETS tables, then propagate EXIT signals to
640640
/// linked processes and DOWN signals to monitoring processes. Called from the
641641
/// exit points in `worker_loop` AFTER dropping the dying process's MutexGuard
642642
/// but BEFORE removing it from the process table (so its heap stays alive for
643643
/// deep-copy).
644644
///
645-
/// The name is freed first, as on BEAM: a process woken by the 'EXIT' or
646-
/// 'DOWN' on another worker, such as a supervisor restarting a named child,
647-
/// must find the name free. A process killed by the link cascade below frees
648-
/// its own name the same way before its signals go out.
645+
/// The name and tables are freed first, as on BEAM: a process woken by the
646+
/// 'EXIT' or 'DOWN' on another worker, such as a supervisor restarting a child,
647+
/// must be able to reuse its registered name and named tables. A process killed
648+
/// by the link cascade below releases its own resources the same way before
649+
/// its signals go out.
649650
///
650651
/// Deadlock safety: the dying process's lock is NOT held. Each linked/monitoring
651652
/// process is locked individually, and the lock is dropped before any recursive
@@ -675,6 +676,7 @@ fn propagate_exit_signals(
675676
};
676677

677678
unregister_dead_process(dead_pid, registered_names);
679+
crate::ets::delete_tables_owned_by(dead_pid);
678680

679681
let is_kill = exit_reason.as_atom_id() == Some(ATOM_KILL);
680682
let is_normal = exit_reason.as_atom_id() == Some(ATOM_NORMAL);
@@ -798,7 +800,6 @@ fn propagate_exit_signals(
798800
#[cfg(mio_supported)]
799801
reactor.cancel_for_pid(their_pid);
800802
crate::socket::remove_all_for_pid(their_pid);
801-
crate::ets::delete_tables_owned_by(their_pid);
802803
alive_count.fetch_sub(1, Ordering::AcqRel);
803804
process_table.remove(&their_pid);
804805
}
@@ -922,7 +923,6 @@ fn cleanup_dead_process(
922923
#[cfg(mio_supported)]
923924
reactor.cancel_for_pid(pid);
924925
crate::socket::remove_all_for_pid(pid);
925-
crate::ets::delete_tables_owned_by(pid);
926926
alive_count.fetch_sub(1, Ordering::AcqRel);
927927
process_table.remove(&pid);
928928
}
@@ -1636,7 +1636,6 @@ fn worker_loop(
16361636
#[cfg(mio_supported)]
16371637
reactor.cancel_for_pid(pid);
16381638
crate::socket::remove_all_for_pid(pid);
1639-
crate::ets::delete_tables_owned_by(pid);
16401639
alive_count.fetch_sub(1, Ordering::AcqRel);
16411640
process_table.remove(&pid);
16421641
} else {
@@ -2066,6 +2065,105 @@ mod tests {
20662065
assert_eq!(owner.inbox.len(), 1, "the owner then gets the linked process's 'DOWN'");
20672066
}
20682067

2068+
// A supervisor may recreate a named table as soon as EXIT/DOWN arrives.
2069+
// Hold the observer's lock so propagation cannot deliver its first DOWN.
2070+
// Both direct exit and a linked-exit cascade must already have deleted
2071+
// their named and unnamed tables, without touching another owner's table.
2072+
#[test]
2073+
fn exit_deletes_ets_before_any_signal() {
2074+
let mut outcomes = Vec::new();
2075+
for cascade in [false, true] {
2076+
let scheduler = Scheduler::new(1);
2077+
// Dedicated PIDs avoid the process-global ETS fixtures in other tests.
2078+
let dead_pid = ProcessId::new(9_400_000 + u64::from(cascade) * 10);
2079+
let linked_pid = ProcessId::new(dead_pid.get() + 1);
2080+
scheduler.spawn_process(Process::new(dead_pid));
2081+
scheduler.process_table.get(&dead_pid).unwrap().lock_for_runtime().transition(ProcessState::Exited);
2082+
let observer_pid = scheduler.spawn_process(Process::new(ProcessId::new(dead_pid.get() + 2)));
2083+
let observer = scheduler.process_table.get(&observer_pid).unwrap().clone();
2084+
let monitored_pid = if cascade { linked_pid } else { dead_pid };
2085+
let monitor = 1_u64;
2086+
observer.lock_for_runtime().add_outgoing_monitor(monitor, monitored_pid);
2087+
let monitors = [(monitor, observer_pid)].into_iter().collect();
2088+
let (links, monitors) = if cascade {
2089+
scheduler.spawn_process(Process::new(linked_pid));
2090+
let linked = scheduler.process_table.get(&linked_pid).unwrap().clone();
2091+
let mut proc = linked.lock_for_runtime();
2092+
proc.add_link(dead_pid);
2093+
proc.otp.monitors_incoming = monitors;
2094+
([linked_pid].into_iter().collect(), HashMap::new())
2095+
} else {
2096+
(HashSet::new(), monitors)
2097+
};
2098+
let atoms = crate::atom_table::global_atom_table();
2099+
let named = [("named_table".to_string(), ErlTerm::nil())];
2100+
let mut owned_tables = Vec::new();
2101+
for pid in [dead_pid, monitored_pid].into_iter().collect::<HashSet<_>>() {
2102+
let name = atoms.intern(&format!("exit_order_ets_{}", pid.get()));
2103+
owned_tables.push(crate::ets::ets_new_handle(name, &named, pid).unwrap());
2104+
owned_tables.push(crate::ets::ets_new_handle(name, &[], pid).unwrap());
2105+
}
2106+
let survivor_name = atoms.intern(&format!("exit_order_survivor_{}", dead_pid.get()));
2107+
let survivor = crate::ets::ets_new_handle(survivor_name, &named, observer_pid).unwrap();
2108+
#[cfg(mio_supported)]
2109+
let (reactor, join) = crate::reactor::spawn_reactor();
2110+
2111+
let guard = observer.lock_for_runtime();
2112+
let mut heap = crate::heap::ProcessHeap::new();
2113+
let undefined = ErlTerm::atom(crate::atom_table::ATOM_UNDEFINED);
2114+
let removed_before_down = thread::scope(|scope| {
2115+
let exiting = scope.spawn(|| {
2116+
cleanup_dead_process(
2117+
dead_pid,
2118+
ErlTerm::atom(crate::atom_table::ATOM_BADARG),
2119+
links,
2120+
monitors,
2121+
&scheduler.process_table,
2122+
&scheduler.injector,
2123+
&scheduler.runnable_count,
2124+
&scheduler.alive_count,
2125+
&scheduler.any_crash,
2126+
&scheduler.initial_pid,
2127+
&scheduler.registered_names,
2128+
&scheduler.timer_wheel,
2129+
&scheduler.io_pool,
2130+
#[cfg(mio_supported)]
2131+
&reactor,
2132+
);
2133+
});
2134+
let deadline = Instant::now() + Duration::from_secs(5);
2135+
let removed = loop {
2136+
if owned_tables.iter().all(|&table| {
2137+
crate::ets::ets_info(table, Some("owner"), &mut heap) == undefined
2138+
}) {
2139+
break true;
2140+
}
2141+
if Instant::now() >= deadline { break false; }
2142+
thread::yield_now();
2143+
};
2144+
let undelivered = observer.inbox.is_empty();
2145+
drop(guard);
2146+
exiting.join().unwrap();
2147+
removed && undelivered
2148+
});
2149+
#[cfg(mio_supported)]
2150+
{ reactor.shutdown(); join.join().unwrap(); }
2151+
2152+
assert_eq!(observer.inbox.len(), 1, "the observer then gets DOWN");
2153+
assert_eq!(crate::ets::ets_info(survivor, Some("owner"), &mut heap), ErlTerm::pid(observer_pid));
2154+
assert!(crate::ets::ets_delete_table(survivor));
2155+
// Recreate each name for the observer, not the exited owner.
2156+
for table in owned_tables.into_iter().filter(|table| table.is_atom()) {
2157+
let name = table.as_atom_id().unwrap();
2158+
assert_eq!(crate::ets::ets_new_handle(name, &named, observer_pid), Some(table));
2159+
assert!(crate::ets::ets_delete_table(table));
2160+
}
2161+
outcomes.push((cascade, removed_before_down));
2162+
}
2163+
assert!(outcomes.iter().all(|(_, removed)| *removed),
2164+
"direct and cascading exits delete ETS before DOWN: {outcomes:?}");
2165+
}
2166+
20692167
// supervisor_shutdown sometimes found a stopped child still alive: the
20702168
// supervisor got the child's 'DOWN' and exited, and the test got the
20712169
// supervisor's 'DOWN' and called is_process_alive/1 on the child, all

‎tests/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ These are automatically skipped by the runner and show up as SKIP in output.
462462
|------------------|---------------------------------------|------------|
463463
| `ets_basic.erl` | ETS new/insert/lookup/delete/tab2list | ~15 |
464464
| `ets_keypos.erl` | ETS keypos and named tables | ~8 |
465+
| `ets_freed_before_signal.erl` | A dead owner's named table can be recreated at the first EXIT or DOWN: normal exit, own reason, trapped link, kill and link cascade, 500 rounds each; a supervised gen_server creates it on each of 100 restarts; also run on BEAM | 5 exit paths + supervisor |
465466
| `ets_stored_fun.erl` | A fun stored in ETS (at the top of a row, nested in a list, tuple and map, after `update_element/3`) or `persistent_term` by a process that then exits or collects garbage keeps its captured list, binaries, tuple, map, float, bignum and inner fun when looked up and called; a fun read by `tab2list/1` or `persistent_term:get/1` still works after the table or term is deleted; completion marker; also run on BEAM | ~16 |
466467
| `ets_stored_bitstring.erl` | Non-byte-aligned heap/refc bitstrings, sub-byte values and extracted tails stored in ETS or `persistent_term` survive writer exit/GC; boxed `update_element/3` replacements and copying reads remain intact after table deletion or term erasure; completion marker; same fixture on BEAM | 4 lifetime scenarios |
467468
| `ets_bitstring_keys.erl` | Partial-bitstring bag duplicate suppression; copied direct/list/tuple keys in hash-backed tables for lookup, membership, insert_new, boxed updates, counters, deletion and forward/backward traversal; copied values as persistent_term keys; significant bits and exact lengths remain distinct; completion marker; same fixture on BEAM | Table-driven |

‎tests/ets_freed_before_signal.erl‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
%% EXPECT: ets_freed_before_signal_ok
2+
%% A dead owner's tables must be gone before its first EXIT or DOWN, as on
3+
%% BEAM. Recreate the named table immediately, including in a supervised
4+
%% gen_server's init. Extra monitors expose cleanup delayed until after signals.
5+
-module(ets_freed_before_signal).
6+
-export([test/0, init/1, handle_call/3, handle_cast/2]).
7+
8+
-define(TABLE, freed_ets_table).
9+
-define(CHILD, freed_ets_child).
10+
-define(ROUNDS, 500).
11+
-define(MONITORS, 32).
12+
-define(RESTARTS, 100).
13+
14+
test() ->
15+
process_flag(trap_exit, true),
16+
lists:foreach(fun(Scenario) -> rounds(Scenario, ?ROUNDS) end,
17+
[monitor_normal, monitor_crash, link_exit, link_killed,
18+
link_cascade]),
19+
ok = supervisor_restarts(),
20+
io:format("ets_freed_before_signal_ok~n"),
21+
ok.
22+
23+
rounds(_Scenario, 0) -> ok;
24+
rounds(Scenario, N) ->
25+
{Pid, Start, Signal, Reason} = setup(Scenario),
26+
Refs = [monitor(process, Pid) || _ <- lists:seq(1, ?MONITORS)],
27+
Start(),
28+
receive_signal(Signal, Pid, Reason),
29+
%% This must succeed on the first attempt, without waiting for cleanup.
30+
?TABLE = ets:new(?TABLE, [named_table, public]),
31+
Self = self(),
32+
Self = ets:info(?TABLE, owner),
33+
[] = ets:lookup(?TABLE, old_owner),
34+
true = ets:insert(?TABLE, {replacement, N}),
35+
[{replacement, N}] = ets:lookup(?TABLE, replacement),
36+
true = ets:delete(?TABLE),
37+
Pending = case Signal of down -> length(Refs) - 1; exit -> length(Refs) end,
38+
flush_downs(Pid, Pending),
39+
rounds(Scenario, N - 1).
40+
41+
setup(monitor_normal) ->
42+
Pid = owner(spawn, fun() -> ok end),
43+
{Pid, fun() -> Pid ! go end, down, normal};
44+
setup(monitor_crash) ->
45+
Pid = owner(spawn, fun() -> exit(crash) end),
46+
{Pid, fun() -> Pid ! go end, down, crash};
47+
setup(link_exit) ->
48+
Pid = owner(spawn_link, fun() -> exit(crash) end),
49+
{Pid, fun() -> Pid ! go end, exit, crash};
50+
setup(link_killed) ->
51+
Pid = owner(spawn_link, fun() -> ok end),
52+
{Pid, fun() -> exit(Pid, kill) end, exit, killed};
53+
setup(link_cascade) ->
54+
Parent = self(),
55+
Leader = spawn(fun() -> receive go -> exit(crash) end end),
56+
Pid = spawn(fun() ->
57+
link(Leader),
58+
create_table(),
59+
Parent ! {ready, self()},
60+
receive never -> ok end
61+
end),
62+
receive {ready, Pid} -> ok end,
63+
{Pid, fun() -> Leader ! go end, down, crash}.
64+
65+
owner(Spawn, Exit) ->
66+
Parent = self(),
67+
Body = fun() ->
68+
create_table(),
69+
Parent ! {ready, self()},
70+
receive go -> Exit() end
71+
end,
72+
Pid = case Spawn of spawn -> spawn(Body); spawn_link -> spawn_link(Body) end,
73+
receive {ready, Pid} -> ok end,
74+
Pid.
75+
76+
create_table() ->
77+
?TABLE = ets:new(?TABLE, [named_table, public]),
78+
true = ets:insert(?TABLE, {old_owner, self()}),
79+
ok.
80+
81+
receive_signal(down, Pid, Reason) ->
82+
receive {'DOWN', _, process, Pid, Reason} -> ok end;
83+
receive_signal(exit, Pid, Reason) ->
84+
receive {'EXIT', Pid, Reason} -> ok end.
85+
86+
flush_downs(_Pid, 0) -> ok;
87+
flush_downs(Pid, N) ->
88+
receive {'DOWN', _, process, Pid, _} -> flush_downs(Pid, N - 1) end.
89+
90+
%% The restart limit permits exactly the requested restarts. A failed init
91+
%% (badarg from ets:new) consumes another attempt and stops the supervisor.
92+
supervisor_restarts() ->
93+
{ok, Sup} = supervisor:start_link(?MODULE, {sup, self()}),
94+
Child0 = receive {started, Initial} -> Initial end,
95+
ok = kill_and_restart(Child0, ?RESTARTS),
96+
Child = whereis(?CHILD),
97+
Ref = monitor(process, Child),
98+
exit(Sup, kill),
99+
receive {'EXIT', Sup, killed} -> ok end,
100+
receive {'DOWN', Ref, process, Child, killed} -> ok end,
101+
undefined = ets:info(?TABLE, owner),
102+
ok.
103+
104+
kill_and_restart(_Child, 0) -> ok;
105+
kill_and_restart(Child, N) ->
106+
exit(Child, kill),
107+
New = receive
108+
{started, Next} when Next =/= Child -> Next;
109+
{'EXIT', Sup, Reason} -> exit({supervisor_stopped, Sup, Reason})
110+
end,
111+
New = ets:info(?TABLE, owner),
112+
ok = gen_server:call(New, ready),
113+
kill_and_restart(New, N - 1).
114+
115+
init({sup, Parent}) ->
116+
{ok, {{one_for_one, ?RESTARTS, 3600},
117+
[{?CHILD, {gen_server, start_link,
118+
[{local, ?CHILD}, ?MODULE, {child, Parent}, []]},
119+
permanent, brutal_kill, worker, [?MODULE]}]}};
120+
init({child, Parent}) ->
121+
create_table(),
122+
Parent ! {started, self()},
123+
{ok, child}.
124+
125+
handle_call(_Request, _From, State) -> {reply, ok, State}.
126+
handle_cast(_Request, State) -> {noreply, State}.

0 commit comments

Comments
 (0)