@@ -724,8 +724,7 @@ fn propagate_exit_signals(
724724 } ;
725725 drop ( proc) ;
726726
727- entry. inbox . push ( fragment) ;
728- entry. has_exit_signals . store ( true , Ordering :: Release ) ;
727+ entry. inbox . push ( MessageFragment :: ExitSignal ( Box :: new ( fragment) ) ) ;
729728 // Try waking the process. try_wake() handles receive-waiting,
730729 // try_wake_io() handles IO-waiting. Only one CAS can succeed.
731730 // The worker that runs the process cancels its receive timer;
@@ -1594,12 +1593,15 @@ fn worker_loop(
15941593 // from a linked process or when the runtime shuts down.
15951594 let is_initial = proc. id == initial_pid. load ( Ordering :: Relaxed ) ;
15961595 if is_initial && !proc. otp . links . is_empty ( ) {
1597- proc. transition ( ProcessState :: Waiting ( WaitReason :: Receive {
1598- timeout_deadline_ms : None ,
1599- } ) ) ;
1600- runnable_count. fetch_sub ( 1 , Ordering :: AcqRel ) ;
1601- drop ( proc) ;
1602- // Do not re-inject — the process sleeps until an EXIT
1596+ // A signal can arrive during the final Running slice
1597+ // and fail its wake. Publish the wait before checking
1598+ // the inbox, just as for an explicit receive.
1599+ if publish_wait ( proc, & proc_entry, ProcessState :: Waiting (
1600+ WaitReason :: Receive { timeout_deadline_ms : None } ) ,
1601+ & timer_wheel, & runnable_count) {
1602+ local. push ( proc_entry) ;
1603+ }
1604+ // Otherwise the process sleeps until an EXIT
16031605 // signal or message wakes it, at which point it will
16041606 // exit normally (no more code to run).
16051607 continue ;
@@ -2423,6 +2425,149 @@ mod tests {
24232425 expired. len ( )
24242426 }
24252427
2428+ #[ derive( Clone , Copy , Debug ) ]
2429+ enum FinalSliceDelivery {
2430+ Message ,
2431+ Exit ,
2432+ ExitBeforeSenderWake ,
2433+ Empty ,
2434+ }
2435+
2436+ // The initial entry returns while it still owns a link. Deliver in its
2437+ // actual Running slice, after the worker's inbox drain, so the sender's
2438+ // wake fails. The worker must publish its wait and recover that wake.
2439+ fn check_initial_return_with_links ( delivery : FinalSliceDelivery ) {
2440+ use crate :: message:: { FragmentElem , ImmediateTerm , MessageFragment } ;
2441+ use std:: cell:: RefCell ;
2442+
2443+ thread_local ! {
2444+ static FINAL_SLICE_SEND : RefCell <Option <Box <dyn FnOnce ( ) >>> = const { RefCell :: new( None ) } ;
2445+ }
2446+ unsafe extern "C" fn returning_entry (
2447+ _ctx : * mut crate :: process:: ProcessContext , _args : * const u64 , _nargs : u32 ,
2448+ ) {
2449+ FINAL_SLICE_SEND . with ( |send| {
2450+ if let Some ( send) = send. borrow_mut ( ) . take ( ) { send ( ) ; }
2451+ } ) ;
2452+ }
2453+
2454+ let scheduler = Scheduler :: new ( 1 ) ;
2455+ let initial_pid = scheduler. next_pid ( ) ;
2456+ let linked_pid = scheduler. next_pid ( ) ;
2457+ let mut initial = Process :: with_entry ( initial_pid, returning_entry, vec ! [ ] ) ;
2458+ initial. add_link ( linked_pid) ;
2459+ scheduler. spawn_process ( initial) ;
2460+ let Steal :: Success ( initial) = scheduler. injector . steal ( ) else {
2461+ panic ! ( "initial process must be queued" ) ;
2462+ } ;
2463+ let mut linked = Process :: new ( linked_pid) ;
2464+ linked. add_link ( initial_pid) ;
2465+ scheduler. spawn_process ( linked) ;
2466+ let Steal :: Success ( linked) = scheduler. injector . steal ( ) else {
2467+ panic ! ( "linked process must be queued" ) ;
2468+ } ;
2469+ assert ! ( !publish_wait( linked. lock_for_runtime( ) , & linked,
2470+ ProcessState :: Waiting ( WaitReason :: Receive { timeout_deadline_ms: None } ) ,
2471+ & scheduler. timer_wheel, & scheduler. runnable_count) ) ;
2472+ scheduler. injector . push ( initial. clone ( ) ) ;
2473+
2474+ let observed = Arc :: new ( AtomicU32 :: new ( 0 ) ) ;
2475+ let observation = observed. clone ( ) ;
2476+ let target = initial. clone ( ) ;
2477+ FINAL_SLICE_SEND . with ( |send| {
2478+ * send. borrow_mut ( ) = Some ( Box :: new ( move || {
2479+ observation. store ( target. published_status ( ) , Ordering :: Release ) ;
2480+ let fragment = match delivery {
2481+ FinalSliceDelivery :: Message => Some ( MessageFragment :: Immediate (
2482+ ImmediateTerm :: new ( ErlTerm :: atom ( crate :: atom_table:: ATOM_OK ) ) ) ) ,
2483+ FinalSliceDelivery :: Exit | FinalSliceDelivery :: ExitBeforeSenderWake => Some (
2484+ MessageFragment :: ExitSignal ( Box :: new ( MessageFragment :: SimpleTuple ( vec ! [
2485+ FragmentElem :: Immediate ( ImmediateTerm :: new( ErlTerm :: atom(
2486+ crate :: atom_table:: ATOM_EXIT_SIGNAL ) ) ) ,
2487+ FragmentElem :: Immediate ( ImmediateTerm :: new( ErlTerm :: pid( linked_pid) ) ) ,
2488+ FragmentElem :: Immediate ( ImmediateTerm :: new( ErlTerm :: atom(
2489+ crate :: atom_table:: ATOM_BADARG ) ) ) ,
2490+ ] ) ) ) ) ,
2491+ FinalSliceDelivery :: Empty => None ,
2492+ } ;
2493+ if let Some ( fragment) = fragment {
2494+ target. inbox . push ( fragment) ;
2495+ // Record an unexpected successful sender wake without
2496+ // panicking across the native entry boundary.
2497+ if !matches ! ( delivery, FinalSliceDelivery :: ExitBeforeSenderWake )
2498+ && target. try_wake ( ) { observation. store ( u32:: MAX , Ordering :: Release ) ; }
2499+ }
2500+ } ) ) ;
2501+ } ) ;
2502+ let worker = Worker :: new_fifo ( ) ;
2503+ let stealers = Arc :: new ( vec ! [ worker. stealer( ) ] ) ;
2504+ // Controlled parks bound the old hang without sleeps or retries.
2505+ let parks = std:: cell:: Cell :: new ( 0 ) ;
2506+ let forced_stop = std:: cell:: Cell :: new ( false ) ;
2507+ #[ cfg( mio_supported) ]
2508+ let ( reactor, join) = crate :: reactor:: spawn_reactor ( ) ;
2509+ worker_loop ( 0 , worker, scheduler. injector . clone ( ) , stealers,
2510+ scheduler. process_table . clone ( ) , scheduler. shutdown . clone ( ) ,
2511+ scheduler. runnable_count . clone ( ) , scheduler. alive_count . clone ( ) ,
2512+ scheduler. any_crash . clone ( ) , scheduler. initial_pid . clone ( ) , 1 ,
2513+ scheduler. registered_names . clone ( ) , scheduler. timer_wheel . clone ( ) ,
2514+ scheduler. io_pool . clone ( ) , scheduler. worker_stats [ 0 ] . clone ( ) ,
2515+ scheduler. worker_inboxes [ 0 ] . clone ( ) , Arc :: new ( vec ! [ ] ) ,
2516+ |_| {
2517+ parks. set ( parks. get ( ) + 1 ) ;
2518+ if parks. get ( ) > 16 {
2519+ forced_stop. set ( true ) ;
2520+ scheduler. shutdown . store ( true , Ordering :: SeqCst ) ;
2521+ }
2522+ } ,
2523+ #[ cfg( mio_supported) ]
2524+ reactor. clone ( ) ) ;
2525+ #[ cfg( mio_supported) ]
2526+ { reactor. shutdown ( ) ; join. join ( ) . unwrap ( ) ; }
2527+ FINAL_SLICE_SEND . with ( |send| assert ! ( send. borrow( ) . is_none( ) ) ) ;
2528+ eprintln ! ( "initial return: delivery={delivery:?} state_at_send={} inbox_empty={} \
2529+ runnable={} forced_stop={}", observed. load( Ordering :: Acquire ) ,
2530+ initial. inbox. is_empty( ) , scheduler. runnable_count. load( Ordering :: Acquire ) ,
2531+ forced_stop. get( ) ) ;
2532+ assert_eq ! ( observed. load( Ordering :: Acquire ) , ASTATUS_RUNNING ) ;
2533+ assert ! ( !forced_stop. get( ) , "the final-slice delivery must not strand the worker" ) ;
2534+ assert ! ( initial. inbox. is_empty( ) ) ;
2535+ assert_eq ! ( scheduler. runnable_count. load( Ordering :: Acquire ) , 0 ) ;
2536+ assert ! ( scheduler. shutdown. load( Ordering :: Acquire ) ) ;
2537+ assert_eq ! ( scheduler. any_crash. load( Ordering :: Acquire ) ,
2538+ matches!( delivery, FinalSliceDelivery :: Exit | FinalSliceDelivery :: ExitBeforeSenderWake ) ) ;
2539+ if matches ! ( delivery, FinalSliceDelivery :: Empty ) {
2540+ assert_eq ! ( initial. published_status( ) , ASTATUS_WAITING ) ;
2541+ assert_eq ! ( initial. lock_for_runtime( ) . otp. links. len( ) , 1 ) ;
2542+ } else {
2543+ assert_eq ! ( initial. published_status( ) , ASTATUS_EXITED ) ;
2544+ assert ! ( scheduler. get_process( initial_pid) . is_none( ) ) ;
2545+ }
2546+ assert ! ( processes_are_blocked( & scheduler. process_table) ) ;
2547+ }
2548+
2549+ #[ test]
2550+ fn initial_return_with_links_observes_final_slice_message ( ) {
2551+ check_initial_return_with_links ( FinalSliceDelivery :: Message ) ;
2552+ }
2553+
2554+ #[ test]
2555+ fn initial_return_with_links_observes_final_slice_exit ( ) {
2556+ check_initial_return_with_links ( FinalSliceDelivery :: Exit ) ;
2557+ }
2558+
2559+ #[ test]
2560+ fn initial_return_with_links_observes_exit_before_sender_wake ( ) {
2561+ // Pause the sender after inbox publication, before its wake attempt.
2562+ // Classification must already be visible to the receiver's self-wake.
2563+ check_initial_return_with_links ( FinalSliceDelivery :: ExitBeforeSenderWake ) ;
2564+ }
2565+
2566+ #[ test]
2567+ fn initial_return_with_links_without_delivery_stays_waiting ( ) {
2568+ check_initial_return_with_links ( FinalSliceDelivery :: Empty ) ;
2569+ }
2570+
24262571 #[ test]
24272572 fn stale_receive_timer_cannot_replace_a_later_one ( ) {
24282573 let scheduler = Scheduler :: new ( 1 ) ;
@@ -2580,14 +2725,13 @@ mod tests {
25802725 use crate :: message:: { FragmentElem , ImmediateTerm , MessageFragment } ;
25812726 // exit/2 can wake the target before another process's
25822727 // linked exit kills it; the queued kill is now stale.
2583- linked. inbox . push ( MessageFragment :: SimpleTuple ( vec ! [
2728+ linked. inbox . push ( MessageFragment :: ExitSignal ( Box :: new ( MessageFragment :: SimpleTuple ( vec ! [
25842729 FragmentElem :: Immediate ( ImmediateTerm :: new( ErlTerm :: atom(
25852730 crate :: atom_table:: ATOM_EXIT_SIGNAL ) ) ) ,
25862731 FragmentElem :: Immediate ( ImmediateTerm :: new( ErlTerm :: pid( dead_pid) ) ) ,
25872732 FragmentElem :: Immediate ( ImmediateTerm :: new( ErlTerm :: atom(
25882733 crate :: atom_table:: ATOM_KILL ) ) ) ,
2589- ] ) ) ;
2590- linked. has_exit_signals . store ( true , Ordering :: Release ) ;
2734+ ] ) ) ) ) ;
25912735 }
25922736 assert ! ( if state == io { linked. try_wake_io( ) } else { linked. try_wake( ) } ) ;
25932737 scheduler. runnable_count . fetch_add ( 1 , Ordering :: AcqRel ) ;
0 commit comments