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-112069: Do not require lock if the set has never been exposed. #118069

Merged
merged 3 commits into from
Apr 25, 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
6 changes: 6 additions & 0 deletions Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2334,6 +2334,12 @@ set_init(PySetObject *self, PyObject *args, PyObject *kwds)
if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
return -1;

if (self->fill == 0 && Py_REFCNT(self) == 1) {
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 we should do the Py_REFCNT() check first so that we don't access self->fill without a lock in case there are other threads with references modifying self.

Also, do we need to set self->hash = -1 as we do below?

Copy link
Member Author

Choose a reason for hiding this comment

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

Also, do we need to set self->hash = -1 as we do below?

Well it will be more safe.

if (iterable == NULL) {
return 0;
}
return set_update_local(self, iterable);
}
Py_BEGIN_CRITICAL_SECTION(self);
if (self->fill)
set_clear_internal(self);
Expand Down
Loading