-
-
Notifications
You must be signed in to change notification settings - Fork 34.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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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.
Show resolved
Hide resolved
|
||
| 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; | ||
| } | ||
|
|
||
|
|
||
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?