Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refine call-init recursion guard and cache swap lifetime handling
  • Loading branch information
youknowone committed Mar 10, 2026
commit 2dd64b84174b4469dc94d329b487c8ea7abde8f2
24 changes: 19 additions & 5 deletions crates/vm/src/builtins/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,26 @@ impl TypeSpecializationCache {
}

#[inline]
fn swap_init(&self, new_init: Option<PyRef<PyFunction>>) {
fn swap_init(&self, new_init: Option<PyRef<PyFunction>>, vm: Option<&VirtualMachine>) {
if let Some(vm) = vm {
// Keep replaced refs alive for the currently executing frame, matching
// CPython-style "old pointer remains valid during ongoing execution"
// without accumulating global retired refs.
self.init.swap_to_temporary_refs(new_init, vm);
return;
}
// SAFETY: old value is moved to `retired`, so it stays alive while
// concurrent readers may still hold borrowed references.
let old = unsafe { self.init.swap(new_init) };
self.retire_old_function(old);
}

#[inline]
fn swap_getitem(&self, new_getitem: Option<PyRef<PyFunction>>) {
fn swap_getitem(&self, new_getitem: Option<PyRef<PyFunction>>, vm: Option<&VirtualMachine>) {
if let Some(vm) = vm {
self.getitem.swap_to_temporary_refs(new_getitem, vm);
return;
}
// SAFETY: old value is moved to `retired`, so it stays alive while
// concurrent readers may still hold borrowed references.
let old = unsafe { self.getitem.swap(new_getitem) };
Expand All @@ -317,7 +328,7 @@ impl TypeSpecializationCache {
// Match CPython _spec_cache contract: type modification invalidates
// getitem cache by NULLing the pointer. `getitem_version` becomes
// meaningless when getitem is NULL.
self.swap_getitem(None);
self.swap_getitem(None, None);
}

fn traverse(&self, tracer_fn: &mut TraverseFn<'_>) {
Expand Down Expand Up @@ -888,6 +899,7 @@ impl PyType {
&self,
init: PyRef<PyFunction>,
tp_version: u32,
vm: &VirtualMachine,
) -> bool {
let Some(ext) = self.heaptype_ext.as_ref() else {
return false;
Expand All @@ -898,7 +910,7 @@ impl PyType {
if self.tp_version_tag.load(Ordering::Acquire) != tp_version {
return false;
}
ext.specialization_cache.swap_init(Some(init));
ext.specialization_cache.swap_init(Some(init), Some(vm));
true
}

Expand All @@ -925,6 +937,7 @@ impl PyType {
&self,
getitem: PyRef<PyFunction>,
tp_version: u32,
vm: &VirtualMachine,
) -> bool {
let Some(ext) = self.heaptype_ext.as_ref() else {
return false;
Expand All @@ -939,7 +952,8 @@ impl PyType {
if self.tp_version_tag.load(Ordering::Acquire) != tp_version {
return false;
}
ext.specialization_cache.swap_getitem(Some(getitem));
ext.specialization_cache
.swap_getitem(Some(getitem), Some(vm));
ext.specialization_cache
.getitem_version
.store(func_version, Ordering::Relaxed);
Expand Down
8 changes: 6 additions & 2 deletions crates/vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7752,7 +7752,11 @@ impl ExecutingFrame<'_> {
type_version = cls.assign_version_tag();
}
if type_version != 0 {
if cls.cache_getitem_for_specialization(func.to_owned(), type_version) {
if cls.cache_getitem_for_specialization(
func.to_owned(),
type_version,
vm,
) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Some(Instruction::BinaryOpSubscrGetitem)
} else {
None
Expand Down Expand Up @@ -8286,7 +8290,7 @@ impl ExecutingFrame<'_> {
version = cls.assign_version_tag();
}
if version != 0
&& cls.cache_init_for_specialization(init_func.to_owned(), version)
&& cls.cache_init_for_specialization(init_func.to_owned(), version, vm)
{
unsafe {
self.code
Expand Down