Skip to content
Merged
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
Use alloc::dealloc in FreeList::Drop to avoid thread-local access panic
During thread teardown, Box::from_raw triggers cascading destructors
that may access already-destroyed thread-local storage (GC state, other
freelists). Use raw dealloc instead to free memory without running
destructors.
  • Loading branch information
youknowone committed Mar 4, 2026
commit 3d4951d2dbc0d1f5646283dce59c9f2b5c5bfba4
13 changes: 12 additions & 1 deletion crates/vm/src/object/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,8 +885,19 @@ impl<T: PyPayload> Default for FreeList<T> {

impl<T: PyPayload> Drop for FreeList<T> {
fn drop(&mut self) {
// During thread teardown, we cannot safely run destructors on cached
// objects because their Drop impls may access thread-local storage
// (GC state, other freelists) that is already destroyed.
// Instead, free just the raw allocation. The payload's heap fields
// (BigInt, PyObjectRef, etc.) are leaked, but this is bounded by
// MAX_FREELIST per type per thread.
for ptr in self.items.drain(..) {
drop(unsafe { Box::from_raw(ptr as *mut PyInner<T>) });
unsafe {
alloc::alloc::dealloc(
ptr as *mut u8,
alloc::alloc::Layout::new::<PyInner<T>>(),
);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
}
}
Expand Down