Skip to content

Commit c6dbf24

Browse files
authored
fix(runtime): report exited processes as no longer alive
Process liveness queries now respect published exit state before process table cleanup finishes. A deterministic regression covers exit notification arriving before table removal without weakening supervisor assertions.
1 parent 64b88b9 commit c6dbf24

4 files changed

Lines changed: 114 additions & 2 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+
## Exited processes are no longer reported alive after shutdown
6+
7+
- `is_process_alive/1` now returns `false` once a process publishes its exit,
8+
even if its worker has not yet removed the process table entry. Previously
9+
a supervisor could finish stopping its children and exit, yet an observer
10+
receiving its `DOWN` could briefly see a stopped child as still alive.
11+
- A deterministic runtime regression holds that ordering between exit-signal
12+
delivery and table removal. The existing `supervisor_shutdown` fixture
13+
continues to check that all children are stopped before shutdown completes.
14+
515
## Linked exits release a woken process's runnable slot once
616

717
- When a linked process exits and kills a process that was woken but has not

‎FEATURES.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ Maintainers with OTP 29.0.4 installed can verify the catalogue with
250250
| cancel_timer/1,2 | ✅ | Cancels send_after/start_timer; async/info boolean options; already sent messages remain |
251251
| binary_part/3 | ✅ | |
252252
| process_info/2 | ✅ | Item atom or list; see notes below |
253-
| is_process_alive/1 | ✅ | |
253+
| is_process_alive/1 | ✅ | Exited processes return `false` before their process table entry is removed. |
254254
| halt/0,1 | ✅ | Immediate termination |
255255
| make_tuple/2,3 | ✅ | |
256256
| atom_to_binary/1,2 | ✅ | /1: UTF-8; /2: latin1, unicode or utf8; latin1 rejects characters above 255 |

‎crates/nera-runtime/src/ffi/process.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1856,6 +1856,10 @@ pub unsafe extern "C" fn nera_rt_spawn_monitor(
18561856
}
18571857

18581858
/// erlang:is_process_alive/1 — check if a process is alive.
1859+
///
1860+
/// Exit signals can reach observers before the worker removes the process
1861+
/// table entry. Observe the published exit state, as link/1 and monitor/2 do,
1862+
/// so a process whose 'EXIT' or 'DOWN' has arrived is no longer alive.
18591863
#[no_mangle]
18601864
pub unsafe extern "C" fn nera_rt_is_process_alive(ctx: *mut ProcessContext, term: u64) -> u64 {
18611865
let pid_id = match ErlTerm(term).as_pid_id() {
@@ -1864,7 +1868,10 @@ pub unsafe extern "C" fn nera_rt_is_process_alive(ctx: *mut ProcessContext, term
18641868
};
18651869
with_sched_from_ctx(ctx, #[inline(always)] |scheduler| {
18661870
let alive = match scheduler {
1867-
Some(sched) => sched.process_table.contains_key(&pid_id),
1871+
Some(sched) => sched
1872+
.process_table
1873+
.get(&pid_id)
1874+
.is_some_and(|entry| entry.published_status() != crate::process::ASTATUS_EXITED),
18681875
None => false,
18691876
};
18701877
if alive {

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

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,6 +2064,101 @@ mod tests {
20642064
assert_eq!(owner.inbox.len(), 1, "the owner then gets the linked process's 'DOWN'");
20652065
}
20662066

2067+
// supervisor_shutdown sometimes found a stopped child still alive: the
2068+
// supervisor got the child's 'DOWN' and exited, and the test got the
2069+
// supervisor's 'DOWN' and called is_process_alive/1 on the child, all
2070+
// before the child's worker removed it from the process table. On BEAM
2071+
// an exiting process is not alive, so once its 'DOWN' is out the answer
2072+
// is false. This replays the child's exit up to that point: published,
2073+
// its 'DOWN' delivered, its table entry not yet removed.
2074+
#[test]
2075+
fn exiting_process_is_not_alive_once_its_down_is_out() {
2076+
let scheduler = Scheduler::new(1);
2077+
let supervisor_pid = scheduler.spawn_process(Process::new(scheduler.next_pid()));
2078+
let child_pid = scheduler.spawn_process(Process::new(scheduler.next_pid()));
2079+
let monitor = 1_u64;
2080+
let supervisor = scheduler.process_table.get(&supervisor_pid).unwrap().clone();
2081+
let child = scheduler.process_table.get(&child_pid).unwrap().clone();
2082+
supervisor
2083+
.lock_for_runtime()
2084+
.add_outgoing_monitor(monitor, child_pid);
2085+
child
2086+
.lock_for_runtime()
2087+
.add_incoming_monitor(monitor, supervisor_pid);
2088+
#[cfg(mio_supported)]
2089+
let (reactor, join) = crate::reactor::spawn_reactor();
2090+
let handle = SchedulerHandle {
2091+
injector: Arc::clone(&scheduler.injector),
2092+
process_table: Arc::clone(&scheduler.process_table),
2093+
next_pid: Arc::clone(&scheduler.next_pid),
2094+
runnable_count: Arc::clone(&scheduler.runnable_count),
2095+
alive_count: Arc::clone(&scheduler.alive_count),
2096+
registered_names: Arc::clone(&scheduler.registered_names),
2097+
timer_wheel: Arc::clone(&scheduler.timer_wheel),
2098+
worker_notify: Arc::clone(&scheduler.worker_notify),
2099+
io_pool: Arc::clone(&scheduler.io_pool),
2100+
#[cfg(mio_supported)]
2101+
reactor: reactor.clone(),
2102+
worker_inboxes: Arc::clone(&scheduler.worker_inboxes),
2103+
num_workers: 1,
2104+
worker_threads: Arc::new(Vec::new()),
2105+
dispatch_rr: AtomicU32::new(0),
2106+
};
2107+
// The test process asks, through the BIF, with its own context.
2108+
let mut observer = crate::process::ProcessContext::new(scheduler.next_pid());
2109+
observer.runtime = &handle;
2110+
let mut is_alive = |pid: ProcessId| {
2111+
// SAFETY: the context and the scheduler handle it points to
2112+
// outlive the call; a PID is an immediate term.
2113+
let answer = unsafe {
2114+
crate::ffi::process::nera_rt_is_process_alive(&mut observer, ErlTerm::pid(pid).0)
2115+
};
2116+
ErlTerm(answer).as_atom_id() == Some(crate::atom_table::ATOM_TRUE)
2117+
};
2118+
assert!(is_alive(child_pid), "a running child is alive");
2119+
2120+
// The child's worker publishes its exit and takes its monitors, as
2121+
// every exit path does, then sends its signals.
2122+
let monitors_in = {
2123+
let mut child = child.lock_for_runtime();
2124+
child.transition(ProcessState::Exited);
2125+
std::mem::take(&mut child.otp.monitors_incoming)
2126+
};
2127+
propagate_exit_signals(
2128+
child_pid,
2129+
ErlTerm::atom(crate::atom_table::global_atom_table().intern("shutdown")),
2130+
HashSet::new(),
2131+
monitors_in,
2132+
&scheduler.process_table,
2133+
&scheduler.injector,
2134+
&scheduler.runnable_count,
2135+
&scheduler.alive_count,
2136+
&scheduler.any_crash,
2137+
&scheduler.initial_pid,
2138+
&scheduler.registered_names,
2139+
&scheduler.timer_wheel,
2140+
&scheduler.io_pool,
2141+
#[cfg(mio_supported)]
2142+
&reactor,
2143+
);
2144+
#[cfg(mio_supported)]
2145+
{
2146+
reactor.shutdown();
2147+
join.join().unwrap();
2148+
}
2149+
assert_eq!(
2150+
supervisor.inbox.len(),
2151+
1,
2152+
"the supervisor has the child's 'DOWN'"
2153+
);
2154+
assert!(
2155+
scheduler.process_table.contains_key(&child_pid),
2156+
"the child's entry is not yet removed"
2157+
);
2158+
assert!(!is_alive(child_pid), "a child whose 'DOWN' is out is not alive");
2159+
assert!(is_alive(supervisor_pid), "the supervisor is still alive");
2160+
}
2161+
20672162
#[test]
20682163
fn spawn_and_run_single_process() {
20692164
let scheduler = Scheduler::new(2);

0 commit comments

Comments
 (0)