Skip to content

gh-132641: Fix race in lru_cache due to inconsistent critical section… #133787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a race in functools.lru_cache under free-threading.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Fixed a race in functools.lru_cache under free-threading.
Fixed a race in :func:`functools.lru_cache` under free-threading.
8 changes: 4 additions & 4 deletions Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ static int
bounded_lru_cache_get_lock_held(lru_cache_object *self, PyObject *args, PyObject *kwds,
PyObject **result, PyObject **key, Py_hash_t *hash)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self->cache);
lru_list_elem *link;

PyObject *key_ = *key = lru_cache_make_key(self->kwd_mark, args, kwds, self->typed);
Expand Down Expand Up @@ -1330,7 +1330,7 @@ static PyObject *
bounded_lru_cache_update_lock_held(lru_cache_object *self,
PyObject *result, PyObject *key, Py_hash_t hash)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self->cache);
lru_list_elem *link;
PyObject *testresult;
int res;
Expand Down Expand Up @@ -1479,7 +1479,7 @@ bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds
Py_hash_t hash;
int res;

Py_BEGIN_CRITICAL_SECTION(self);
Py_BEGIN_CRITICAL_SECTION(self->cache);
res = bounded_lru_cache_get_lock_held(self, args, kwds, &result, &key, &hash);
Py_END_CRITICAL_SECTION();

Expand All @@ -1492,7 +1492,7 @@ bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds

result = PyObject_Call(self->func, args, kwds);

Py_BEGIN_CRITICAL_SECTION(self);
Py_BEGIN_CRITICAL_SECTION(self->cache);
/* Note: key will be stolen in the below function, and
result may be stolen or sometimes re-returned as a passthrough.
Treat both as being stolen.
Expand Down
Loading