-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-133931: Introduce _PyObject_XSetRefDelayed to replace Py_XSETREF #134377
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
base: main
Are you sure you want to change the base?
Conversation
Objects/obmalloc.c
Outdated
_PyObject_XSetRefDelayed(PyObject **ptr, PyObject *value) | ||
{ | ||
PyObject *old = *ptr; | ||
FT_ATOMIC_STORE_PTR_RELEASE(*ptr, Py_NewRef(value)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default build implementation in pycore_pymem.h
doesn't include the Py_NewRef()
.
My preference is to keep the Py_NewRef(value)
in the callers, i.e.:
_PyObject_XSetRefDelayed(dictptr, Py_NewRef(value));
to match the semantics of Py_XSETREF
.
Interesting... CI passed in my local but not in the CI.. :( |
Ah okay..
|
Why do we need Also, what is "delayed"? Either choose a clearer name, or add an explanatory comment in the header file. |
IIUC, the critical section is for the root object to prevent other threads from mutating the child object, and a delayed reference to prevent use-after-free from other threads that reference the child object. |
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
Could someone please explain to me, why Line 7381 in 598aa7c
Which one is more correct? |
@corona10 |
Gentle ping @kumaraditya303 @vstinner |
@@ -3967,13 +3967,7 @@ _PyObject_SetDict(PyObject *obj, PyObject *value) | |||
return -1; | |||
} | |||
Py_BEGIN_CRITICAL_SECTION(obj); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind adding a comment explaining why XDecRefDelayed() is needed here?
@@ -740,7 +745,9 @@ gen_set_qualname(PyObject *self, PyObject *value, void *Py_UNUSED(ignored)) | |||
"__qualname__ must be set to a string object"); | |||
return -1; | |||
} | |||
Py_XSETREF(op->gi_qualname, Py_NewRef(value)); | |||
Py_BEGIN_CRITICAL_SECTION(self); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind adding a comment explaining why XDecRefDelayed() is needed here? The qualname should be a simple Python str object, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even with simple str objects there can be a crash w/o critical session. What exact way of fixing you're proposing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not proposing anything, I don't understand well the purpose of XDecRefDelayed(). That's why I'm asking for a comment :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, it will be great to have some best practices for free-threading build in InternalDocs. This case seems like a standard one, and some standard solutions will be great.
For this exact case we just need some kind of locking for synchronization reading/writing. And there's one way for this.
@@ -718,15 +720,18 @@ gen_set_name(PyObject *self, PyObject *value, void *Py_UNUSED(ignored)) | |||
"__name__ must be set to a string object"); | |||
return -1; | |||
} | |||
Py_XSETREF(op->gi_name, Py_NewRef(value)); | |||
Py_BEGIN_CRITICAL_SECTION(self); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind adding a comment explaining why XDecRefDelayed() is needed here? The name should be a simple Python str object, no?
gen_set_name
andgen_set_qualname
thread-safe in free-threaded builds #133931