@@ -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