@@ -1957,6 +1957,7 @@ impl PyCData {
19571957 break ;
19581958 } ;
19591959 let Some ( root) = Self :: overlapping_storage_root ( & source, address, size) else {
1960+ Self :: register_buffer_writer ( & source, pointer, vm) ?;
19601961 continue ;
19611962 } ;
19621963 // Resurrection may have cleared the registry's weakref even
@@ -1980,6 +1981,9 @@ impl PyCData {
19801981 let overlaps = root
19811982 . downcast_ref :: < Self > ( )
19821983 . is_some_and ( |root| root. overlaps_storage ( address, size) ) ;
1984+ if !overlaps {
1985+ Self :: register_buffer_writer ( & root, pointer, vm) ?;
1986+ }
19831987 // A field needs only paths to its own destination. Retargeting
19841988 // invalidates these by identity/range and rediscovers the path;
19851989 // retaining every sibling here would create N caches of N paths.
@@ -2032,12 +2036,37 @@ impl PyCData {
20322036 if owned {
20332037 continue ;
20342038 }
2039+ } else {
2040+ Self :: register_buffer_writer ( & obj, pointer, vm) ?;
20352041 }
20362042 Self :: provenance_children ( & obj, & mut pending) ;
20372043 }
20382044 Ok ( direct. or ( target) )
20392045 }
20402046
2047+ // A byref cast has separate ownership metadata and can write outside its
2048+ // source view while remaining inside the shared exporter. Let other aliases
2049+ // reach that writer through the source, including readers created later.
2050+ // This provenance link does not select or pin the write's destination.
2051+ fn register_buffer_writer (
2052+ source : & PyObject ,
2053+ pointer : & PyObject ,
2054+ vm : & VirtualMachine ,
2055+ ) -> PyResult < ( ) > {
2056+ let root = Self :: find_root_object ( source) ;
2057+ let registered = root. downcast_ref :: < Self > ( ) . is_some_and ( |storage| {
2058+ storage
2059+ . kept_refs
2060+ . read ( )
2061+ . as_ref ( )
2062+ . is_some_and ( |refs| refs. buffer_alias . is_some ( ) )
2063+ } ) ;
2064+ if registered {
2065+ Self :: register_writer ( & root, pointer, vm) ?;
2066+ }
2067+ Ok ( ( ) )
2068+ }
2069+
20412070 fn register_contents_writer (
20422071 parent : & PyObject ,
20432072 key : & str ,
0 commit comments