Skip to content

Commit e6efa79

Browse files
committed
Fix format, revert eager retained_back materialization
Remove double blank line in frame.rs (lint failure). Revert with_frame retained_back to only use already-materialized caller FrameObjects — eagerly materializing the caller added refcounts on local variables, preventing timely deallocation and causing test_io, test_memoryview, and test_futures failures. Assisted-by: Claude
1 parent 8264e2c commit e6efa79

3 files changed

Lines changed: 29 additions & 10 deletions

File tree

‎crates/vm/src/builtins/frame.rs‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,14 @@ impl FrameObject {
573573
// lasti points past the current instruction (already incremented).
574574
// stacks[lasti - 1] gives the stack state before executing the
575575
// instruction that triggered this trace event, which is the current
576-
// evaluation stack.
577-
let current_lasti = self.lasti() as usize;
576+
// evaluation stack. Read from the live iframe when available so the
577+
// value reflects the actual execution position.
578+
let live = self.find_live_source_iframe();
579+
let current_lasti = if !live.is_null() {
580+
(unsafe { (*live).lasti.load(Relaxed) }) as usize
581+
} else {
582+
self.lasti() as usize
583+
};
578584
let start_idx = current_lasti.saturating_sub(1);
579585
let start_stack = if start_idx < stacks.len() {
580586
stacks[start_idx]
@@ -624,8 +630,7 @@ impl FrameObject {
624630

625631
// Store the pending unwind and new lasti. When this frame is backed
626632
// by a live stack-allocated iframe, write to the live iframe so the
627-
// execution loop picks up the jump target.
628-
let live = self.find_live_source_iframe();
633+
// execution loop picks up the jump target. Reuse `live` from above.
629634
let target = if !live.is_null() {
630635
unsafe { &*live }
631636
} else {

‎crates/vm/src/frame.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1594,7 +1594,6 @@ impl FrameObject {
15941594
self.iframe().lasti.store(val, Relaxed);
15951595
}
15961596

1597-
15981597
fn has_active_hidden_locals(&self) -> bool {
15991598
use rustpython_compiler_core::bytecode::{CO_FAST_CELL, CO_FAST_FREE, CO_FAST_HIDDEN};
16001599
let code = self.iframe().code();

‎crates/vm/src/vm/mod.rs‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,6 +1726,10 @@ impl VirtualMachine {
17261726
}
17271727

17281728
self.recursion_depth.update(|d| d + 1);
1729+
// Decrement on all exit paths (including panic between here and
1730+
// the explicit decrement at the bottom).
1731+
let _depth_guard =
1732+
scopeguard::guard((), |()| self.recursion_depth.update(|d| d.saturating_sub(1)));
17291733

17301734
#[cfg(all(not(unix), feature = "threading"))]
17311735
crate::vm::thread::push_thread_frame(FramePtr(NonNull::from(&*frame)));
@@ -1750,20 +1754,29 @@ impl VirtualMachine {
17501754
core::sync::atomic::Ordering::AcqRel,
17511755
);
17521756

1753-
let result = self.dispatch_traced_frame(&frame, |frame| f(frame.to_owned()));
1757+
let result = if self.use_tracing.get() {
1758+
self.dispatch_traced_frame(&frame, |frame| f(frame.to_owned()))
1759+
} else {
1760+
f(frame.to_owned())
1761+
};
17541762

17551763
// Capture f_back before clearing previous so code holding a
17561764
// reference to this FrameObject can walk the chain after return.
17571765
if !old_chain.is_null() {
17581766
let strong = frame.as_object().strong_count();
1759-
// Only set retained_back if someone else holds a reference (escaped).
1767+
// Only set retained_back if someone else holds a reference (escaped)
1768+
// AND the caller already has a FrameObject. Materializing the caller
1769+
// here would add refcounts on its local variables, preventing timely
1770+
// __del__ / ResourceWarning on dealloc. If the caller hasn't been
1771+
// materialized, f_back will resolve via the TLS chain while the
1772+
// caller is still executing, or return None after it returns.
17601773
if strong > 1 {
17611774
let mut guard = frame.iframe().retained_back.lock();
17621775
if guard.is_none() {
17631776
let prev_iframe = unsafe { &*old_chain };
1764-
// Materialize the caller if needed so f_back resolves.
1765-
let fo = prev_iframe.materialize(self);
1766-
*guard = Some(fo.to_owned());
1777+
if let Some(fo) = prev_iframe.frame_obj() {
1778+
*guard = Some(fo.to_owned());
1779+
}
17671780
}
17681781
}
17691782
}
@@ -1788,6 +1801,8 @@ impl VirtualMachine {
17881801
let _ = crate::vm::thread::set_current_frame(old_chain);
17891802
#[cfg(all(not(unix), feature = "threading"))]
17901803
crate::vm::thread::pop_thread_frame();
1804+
// Disarm the panic guard — normal decrement.
1805+
scopeguard::ScopeGuard::into_inner(_depth_guard);
17911806
self.recursion_depth.update(|d| d - 1);
17921807

17931808
result

0 commit comments

Comments
 (0)