@@ -998,7 +998,8 @@ impl GcState {
998998 self . promote_survivors ( generation, & survivor_refs) ;
999999 drop ( survivor_refs) ;
10001000
1001- // Resurrected objects stay tracked — just drop our references
1001+ // Resurrected objects stay tracked and survive this collection too.
1002+ self . promote_survivors ( generation, & resurrected) ;
10021003 drop ( resurrected) ;
10031004
10041005 if debug. contains ( GcDebugFlags :: COLLECTABLE ) {
@@ -1012,6 +1013,7 @@ impl GcState {
10121013 }
10131014
10141015 if debug. contains ( GcDebugFlags :: SAVEALL ) {
1016+ self . promote_survivors ( generation, & truly_dead) ;
10151017 let mut garbage_guard = gc. garbage . lock ( ) ;
10161018 for obj_ref in & truly_dead {
10171019 garbage_guard. push ( obj_ref. clone ( ) ) ;
@@ -1129,7 +1131,7 @@ impl GcState {
11291131 }
11301132 }
11311133
1132- /// Promote surviving objects to the next generation.
1134+ /// Promote surviving objects to the next generation, or gen2 for a full collection .
11331135 ///
11341136 /// `survivors` must be strong references (`PyObjectRef`) to keep objects alive,
11351137 /// since the generation read locks are released before this is called.
@@ -1138,36 +1140,41 @@ impl GcState {
11381140 /// a race where concurrent `untrack_object` reads a stale `gc_generation`
11391141 /// and operates on the wrong list.
11401142 fn promote_survivors ( & self , from_gen : usize , survivors : & [ PyObjectRef ] ) {
1141- if from_gen >= 2 {
1142- return ; // Already in oldest generation
1143- }
1144-
1145- let next_gen = from_gen + 1 ;
1146-
1147- for obj_ref in survivors {
1148- let obj = obj_ref. as_ref ( ) ;
1149- let ptr = NonNull :: from ( obj) ;
1150- let obj_gen = obj. gc_generation ( ) ;
1151- if obj_gen as usize <= from_gen && obj_gen <= 2 {
1152- let src_gen = obj_gen as usize ;
1143+ let next_gen = ( from_gen + 1 ) . min ( 2 ) ;
11531144
1145+ // The world has restarted by this point. Batch lock acquisition and
1146+ // counter updates, but bound each batch so other interpreters can keep
1147+ // tracking and untracking objects between batches.
1148+ for batch in survivors. chunks ( 256 ) {
1149+ for src_gen in 0 ..next_gen {
11541150 // Lock both source and destination lists simultaneously.
11551151 // Always ascending order (src_gen < next_gen) → no deadlock.
11561152 let mut src = self . generation_lists [ src_gen] . write ( ) ;
11571153 let mut dst = self . generation_lists [ next_gen] . write ( ) ;
1154+ let mut promoted = 0 ;
11581155
1159- // Re-check under locks: object might have been untracked concurrently
1160- if obj. gc_generation ( ) != obj_gen || !obj. is_gc_tracked ( ) {
1161- continue ;
1162- }
1163-
1164- if unsafe { src. remove ( ptr) } . is_some ( ) {
1165- release_count ( & self . counts [ src_gen] ) ;
1156+ for obj_ref in batch {
1157+ let obj = obj_ref. as_ref ( ) ;
1158+ // Re-check under locks: object might have been untracked concurrently
1159+ if obj. gc_generation ( ) as usize != src_gen || !obj. is_gc_tracked ( ) {
1160+ continue ;
1161+ }
11661162
1167- dst. push_front ( ptr) ;
1168- self . counts [ next_gen] . fetch_add ( 1 , Ordering :: Relaxed ) ;
1163+ let ptr = NonNull :: from ( obj) ;
1164+ if unsafe { src. remove ( ptr) } . is_some ( ) {
1165+ dst. push_front ( ptr) ;
1166+ obj. set_gc_generation ( next_gen as u8 ) ;
1167+ promoted += 1 ;
1168+ }
1169+ }
11691170
1170- obj. set_gc_generation ( next_gen as u8 ) ;
1171+ if promoted != 0 {
1172+ let _ = self . counts [ src_gen] . try_update (
1173+ Ordering :: Relaxed ,
1174+ Ordering :: Relaxed ,
1175+ |count| Some ( count. saturating_sub ( promoted) ) ,
1176+ ) ;
1177+ self . counts [ next_gen] . fetch_add ( promoted, Ordering :: Relaxed ) ;
11711178 }
11721179 }
11731180 }
@@ -1615,4 +1622,78 @@ mod tests {
16151622 } ) ;
16161623 assert_eq ! ( underflows, 0 ) ;
16171624 }
1625+
1626+ #[ test]
1627+ fn survivor_promotion_handles_mixed_and_stale_generations ( ) {
1628+ // Isolate list membership and counters from other tests' collections.
1629+ // Detach every object before its normal deallocator consults gc_state().
1630+ struct Heap {
1631+ state : GcState ,
1632+ objects : Vec < PyObjectRef > ,
1633+ }
1634+
1635+ impl Drop for Heap {
1636+ fn drop ( & mut self ) {
1637+ for obj in & self . objects {
1638+ unsafe { self . state . untrack_object ( NonNull :: from ( obj. as_ref ( ) ) ) } ;
1639+ }
1640+ }
1641+ }
1642+
1643+ let ctx = crate :: vm:: Context :: genesis ( ) ;
1644+ // A global collector must not retain a promotion snapshot containing
1645+ // objects that this test has moved into its private lists.
1646+ let _collector = gc_state ( ) . collecting . lock ( ) ;
1647+ let mut heap = Heap {
1648+ state : GcState :: new ( ) ,
1649+ objects : Vec :: new ( ) ,
1650+ } ;
1651+ for _ in 0 ..600 {
1652+ let obj: PyObjectRef = ctx. new_list ( Vec :: new ( ) ) . into ( ) ;
1653+ let ptr = NonNull :: from ( obj. as_ref ( ) ) ;
1654+ unsafe {
1655+ gc_state ( ) . untrack_object ( ptr) ;
1656+ heap. state . track_object ( ptr, 1 ) ;
1657+ }
1658+ heap. objects . push ( obj) ;
1659+ }
1660+
1661+ let Heap { state, objects } = & heap;
1662+ // More than one batch, with survivors initially in all three generations.
1663+ state. promote_survivors ( 0 , & objects[ ..200 ] ) ;
1664+ state. promote_survivors ( 1 , & objects[ ..100 ] ) ;
1665+ assert_eq ! ( state. get_count( ) , ( 400 , 100 , 100 ) ) ;
1666+
1667+ // Finalizers or another thread may freeze/untrack a survivor after the
1668+ // collection took its snapshot, but before promotion acquires the locks.
1669+ objects[ 250 ] . set_gc_owner ( 2 ) ;
1670+ state. freeze ( 2 ) ;
1671+ unsafe { state. untrack_object ( NonNull :: from ( objects[ 500 ] . as_ref ( ) ) ) } ;
1672+ // Counts are advisory and may have been reset by an earlier collection.
1673+ state. counts [ 0 ] . store ( 0 , Ordering :: Relaxed ) ;
1674+
1675+ state. promote_survivors ( 2 , objects) ;
1676+ assert_eq ! ( state. get_count( ) , ( 0 , 0 , 598 ) ) ;
1677+ assert_eq ! ( state. get_freeze_count( ) , 1 ) ;
1678+ for ( index, obj) in objects. iter ( ) . enumerate ( ) {
1679+ let expected = match index {
1680+ 250 => GC_PERMANENT ,
1681+ 500 => GC_UNTRACKED ,
1682+ _ => 2 ,
1683+ } ;
1684+ assert_eq ! ( obj. gc_generation( ) , expected) ;
1685+ }
1686+ assert_eq ! ( state. generation_lists[ 0 ] . read( ) . iter( ) . count( ) , 0 ) ;
1687+ assert_eq ! ( state. generation_lists[ 1 ] . read( ) . iter( ) . count( ) , 0 ) ;
1688+ assert_eq ! ( state. generation_lists[ 2 ] . read( ) . iter( ) . count( ) , 598 ) ;
1689+ // Each node must occur exactly once, with its intrusive links intact.
1690+ let promoted: GcSet < _ > = state. generation_lists [ 2 ]
1691+ . read ( )
1692+ . iter ( )
1693+ . map ( NonNull :: from)
1694+ . collect ( ) ;
1695+ for obj in objects. iter ( ) . filter ( |obj| obj. gc_generation ( ) == 2 ) {
1696+ assert ! ( promoted. contains( & NonNull :: from( obj. as_ref( ) ) ) ) ;
1697+ }
1698+ }
16181699}
0 commit comments