Skip to content
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

gh-129668: Fix thread-safety of MemoryError freelist in free threaded build #129704

Merged
merged 1 commit into from
Feb 6, 2025
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
3 changes: 3 additions & 0 deletions Include/internal/pycore_exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ struct _Py_exc_state {
PyObject *errnomap;
PyBaseExceptionObject *memerrors_freelist;
int memerrors_numfree;
#ifdef Py_GIL_DISABLED
PyMutex memerrors_lock;
#endif
// The ExceptionGroup type
PyObject *PyExc_ExceptionGroup;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix race condition when raising :exc:`MemoryError` in the free threaded
build.
62 changes: 36 additions & 26 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -3832,36 +3832,43 @@ SimpleExtendsException(PyExc_Exception, ReferenceError,

#define MEMERRORS_SAVE 16

#ifdef Py_GIL_DISABLED
# define MEMERRORS_LOCK(state) PyMutex_LockFlags(&state->memerrors_lock, _Py_LOCK_DONT_DETACH)
# define MEMERRORS_UNLOCK(state) PyMutex_Unlock(&state->memerrors_lock)
#else
# define MEMERRORS_LOCK(state) ((void)0)
# define MEMERRORS_UNLOCK(state) ((void)0)
#endif

static PyObject *
get_memory_error(int allow_allocation, PyObject *args, PyObject *kwds)
{
PyBaseExceptionObject *self;
PyBaseExceptionObject *self = NULL;
struct _Py_exc_state *state = get_exc_state();
if (state->memerrors_freelist == NULL) {
if (!allow_allocation) {
PyInterpreterState *interp = _PyInterpreterState_GET();
return Py_NewRef(
&_Py_INTERP_SINGLETON(interp, last_resort_memory_error));
}
PyObject *result = BaseException_new((PyTypeObject *)PyExc_MemoryError, args, kwds);
return result;
}

/* 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 */
MEMERRORS_LOCK(state);
if (state->memerrors_freelist != NULL) {
/* Fetch MemoryError from freelist and initialize it */
self = state->memerrors_freelist;
state->memerrors_freelist = (PyBaseExceptionObject *) self->dict;
state->memerrors_numfree--;
self->dict = NULL;
self->args = (PyObject *)&_Py_SINGLETON(tuple_empty);
_Py_NewReference((PyObject *)self);
_PyObject_GC_TRACK(self);
}
MEMERRORS_UNLOCK(state);

if (self->args == NULL) {
return NULL;
if (self != NULL) {
return (PyObject *)self;
}

state->memerrors_freelist = (PyBaseExceptionObject *) self->dict;
state->memerrors_numfree--;
self->dict = NULL;
_Py_NewReference((PyObject *)self);
_PyObject_GC_TRACK(self);
return (PyObject *)self;
if (!allow_allocation) {
PyInterpreterState *interp = _PyInterpreterState_GET();
return Py_NewRef(
&_Py_INTERP_SINGLETON(interp, last_resort_memory_error));
}
return BaseException_new((PyTypeObject *)PyExc_MemoryError, args, kwds);
}

static PyObject *
Expand Down Expand Up @@ -3907,14 +3914,17 @@ MemoryError_dealloc(PyObject *obj)
}

struct _Py_exc_state *state = get_exc_state();
if (state->memerrors_numfree >= MEMERRORS_SAVE) {
Py_TYPE(self)->tp_free((PyObject *)self);
}
else {
MEMERRORS_LOCK(state);
if (state->memerrors_numfree < MEMERRORS_SAVE) {
self->dict = (PyObject *) state->memerrors_freelist;
state->memerrors_freelist = self;
state->memerrors_numfree++;
MEMERRORS_UNLOCK(state);
return;
}
MEMERRORS_UNLOCK(state);

Py_TYPE(self)->tp_free((PyObject *)self);
}

static int
Expand Down
Loading