assert(_PyObject_GetManagedDict(self) == dict);
- if (_PyDict_DetachFromObject(dict, self) < 0) {
+ int err;
+ Py_BEGIN_CRITICAL_SECTION(dict);
+ err = _PyDict_DetachFromObject(dict, self);
+ Py_END_CRITICAL_SECTION();
+ if (err < 0) {
return -1;
}
#ifdef Py_GIL_DISABLED
PyInterpreterState *interp = _PyInterpreterState_GET();
- _PyEval_StopTheWorld(interp);
+ int unique = _PyObject_IsUniquelyReferenced(self);
+ if (!unique) {
+ _PyEval_StopTheWorld(interp);
+ }
#endif
PyTypeObject *oldto = Py_TYPE(self);
int res = object_set_class_world_stopped(self, newto);
#ifdef Py_GIL_DISABLED
- _PyEval_StartTheWorld(interp);
+ if (!unique) {
+ _PyEval_StartTheWorld(interp);
+ }
#endif
if (res == 0) {
if (oldto->tp_flags & Py_TPFLAGS_HEAPTYPE) {