@@ -579,9 +579,9 @@ fn drain_inbox_into(entry: &ProcessEntry, proc: &mut Process) {
579579 handle_active_read_on_worker ( proc, stream_handle, & in_flight) ;
580580 }
581581 }
582- MessageFragment :: ExitSignal ( signal ) => {
583- let msg = signal . copy_to_heap ( & mut proc. heap ) ;
584- proc. mailbox . send_exit_signal ( msg) ;
582+ MessageFragment :: ExitSignal { message , normal_self_exit } => {
583+ let msg = message . copy_to_heap ( & mut proc. heap ) ;
584+ proc. mailbox . send_exit_signal ( msg, normal_self_exit ) ;
585585 entry. has_exit_signals . store ( true , Ordering :: Release ) ;
586586 }
587587 other => {
@@ -614,9 +614,9 @@ fn drain_inbox_into(entry: &ProcessEntry, proc: &mut Process) {
614614 handle_active_read_on_worker ( proc, stream_handle, & in_flight) ;
615615 }
616616 }
617- MessageFragment :: ExitSignal ( signal ) => {
618- let msg = signal . copy_to_heap ( & mut proc. heap ) ;
619- proc. mailbox . send_exit_signal ( msg) ;
617+ MessageFragment :: ExitSignal { message , normal_self_exit } => {
618+ let msg = message . copy_to_heap ( & mut proc. heap ) ;
619+ proc. mailbox . send_exit_signal ( msg, normal_self_exit ) ;
620620 entry. has_exit_signals . store ( true , Ordering :: Release ) ;
621621 }
622622 other => {
@@ -2147,9 +2147,10 @@ impl Process {
21472147/// trapped `{'EXIT', FromPid, Reason}` messages, according to BEAM semantics:
21482148///
21492149/// - `kill` reason: always kill (untrappable) — returns `Some(ATOM_KILLED)`
2150- /// - `trap_exit=true` + non-kill: leave in mailbox as regular message
2151- /// - `trap_exit=false` + `normal` from another process: silently remove
2152- /// - `trap_exit=false` + self-sent `normal` or any other reason: kill
2150+ /// - untrapped self-normal at send time: always exit with `normal`
2151+ /// - `trap_exit=true` + other non-kill: leave in mailbox as regular message
2152+ /// - `trap_exit=false` + other `normal`: silently remove
2153+ /// - `trap_exit=false` + any other reason: kill
21532154///
21542155/// Returns `Some(exit_reason)` if the process should die.
21552156///
@@ -2186,9 +2187,9 @@ pub(crate) unsafe fn process_exit_signals(proc: &mut Process) -> Option<ErlTerm>
21862187 let physical = ( head. wrapping_add ( offset) & mask) as usize ;
21872188 // Provenance belongs to the mailbox slot, not the tuple's contents.
21882189 // Once a signal is trapped it remains an ordinary message on later scans.
2189- if ! proc. mailbox . exit_signals . remove ( & physical) {
2190+ let Some ( normal_self_exit ) = proc. mailbox . exit_signals . remove ( & physical) else {
21902191 continue ;
2191- }
2192+ } ;
21922193 // SAFETY: each original occupied slot was initialized. This loop only
21932194 // tombstones/removes slots: no resize or append invalidates the buffer.
21942195 let msg = ErlTerm ( unsafe { * buffer. add ( physical) } ) ;
@@ -2229,16 +2230,21 @@ pub(crate) unsafe fn process_exit_signals(proc: &mut Process) -> Option<ErlTerm>
22292230 return Some ( ErlTerm :: atom ( ATOM_KILLED ) ) ;
22302231 }
22312232
2233+ if normal_self_exit && reason_atom == Some ( ATOM_NORMAL ) {
2234+ // This call was already fatal when sent. Conversely, a normal
2235+ // self-signal sent while trapping must not become a suicide
2236+ // request merely because trapping is disabled before this scan.
2237+ // SAFETY: this live slot belongs to the unchanged scan buffer.
2238+ unsafe { proc. mailbox . remove_scanned_slot ( physical) ; }
2239+ return Some ( reason) ;
2240+ }
2241+
22322242 if proc. otp . trap_exit {
22332243 // trap_exit=true: keep in mailbox as a regular message
22342244 continue ;
22352245 }
22362246
2237- // A normal signal from another process is ignored, but an explicit
2238- // exit(self(), normal) terminates a process that is not trapping exits.
2239- // SAFETY: the same live three-element tuple contains its sender PID.
2240- let from_pid = unsafe { ErlTerm ( * ptr. add ( 2 ) ) } ;
2241- if reason_atom == Some ( ATOM_NORMAL ) && from_pid != ErlTerm :: pid ( proc. id ) {
2247+ if reason_atom == Some ( ATOM_NORMAL ) {
22422248 // SAFETY: this live slot was just read from the stable scan buffer;
22432249 // no intervening operation changes storage or removes this slot.
22442250 unsafe {
@@ -2247,7 +2253,7 @@ pub(crate) unsafe fn process_exit_signals(proc: &mut Process) -> Option<ErlTerm>
22472253 continue ;
22482254 }
22492255
2250- // Fatal signal, including self-sent normal : exit with this reason
2256+ // Non-normal, non-kill signal : exit with this reason
22512257 // SAFETY: this live slot belongs to the unchanged scan buffer.
22522258 unsafe {
22532259 proc. mailbox . remove_scanned_slot ( physical) ;
@@ -2283,9 +2289,9 @@ pub struct Mailbox {
22832289 scan_pos : u32 ,
22842290 /// Number of tombstoned (removed) slots within `[head, head+len)`.
22852291 tombstone_count : u32 ,
2286- /// Physical slots containing pending exit/2 signals, not ordinary messages .
2292+ /// Pending signal slots and their captured normal-self suicide requests .
22872293 /// Empty before compiled receive code runs; GC preserves slot positions.
2288- exit_signals : HashSet < usize > ,
2294+ exit_signals : HashMap < usize , bool > ,
22892295}
22902296
22912297// Field offsets for codegen inlining.
@@ -2330,7 +2336,7 @@ impl Mailbox {
23302336 len : 0 ,
23312337 scan_pos : 0 ,
23322338 tombstone_count : 0 ,
2333- exit_signals : HashSet :: new ( ) ,
2339+ exit_signals : HashMap :: new ( ) ,
23342340 }
23352341 }
23362342
@@ -2374,7 +2380,7 @@ impl Mailbox {
23742380 assert ! ( !new_buf. is_null( ) , "allocation failed" ) ;
23752381
23762382 let mut write = 0u32 ;
2377- let mut exit_signals = HashSet :: new ( ) ;
2383+ let mut exit_signals = HashMap :: new ( ) ;
23782384 if self . len > 0 && !self . buf . is_null ( ) {
23792385 for i in 0 ..self . len {
23802386 let src_idx = self . physical_index ( i) ;
@@ -2385,8 +2391,8 @@ impl Mailbox {
23852391 unsafe {
23862392 * new_buf. add ( write as usize ) = val;
23872393 }
2388- if self . exit_signals . contains ( & src_idx) {
2389- exit_signals. insert ( write as usize ) ;
2394+ if let Some ( & normal_self_exit ) = self . exit_signals . get ( & src_idx) {
2395+ exit_signals. insert ( write as usize , normal_self_exit ) ;
23902396 }
23912397 write += 1 ;
23922398 }
@@ -2446,9 +2452,9 @@ impl Mailbox {
24462452 }
24472453
24482454 /// Append an exit/2 signal with provenance that ordinary sends cannot forge.
2449- fn send_exit_signal ( & mut self , msg : ErlTerm ) {
2455+ fn send_exit_signal ( & mut self , msg : ErlTerm , normal_self_exit : bool ) {
24502456 self . send ( msg) ;
2451- self . exit_signals . insert ( self . physical_index ( self . len - 1 ) ) ;
2457+ self . exit_signals . insert ( self . physical_index ( self . len - 1 ) , normal_self_exit ) ;
24522458 }
24532459
24542460 /// Number of live messages currently in the mailbox.
@@ -2566,14 +2572,14 @@ impl Mailbox {
25662572 debug_assert_eq ! ( self . scan_pos, 0 , "compact: must not run during active scan" ) ;
25672573 let alive = self . len - self . tombstone_count ;
25682574 let mut write_pos: u32 = 0 ;
2569- let mut exit_signals = HashSet :: new ( ) ;
2575+ let mut exit_signals = HashMap :: new ( ) ;
25702576 for read_pos in 0 ..self . len {
25712577 let phys_r = self . physical_index ( read_pos) ;
25722578 let val = unsafe { * self . buf . add ( phys_r) } ;
25732579 if val != crate :: term:: TOMBSTONE_VALUE {
25742580 let phys_w = self . physical_index ( write_pos) ;
2575- if self . exit_signals . contains ( & phys_r) {
2576- exit_signals. insert ( phys_w) ;
2581+ if let Some ( & normal_self_exit ) = self . exit_signals . get ( & phys_r) {
2582+ exit_signals. insert ( phys_w, normal_self_exit ) ;
25772583 }
25782584 if write_pos != read_pos {
25792585 unsafe {
@@ -2915,7 +2921,7 @@ mod tests {
29152921 ErlTerm :: pid ( crate :: process:: ProcessId :: new ( 2 ) ) ,
29162922 ErlTerm :: atom ( reason) ,
29172923 ] ) ;
2918- proc. mailbox . send_exit_signal ( signal) ;
2924+ proc. mailbox . send_exit_signal ( signal, false ) ;
29192925 }
29202926 // Fill the ring so the pre-fix stale-length read cannot read uninitialized
29212927 // slots; it still incorrectly skips the kill after removing the head.
@@ -2944,7 +2950,7 @@ mod tests {
29442950 let signal =
29452951 proc. heap
29462952 . alloc_tuple ( & [ ErlTerm :: atom ( ATOM_EXIT_SIGNAL ) , ErlTerm :: pid ( crate :: process:: ProcessId :: new ( 2 ) ) , reason] ) ;
2947- proc. mailbox . send_exit_signal ( signal) ;
2953+ proc. mailbox . send_exit_signal ( signal, false ) ;
29482954 // SAFETY: all roots are immediate or initialized on this process heap,
29492955 // held exclusively; the mailbox roots are updated by the collector.
29502956 unsafe { proc. collect_garbage_full ( ) } ;
@@ -3018,11 +3024,11 @@ mod tests {
30183024 for _ in 0 ..14 {
30193025 proc. mailbox . remove_at ( 0 ) ;
30203026 }
3021- proc. mailbox . send_exit_signal ( normal) ;
3027+ proc. mailbox . send_exit_signal ( normal, false ) ;
30223028 proc. mailbox . send ( ErlTerm :: small_int ( 99 ) ) ;
3023- proc. mailbox . send_exit_signal ( normal) ;
3024- proc. mailbox . send_exit_signal ( normal) ;
3025- proc. mailbox . send_exit_signal ( kill) ;
3029+ proc. mailbox . send_exit_signal ( normal, false ) ;
3030+ proc. mailbox . send_exit_signal ( normal, false ) ;
3031+ proc. mailbox . send_exit_signal ( kill, false ) ;
30263032 proc. mailbox . remove_at ( 3 ) ; // a tombstone between EXIT signals
30273033 proc. mailbox . remove_at ( 0 ) ;
30283034 proc. mailbox . remove_at ( 0 ) ; // head wraps during the next signal removal
@@ -3041,28 +3047,28 @@ mod tests {
30413047 ErlTerm :: atom ( ATOM_NORMAL ) ,
30423048 ] ) ;
30433049 for _ in 0 ..3 {
3044- proc. mailbox . send_exit_signal ( normal) ;
3050+ proc. mailbox . send_exit_signal ( normal, false ) ;
30453051 }
30463052 // SAFETY: every queued tuple is initialized on this live, unmoving heap.
30473053 assert_eq ! ( unsafe { process_exit_signals( & mut proc) } , None ) ;
30483054 assert ! ( proc. mailbox. is_empty( ) ) ;
30493055 }
30503056 #[ test]
30513057 fn exit_signal_provenance_survives_mailbox_growth_and_compaction ( ) {
3052- use crate :: atom_table:: { ATOM_EXIT_SIGNAL , ATOM_KILL , ATOM_KILLED , ATOM_NORMAL } ;
3058+ use crate :: atom_table:: { ATOM_EXIT_SIGNAL , ATOM_NORMAL } ;
30533059 let mut proc = Process :: new ( ProcessId :: new ( 1 ) ) ;
30543060 let ordinary = proc. heap . alloc_tuple ( & [
30553061 ErlTerm :: atom ( ATOM_EXIT_SIGNAL ) , ErlTerm :: pid ( proc. id ) , ErlTerm :: atom ( ATOM_NORMAL ) ,
30563062 ] ) ;
30573063 let signal = proc. heap . alloc_tuple ( & [
3058- ErlTerm :: atom ( ATOM_EXIT_SIGNAL ) , ErlTerm :: pid ( proc. id ) , ErlTerm :: atom ( ATOM_KILL ) ,
3064+ ErlTerm :: atom ( ATOM_EXIT_SIGNAL ) , ErlTerm :: pid ( proc. id ) , ErlTerm :: atom ( ATOM_NORMAL ) ,
30593065 ] ) ;
30603066 // Start with a wrapped ring so growing moves the signal's physical slot.
30613067 for i in 0 ..16 { proc. mailbox . send ( ErlTerm :: small_int ( i) ) ; }
30623068 for _ in 0 ..14 { proc. mailbox . remove_at ( 0 ) ; }
30633069 proc. mailbox . send ( ordinary) ;
30643070 for i in 0 ..10 { proc. mailbox . send ( ErlTerm :: small_int ( i) ) ; }
3065- proc. mailbox . send_exit_signal ( signal) ;
3071+ proc. mailbox . send_exit_signal ( signal, true ) ;
30663072 for i in 0 ..64 { proc. mailbox . send ( ErlTerm :: small_int ( i) ) ; }
30673073 // Remove the leading padding and leave tombstones before and after the
30683074 // signal. Compaction must move its provenance with its message.
@@ -3072,10 +3078,30 @@ mod tests {
30723078 for i in 12 ..60 { proc. mailbox . remove_at ( i) ; }
30733079 proc. mailbox . reset_scan ( ) ;
30743080 // SAFETY: all terms are owned by this live, exclusively accessed heap.
3075- assert_eq ! ( unsafe { process_exit_signals( & mut proc) } , Some ( ErlTerm :: atom( ATOM_KILLED ) ) ) ;
3081+ assert_eq ! ( unsafe { process_exit_signals( & mut proc) } , Some ( ErlTerm :: atom( ATOM_NORMAL ) ) ) ;
30763082 assert_eq ! ( proc. mailbox. messages( ) . next( ) , Some ( ordinary) ) ;
30773083 }
30783084
3085+ #[ test]
3086+ fn exit_normal_self_request_is_per_signal ( ) {
3087+ use crate :: atom_table:: { ATOM_BADARG , ATOM_EXIT_SIGNAL , ATOM_NORMAL } ;
3088+ let mut proc = Process :: new ( ProcessId :: new ( 1 ) ) ;
3089+ let normal = proc. heap . alloc_tuple ( & [
3090+ ErlTerm :: atom ( ATOM_EXIT_SIGNAL ) , ErlTerm :: pid ( proc. id ) , ErlTerm :: atom ( ATOM_NORMAL ) ,
3091+ ] ) ;
3092+ let other = proc. heap . alloc_tuple ( & [
3093+ ErlTerm :: atom ( ATOM_EXIT_SIGNAL ) , ErlTerm :: pid ( ProcessId :: new ( 2 ) ) , ErlTerm :: atom ( ATOM_BADARG ) ,
3094+ ] ) ;
3095+ // The first self-normal was sent while trapping; a later suicide
3096+ // request must not retroactively make that earlier signal fatal.
3097+ proc. mailbox . send_exit_signal ( normal, false ) ;
3098+ proc. mailbox . send_exit_signal ( other, false ) ;
3099+ proc. mailbox . send_exit_signal ( normal, true ) ;
3100+ // SAFETY: every signal is initialized on this live, exclusively owned heap.
3101+ assert_eq ! ( unsafe { process_exit_signals( & mut proc) } , Some ( ErlTerm :: atom( ATOM_BADARG ) ) ) ;
3102+ assert_eq ! ( proc. mailbox. messages( ) . collect:: <Vec <_>>( ) , vec![ normal] ) ;
3103+ }
3104+
30793105 #[ test]
30803106 fn exit_message_provenance_survives_single_and_batch_inbox_drain ( ) {
30813107 use crate :: atom_table:: { ATOM_EXIT_SIGNAL , ATOM_KILL , ATOM_KILLED , ATOM_NORMAL , ATOM_TRUE } ;
@@ -3092,9 +3118,11 @@ mod tests {
30923118 // SAFETY: both source graphs remain live and unmoving on this heap.
30933119 entry. inbox . push ( unsafe { MessageFragment :: from_term ( ordinary) } ) ;
30943120 if single_first { proc. drain_inbox ( ) ; }
3095- entry. inbox . push ( MessageFragment :: ExitSignal ( Box :: new (
3096- unsafe { MessageFragment :: from_term ( signal) }
3097- ) ) ) ;
3121+ entry. inbox . push ( MessageFragment :: ExitSignal {
3122+ // SAFETY: this graph is still live on the locked source heap.
3123+ message : Box :: new ( unsafe { MessageFragment :: from_term ( signal) } ) ,
3124+ normal_self_exit : false ,
3125+ } ) ;
30983126 proc. drain_inbox ( ) ;
30993127 // SAFETY: draining transferred both graphs into this locked process.
31003128 assert_eq ! ( unsafe { process_exit_signals( & mut proc) } , Some ( ErlTerm :: atom( ATOM_KILLED ) ) ) ;
@@ -3704,14 +3732,16 @@ mod tests {
37043732 for & msg in & ordinary {
37053733 entry. inbox . push ( MessageFragment :: Immediate ( ImmediateTerm :: new ( msg) ) ) ;
37063734 }
3707- entry. inbox . push ( MessageFragment :: ExitSignal ( Box :: new (
3708- MessageFragment :: SimpleTuple ( vec ! [
3735+ entry. inbox . push ( MessageFragment :: ExitSignal {
3736+ message : Box :: new ( MessageFragment :: SimpleTuple ( vec ! [
37093737 FragmentElem :: Immediate ( ImmediateTerm :: new( ErlTerm :: atom(
37103738 crate :: atom_table:: ATOM_EXIT_SIGNAL ) ) ) ,
37113739 FragmentElem :: Immediate ( ImmediateTerm :: new( ErlTerm :: pid( ProcessId :: new( 2 ) ) ) ) ,
37123740 FragmentElem :: Immediate ( ImmediateTerm :: new( ErlTerm :: atom(
37133741 crate :: atom_table:: ATOM_BADARG ) ) ) ,
3714- ] ) ) ) ) ;
3742+ ] ) ) ,
3743+ normal_self_exit : false ,
3744+ } ) ;
37153745 // No sender-side flag store or wake has occurred.
37163746 assert ! ( !entry. has_exit_signals. load( Ordering :: Relaxed ) ) ;
37173747 let mut proc = entry. lock_for_runtime ( ) ;
0 commit comments