8000 gh-117657: Make Py_TYPE and Py_SET_TYPE thread safe by Fidget-Spinner · Pull Request #120165 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-117657: Make Py_TYPE and Py_SET_TYPE thread safe #120165

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 9 commits into from
Jun 12, 2024
Merged
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
Prev Previous commit
Next Next commit
Lock critical section
  • Loading branch information
Fidget-Spinner committed Jun 12, 2024
commit 9a33a780d5a87865b3e3f9061e4378c5d1b2da46
8 changes: 7 additions & 1 deletion Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -6630,12 +6630,18 @@ object_set_class(PyObject *self, PyObject *value, void *closure)
return -1;
}
}
Py_BEGIN_CRITICAL_SECTION(self);
if (newto->tp_flags & Py_TPFLAGS_HEAPTYPE) {
Py_INCREF(newto);
Copy link
Member

Choose a reason for hiding this comment

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

Is it important to do the INCREF(newto) and DECREF(oldto) inside the critical section?

Or it it possible to restrict the critical section to the following code?

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

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right. I can shrink it!

}
// 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);
if (oldto->tp_flags & Py_TPFLAGS_HEAPTYPE)
if (oldto->tp_flags & Py_TPFLAGS_HEAPTYPE) {
Py_DECREF(oldto);
}
Py_END_CRITICAL_SECTION();

RARE_EVENT_INC(set_class);
return 0;
Expand Down
Loading
0