Skip to content

MemoryError freelist is not thread-safe in free threaded build #129668

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

Closed
colesbury opened this issue Feb 4, 2025 · 0 comments
Closed

MemoryError freelist is not thread-safe in free threaded build #129668

colesbury opened this issue Feb 4, 2025 · 0 comments
Assignees
Labels
3.13 bugs and security fixes 3.14 bugs and security fixes topic-free-threading type-bug An unexpected behavior, bug, or error

Comments

@colesbury
Copy link
Contributor

colesbury commented Feb 4, 2025

Bug report

The MemoryError freelist isn't thread-safe if the GIL is disabled:

cpython/Objects/exceptions.c

Lines 3850 to 3860 in 285c1c4

/* Fetch object from freelist and revive it */
self = state->memerrors_freelist;
self->args = PyTuple_New(0);
/* This shouldn't happen since the empty tuple is persistent */
if (self->args == NULL) {
return NULL;
}
state->memerrors_freelist = (PyBaseExceptionObject *) self->dict;
state->memerrors_numfree--;

Most of the freelists were made thread-safe by making them per-thread in the free threaded build (using pycore_freelist.h), but we don't want to do that for MemoryError because its freelist serves a different purpose (it's not really for performance). I think we should just use a lock for MemoryError's freelist.

Linked PRs

@colesbury colesbury added 3.13 bugs and security fixes 3.14 bugs and security fixes topic-free-threading type-bug An unexpected behavior, bug, or error labels Feb 4, 2025
colesbury added a commit to colesbury/cpython that referenced this issue Feb 4, 2025
The MemoryError freelist was not thread-safe in the free threaded build.
Use a mutex to protect accesses to the freelist. Unlike other freelists,
the MemoryError freelist is not performance sensitive.
@colesbury colesbury self-assigned this Feb 5, 2025
colesbury added a commit to colesbury/cpython that referenced this issue Feb 5, 2025
The MemoryError freelist was not thread-safe in the free threaded build.
Use a mutex to protect accesses to the freelist. Unlike other freelists,
the MemoryError freelist is not performance sensitive.
colesbury added a commit that referenced this issue Feb 6, 2025
… build (gh-129704)

The MemoryError freelist was not thread-safe in the free threaded build.
Use a mutex to protect accesses to the freelist. Unlike other freelists,
the MemoryError freelist is not performance sensitive.
colesbury added a commit to colesbury/cpython that referenced this issue Feb 6, 2025
…readed build (pythongh-129704)

The MemoryError freelist was not thread-safe in the free threaded build.
Use a mutex to protect accesses to the freelist. Unlike other freelists,
the MemoryError freelist is not performance sensitive.

(cherry picked from commit 51b4edb)
colesbury added a commit that referenced this issue Feb 6, 2025
…hreaded build (gh-129704) (gh-129742)

The MemoryError freelist was not thread-safe in the free threaded build.
Use a mutex to protect accesses to the freelist. Unlike other freelists,
the MemoryError freelist is not performance sensitive.

(cherry picked from commit 51b4edb)
colesbury added a commit to colesbury/cpython that referenced this issue Feb 6, 2025
This would catch the data race involving the MemoryError freelist that
was fixed in pythongh-129668.
colesbury added a commit to colesbury/cpython that referenced this issue Feb 6, 2025
This would catch the data race involving the MemoryError freelist that
was fixed in pythongh-129668.
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this issue Feb 7, 2025
…readed build (pythongh-129704)

The MemoryError freelist was not thread-safe in the free threaded build.
Use a mutex to protect accesses to the freelist. Unlike other freelists,
the MemoryError freelist is not performance sensitive.
cmaloney pushed a commit to cmaloney/cpython that referenced this issue Feb 8, 2025
…readed build (pythongh-129704)

The MemoryError freelist was not thread-safe in the free threaded build.
Use a mutex to protect accesses to the freelist. Unlike other freelists,
the MemoryError freelist is not performance sensitive.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes 3.14 bugs and security fixes topic-free-threading type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant