Skip to content

Commit 0ba3a55

Browse files
committed
fix
1 parent a485341 commit 0ba3a55

File tree

4 files changed

+23
-22
lines changed

4 files changed

+23
-22
lines changed

crates/common/src/refcount.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,16 @@ impl RefCount {
119119

120120
/// Returns true if successful
121121
#[inline]
122+
#[must_use]
122123
pub fn safe_inc(&self) -> bool {
123124
let mut old = State::from_raw(self.state.load(Ordering::SeqCst));
124125
loop {
125126
if old.destructed() {
126127
return false;
127128
}
129+
if (old.strong() as usize) >= STRONG {
130+
refcount_overflow();
131+
}
128132
let new_state = old.add_strong(1);
129133
match self.state.compare_exchange(
130134
old.as_raw(),
@@ -140,13 +144,12 @@ impl RefCount {
140144

141145
/// Decrement strong count. Returns true when count drops to 0.
142146
#[inline]
147+
#[must_use]
143148
pub fn dec(&self) -> bool {
144149
let old = State::from_raw(self.state.fetch_sub(COUNT, Ordering::SeqCst));
145150

146-
// LEAKED objects never reach 0.
147-
// Undo the subtraction to prevent underflow from corrupting flag bits.
151+
// LEAKED objects never reach 0
148152
if old.leaked() {
149-
self.state.fetch_add(COUNT, Ordering::SeqCst);
150153
return false;
151154
}
152155

crates/vm/src/object/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,7 @@ impl PyObject {
11661166
// Undo the temporary resurrection. Always remove both
11671167
// temporary refs; the second dec returns true only when
11681168
// ref_count drops to 0 (no resurrection).
1169-
zelf.0.ref_count.dec();
1169+
let _ = zelf.0.ref_count.dec();
11701170
zelf.0.ref_count.dec()
11711171
});
11721172
match ret {

crates/vm/src/vm/interpreter.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,12 @@ impl Interpreter {
387387
/// Finalization steps (matching Py_FinalizeEx):
388388
/// 1. Flush stdout and stderr.
389389
/// 1. Handle exit exception and turn it to exit code.
390-
/// 1. Wait for thread shutdown (call threading._shutdown).
391-
/// 1. Set finalizing flag (suppresses unraisable exceptions).
392390
/// 1. Call threading._shutdown() to join non-daemon threads.
393391
/// 1. Run atexit exit functions.
394-
/// 1. GC pass and module cleanup (finalize_modules).
395-
/// 1. Final GC pass.
392+
/// 1. Set finalizing flag (suppresses unraisable exceptions from __del__).
393+
/// 1. Forced GC collection pass (collect cycles while builtins are available).
394+
/// 1. Module finalization (finalize_modules).
395+
/// 1. Final stdout/stderr flush.
396396
///
397397
/// Note that calling `finalize` is not necessary by purpose though.
398398
pub fn finalize(self, exc: Option<PyBaseExceptionRef>) -> u32 {

crates/vm/src/vm/mod.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ impl VirtualMachine {
821821
}
822822
}
823823

824-
/// Phase 4: Clear module dicts in reverse import order.
824+
/// Phase 5: Clear module dicts in reverse import order.
825825
/// Skip builtins and sys — those are cleared last in Phase 7.
826826
fn finalize_clear_module_dicts(&self, module_weakrefs: &[(String, PyRef<PyWeak>)]) {
827827
let builtins_dict = self.builtins.dict();
@@ -1119,18 +1119,6 @@ impl VirtualMachine {
11191119
core::sync::atomic::Ordering::AcqRel,
11201120
);
11211121

1122-
use crate::protocol::TraceEvent;
1123-
let result = match self.trace_event(TraceEvent::Call, None) {
1124-
Ok(()) => {
1125-
let result = f(frame);
1126-
if result.is_ok() {
1127-
let _ = self.trace_event(TraceEvent::Return, None);
1128-
}
1129-
result
1130-
}
1131-
Err(e) => Err(e),
1132-
};
1133-
11341122
// Ensure cleanup on panic: restore owner, pop exception, frame chain, frames Vec,
11351123
// and recursion depth.
11361124
scopeguard::defer! {
@@ -1146,7 +1134,17 @@ impl VirtualMachine {
11461134
self.recursion_depth.update(|d| d - 1);
11471135
}
11481136

1149-
result
1137+
use crate::protocol::TraceEvent;
1138+
match self.trace_event(TraceEvent::Call, None) {
1139+
Ok(()) => {
1140+
let result = f(frame);
1141+
if result.is_ok() {
1142+
let _ = self.trace_event(TraceEvent::Return, None);
1143+
}
1144+
result
1145+
}
1146+
Err(e) => Err(e),
1147+
}
11501148
}
11511149

11521150
/// Returns a basic CompileOpts instance with options accurate to the vm. Used

0 commit comments

Comments
 (0)