Skip to content

Commit 3ccbe51

Browse files
committed
Drop cached values in type_cache_clear_version to prevent refcount leaks
Restore value dropping when invalidating cache entries for a specific version tag. Without this, cached PyObjectRef values were leaked until entry reuse, causing inflated sys.getrefcount() in test_memoryview.
1 parent e593b38 commit 3ccbe51

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

crates/vm/src/builtins/type.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,19 @@ fn type_cache_hash(version: u32, name: &'static PyStrInterned) -> usize {
132132
((version ^ name_hash) as usize) & TYPE_CACHE_MASK
133133
}
134134

135-
/// Invalidate cache entries for a specific version tag.
136-
/// Called from modified() when a type is changed. Only sets version to 0;
137-
/// value pointers are left in place and cleaned up when entries are reused
138-
/// (populate) or during type_cache_clear (GC).
135+
/// Invalidate cache entries for a specific version tag and release values.
136+
/// Called from modified() when a type is changed.
139137
fn type_cache_clear_version(version: u32) {
138+
let mut to_drop = Vec::new();
140139
for entry in TYPE_CACHE.iter() {
141140
if entry.version.load(Ordering::Relaxed) == version {
142141
entry.version.store(0, Ordering::Release);
142+
if let Some(v) = entry.take_value() {
143+
to_drop.push(v);
144+
}
143145
}
144146
}
147+
drop(to_drop);
145148
}
146149

147150
/// Clear all type cache entries.

0 commit comments

Comments
 (0)