8000 gh-120198: Stop the world when setting __class__ on free-threaded build by Fidget-Spinner · Pull Request #120672 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-120198: Stop the world when setting __class__ on free-threaded build #120672

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

Merged
merged 8 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 1 addition & 5 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,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);
return (PyTypeObject *)&ob->ob_type;
#else
return ob->ob_type;
#endif
Expand Down Expand Up @@ -278,11 +278,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
6 changes: 5 additions & 1 deletion Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@
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 @@ -7170,7 +7174,7 @@
int
_PyDict_DetachFromObject(PyDictObject *mp, PyObject *obj)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(obj);
ASSERT_WORLD_STOPPED_OR_OBJ_LOCKED(obj);

Check failure on line 7177 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Address sanitizer

implicit declaration of function ‘ASSERT_WORLD_STOPPED_OR_OBJ_LOCKED’; did you mean ‘ASSERT_WORLD_STOPPED_OR_DICT_LOCKED’? [-Werror=implicit-function-declaration]

Check failure on line 7177 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (1.1.1w)

implicit declaration of function ‘ASSERT_WORLD_STOPPED_OR_OBJ_LOCKED’; did you mean ‘ASSERT_WORLD_STOPPED_OR_DICT_LOCKED’? [-Werror=implicit-function-declaration]

Check failure on line 7177 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

implicit declaration of function ‘ASSERT_WORLD_STOPPED_OR_OBJ_LOCKED’; did you mean ‘ASSERT_WORLD_STOPPED_OR_DICT_LOCKED’? [-Werror=implicit-function-declaration]

Check failure on line 7177 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.0.13)

implicit declaration of function ‘ASSERT_WORLD_STOPPED_OR_OBJ_LOCKED’; did you mean ‘ASSERT_WORLD_STOPPED_OR_DICT_LOCKED’? [-Werror=implicit-function-declaration]

Check failure on line 7177 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.1.5)

implicit declaration of function ‘ASSERT_WORLD_STOPPED_OR_OBJ_LOCKED’; did you mean ‘ASSERT_WORLD_STOPPED_OR_DICT_LOCKED’? [-Werror=implicit-function-declaration]

Check failure on line 7177 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.2.1)

implicit declaration of function ‘ASSERT_WORLD_STOPPED_OR_OBJ_LOCKED’; did you mean ‘ASSERT_WORLD_STOPPED_OR_DICT_LOCKED’? [-Werror=implicit-function-declaration]

Check warning on line 7177 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

'ASSERT_WORLD_STOPPED_OR_OBJ_LOCKED' undefined; assuming extern returning int [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 7177 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Windows / build (arm64)

'ASSERT_WORLD_STOPPED_OR_OBJ_LOCKED' undefined; assuming extern returning int [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 7177 in Objec 8000 ts/dictobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test

implicit declaration of function ‘ASSERT_WORLD_STOPPED_OR_OBJ_LOCKED’; did you mean ‘ASSERT_WORLD_STOPPED_OR_DICT_LOCKED’? [-Werror=implicit-function-declaration]

Check warning on line 7177 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build (arm64)

'ASSERT_WORLD_STOPPED_OR_OBJ_LOCKED' undefined; assuming extern returning int [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 7177 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (x64)

'ASSERT_WORLD_STOPPED_OR_OBJ_LOCKED' undefined; assuming extern returning int [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]
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);
#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);
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.

assert(_PyObject_GetManagedDict(self) == dict ||
dict->ma_values != _PyObject_InlineValues(self));

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);
}

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
0