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-120198: Stop the world when setting __class__ on free-threaded build #120672

Merged
merged 8 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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: 0 additions & 8 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,7 @@ _Py_IsOwnedByCurrentThread(PyObject *ob)

// bpo-39573: The Py_SET_TYPE() function must be used to set an object type.
static inline PyTypeObject* Py_TYPE(PyObject *ob) {
#ifdef Py_GIL_DISABLED
return (PyTypeObject *)_Py_atomic_load_ptr_relaxed(&ob->ob_type);
#else
return ob->ob_type;
#endif
}
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
# define Py_TYPE(ob) Py_TYPE(_PyObject_CAST(ob))
Expand Down Expand Up @@ -278,11 +274,7 @@ static inline int Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {


static inline void Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
#ifdef Py_GIL_DISABLED
_Py_atomic_store_ptr(&ob->ob_type, type);
#else
ob->ob_type = type;
#endif
}
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
# define Py_SET_TYPE(ob, type) Py_SET_TYPE(_PyObject_CAST(ob), type)
Expand Down
7 changes: 6 additions & 1 deletion Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ ASSERT_DICT_LOCKED(PyObject *op)
if (!_PyInterpreterState_GET()->stoptheworld.world_stopped) { \
ASSERT_DICT_LOCKED(op); \
}
#define ASSERT_WORLD_STOPPED_OR_OBJ_LOCKED(op) \
if (!_PyInterpreterState_GET()->stoptheworld.world_stopped) { \
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op); \
}

#define IS_DICT_SHARED(mp) _PyObject_GC_IS_SHARED(mp)
#define SET_DICT_SHARED(mp) _PyObject_GC_SET_SHARED(mp)
Expand Down Expand Up @@ -227,6 +231,7 @@ static inline void split_keys_entry_added(PyDictKeysObject *keys)

#define ASSERT_DICT_LOCKED(op)
#define ASSERT_WORLD_STOPPED_OR_DICT_LOCKED(op)
#define ASSERT_WORLD_STOPPED_OR_OBJ_LOCKED(op)
#define LOCK_KEYS(keys)
#define UNLOCK_KEYS(keys)
#define ASSERT_KEYS_LOCKED(keys)
Expand Down Expand Up @@ -7170,7 +7175,7 @@ PyObject_ClearManagedDict(PyObject *obj)
int
_PyDict_DetachFromObject(PyDictObject *mp, PyObject *obj)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(obj);
ASSERT_WORLD_STOPPED_OR_OBJ_LOCKED(obj);
assert(_PyObject_ManagedDictPointer(obj)->dict == mp);
assert(_PyObject_InlineValuesConsistencyCheck(obj));

Expand Down
39 changes: 23 additions & 16 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -6599,6 +6599,12 @@ object_set_class(PyObject *self, PyObject *value, void *closure)
}

PyTypeObject *oldto = Py_TYPE(self);
#ifdef Py_GIL_DISABLED
PyInterpreterState *interp = _PyInterpreterState_GET();
// The real Py_TYPE(self) (`oldto`) may have changed from
// underneath us in another thread, so we re-fetch it here.
_PyEval_StopTheWorld(interp);
Fidget-Spinner marked this conversation as resolved.
Show resolved Hide resolved
#endif

/* In versions of CPython prior to 3.5, the code in
compatible_for_assignment was not set up to correctly check for memory
Expand Down Expand Up @@ -6656,7 +6662,7 @@ object_set_class(PyObject *self, PyObject *value, void *closure)
PyErr_Format(PyExc_TypeError,
"__class__ assignment only supported for mutable types "
"or ModuleType subclasses");
return -1;
goto err;
}

if (compatible_for_assignment(oldto, newto, "__class__")) {
Expand All @@ -6665,47 +6671,48 @@ object_set_class(PyObject *self, PyObject *value, void *closure)
if (oldto->tp_flags & Py_TPFLAGS_INLINE_VALUES) {
PyDictObject *dict = _PyObject_MaterializeManagedDict(self);
Fidget-Spinner marked this conversation as resolved.
Show resolved Hide resolved
if (dict == NULL) {
return -1;
goto err;
}

bool error = false;

Py_BEGIN_CRITICAL_SECTION2(self, dict);

// If we raced after materialization and replaced the dict
// then the materialized dict should no longer have the
// inline values in which case detach is a nop.
// Note: we don't need to lock here because the world should be stopped.
Fidget-Spinner marked this conversation as resolved.
Show resolved Hide resolved

assert(_PyObject_GetManagedDict(self) == dict ||
dict->ma_values != _PyObject_InlineValues(self));
Fidget-Spinner marked this conversation as resolved.
Show resolved Hide resolved

if (_PyDict_DetachFromObject(dict, self) < 0) {
error = true;
goto err;
}

Py_END_CRITICAL_SECTION2();
if (error) {
return -1;
}
}
if (newto->tp_flags & Py_TPFLAGS_HEAPTYPE) {
Py_INCREF(newto);
}
Py_BEGIN_CRITICAL_SECTION(self);
// The real Py_TYPE(self) (`oldto`) may have changed from
// underneath us in another thread, so we re-fetch it here.

oldto = Py_TYPE(self);
Py_SET_TYPE(self, newto);
Py_END_CRITICAL_SECTION();

if (oldto->tp_flags & Py_TPFLAGS_HEAPTYPE) {
Py_DECREF(oldto);
Fidget-Spinner marked this conversation as resolved.
Show resolved Hide resolved
}

RARE_EVENT_INC(set_class);

#ifdef Py_GIL_DISABLED
_PyEval_StartTheWorld(interp);
#endif
return 0;
}
else {
return -1;
goto err;
}
err:
Copy link
Member

Choose a reason for hiding this comment

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

To have a more regular try: ... finally: _PyEval_StartTheWorld() pattern, you can add an int res = -1; variable, replace goto err with goto done, and set res to 0 on success (3 lines above).

Copy link
Contributor

Choose a reason for hiding this comment

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

I think @vstinner's suggestion is fine. Or you can refactor the parts that should be in a stop-the-world call into it's own function, like we often do for locks.

Another advantage of moving the body to a separate function is that it makes it more clear what data crosses the stop-the-world boundary -- some data loaded before the stop-the-world call may not be valid after it.

#ifdef Py_GIL_DISABLED
_PyEval_StartTheWorld(interp);
#endif
return -1;
}

static PyGetSetDef object_getsets[] = {
Expand Down
Loading