Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-145272: Fix data race when accessing func_code in PyFunctionObject#145534
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?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -88,10 +88,14 @@ PyAPI_FUNC(int) PyFunction_SetAnnotations(PyObject *, PyObject *); | ||
| /* Static inline functions for direct access to these values. | ||
| Type checks are *not* done, so use with care. */ | ||
| static inline PyObject* PyFunction_GET_CODE(PyObject *func) { | ||
| return _PyFunction_CAST(func)->func_code; | ||
| PyFunctionObject *op = _PyFunction_CAST(func); | ||
| #ifdef Py_GIL_DISABLED | ||
| return (PyObject *)_Py_atomic_load_ptr(&op->func_code); | ||
| #else | ||
| return op->func_code; | ||
| #endif | ||
| } | ||
| #define PyFunction_GET_CODE(func) PyFunction_GET_CODE(_PyObject_CAST(func)) | ||
Krishna-web-hub marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| static inline PyObject* PyFunction_GET_GLOBALS(PyObject *func) { | ||
| return _PyFunction_CAST(func)->func_globals; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fixing race condition in PyFunctionObject.func_code | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should be less precise in terms of implementation and rather mention | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -631,7 +631,8 @@ func_get_code(PyObject *self, void *Py_UNUSED(ignored)) | ||
| return NULL; | ||
| } | ||
| return Py_NewRef(op->func_code); | ||
| PyCodeObject *code = _Py_atomic_load_ptr(&op->func_code); | ||
Krishna-web-hub marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return Py_NewRef(code); | ||
Comment on lines
+634
to
+635
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't right.
| ||
| } | ||
| static int | ||
| @@ -664,7 +665,7 @@ func_set_code(PyObject *self, PyObject *value, void *Py_UNUSED(ignored)) | ||
| return -1; | ||
| } | ||
| PyObject *func_code = PyFunction_GET_CODE(op); | ||
| PyCodeObject *func_code = (PyCodeObject *)PyFunction_GET_CODE(op); | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this change? | ||
| int old_flags = ((PyCodeObject *)func_code)->co_flags; | ||
| int new_flags = ((PyCodeObject *)value)->co_flags; | ||
| int mask = CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR; | ||
| @@ -679,7 +680,10 @@ func_set_code(PyObject *self, PyObject *value, void *Py_UNUSED(ignored)) | ||
| handle_func_event(PyFunction_EVENT_MODIFY_CODE, op, value); | ||
| _PyFunction_ClearVersion(op); | ||
| Py_XSETREF(op->func_code, Py_NewRef(value)); | ||
| PyCodeObject *new = (PyCodeObject *)Py_NewRef(value); | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be GIL_DISABLED-guarded Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, don't we have a FT-helper alterantive to Py_XSETREF? | ||
| PyCodeObject *old = | ||
| (PyCodeObject *)_Py_atomic_exchange_ptr(&op->func_code, new); | ||
| Py_XDECREF(old); | ||
| return 0; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
Aren't all attributes of function objects subject to this issue then?