Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 6 additions & 2 deletions src/policies/fifopolicy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ impl traits::OccupiedExt for Occupied<'_> {

#[inline]
fn replace(self, new: Self::Handle) -> Self::Handle {
// In update we don't need to increment this; because this does not change the memory address ranges
// self.shared.generation_version().increment();
// The replace operation does not modify memory range here in the normal case,
// but when the GIL is disabled, concurrent modification can cause a segfault.
// Therefore, in free-threaded versions we have to change the generation version
// before manipulating memory.
#[cfg(Py_GIL_DISABLED)]
self.shared.generation_version().increment();

let item = unsafe { get_handle!(&mut self.policy, *self.bucket.as_ref()) };

Expand Down
8 changes: 6 additions & 2 deletions src/policies/lfupolicy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,12 @@ impl traits::OccupiedExt for Occupied<'_> {
type Shared = Shared;

fn replace(self, new: Self::Handle) -> Self::Handle {
// Here we don't need to increment generation version
// self.shared.generation_version().increment();
// The replace operation does not modify memory range here in the normal case,
// but when the GIL is disabled, concurrent modification can cause a segfault.
// Therefore, in free-threaded versions we have to change the generation version
// before manipulating memory.
#[cfg(Py_GIL_DISABLED)]
self.shared.generation_version().increment();

unsafe {
let cursor = self.bucket.as_mut();
Expand Down
7 changes: 7 additions & 0 deletions src/policies/nopolicy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ impl traits::OccupiedExt for Occupied<'_> {
}

fn replace(self, new: Self::Handle) -> Self::Handle {
// The replace operation does not modify memory range here in the normal case,
// but when the GIL is disabled, concurrent modification can cause a segfault.
// Therefore, in free-threaded versions we have to change the generation version
// before manipulating memory.
#[cfg(Py_GIL_DISABLED)]
self.shared.generation_version().increment();

self.policy.currsize = self.policy.currsize.saturating_add(new.size());
let old = unsafe { std::mem::replace(self.bucket.as_mut(), new) };
self.policy.currsize = self.policy.currsize.saturating_sub(old.size());
Expand Down
7 changes: 7 additions & 0 deletions src/policies/rrpolicy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ impl traits::OccupiedExt for Occupied<'_> {

#[inline]
fn replace(self, new: Self::Handle) -> Self::Handle {
// The replace operation does not modify memory range here in the normal case,
// but when the GIL is disabled, concurrent modification can cause a segfault.
// Therefore, in free-threaded versions we have to change the generation version
// before manipulating memory.
#[cfg(Py_GIL_DISABLED)]
self.shared.generation_version().increment();

self.policy.currsize = self.policy.currsize.saturating_add(new.size());
let old = unsafe { std::mem::replace(self.bucket.as_mut(), new) };
self.policy.currsize = self.policy.currsize.saturating_sub(old.size());
Expand Down
8 changes: 6 additions & 2 deletions src/policies/ttlpolicy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,12 @@ impl traits::OccupiedExt for Occupied<'_> {

#[inline]
fn replace(self, new: Self::Handle) -> Self::Handle {
// In update we don't need to increment this; because this does not change the memory address ranges
// self.shared.generation_version().increment();
// The replace operation does not modify memory range here in the normal case,
// but when the GIL is disabled, concurrent modification can cause a segfault.
// Therefore, in free-threaded versions we have to change the generation version
// before manipulating memory.
#[cfg(Py_GIL_DISABLED)]
self.shared.generation_version().increment();

let item = unsafe { get_handle!(&mut self.policy, *self.bucket.as_ref()) };

Expand Down
8 changes: 6 additions & 2 deletions src/policies/vttlpolicy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,12 @@ impl traits::OccupiedExt for Occupied<'_> {
type Shared = Shared;

fn replace(self, new: Self::Handle) -> Self::Handle {
// Here we don't need to increment generation version
// self.shared.generation_version().increment();
// The replace operation does not modify memory range here in the normal case,
// but when the GIL is disabled, concurrent modification can cause a segfault.
// Therefore, in free-threaded versions we have to change the generation version
// before manipulating memory.
#[cfg(Py_GIL_DISABLED)]
self.shared.generation_version().increment();

unsafe {
let cursor = self.bucket.as_mut();
Expand Down
16 changes: 10 additions & 6 deletions tests/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import platform
import subprocess
import sys
import sysconfig
import threading
import time
import typing
Expand Down Expand Up @@ -669,13 +670,16 @@ def test_generation_version_on_insert(self):
for _ in cache.items():
cache.insert("C", 1)

if isinstance(cache, cachebox.LRUCache):
return
if not isinstance(cache, cachebox.LRUCache) and not sysconfig.get_config_var(
"Py_GIL_DISABLED"
):
for i in cache:
cache.insert(i, "hello")

for i in cache:
# It should not increment the generation version
# in replacing value
cache.insert(i, "hello")
else:
with pytest.raises(RuntimeError):
for i in cache:
cache.insert(i, "hello")

def test_generation_version_on_shrink_to_fit(self):
cache = self.create_cache(10, {i: i for i in range(3)})
Expand Down
4 changes: 4 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading