-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-128013: fix data race in PyUnicode_AsUTF8AndSize on free-threading #128021
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
Conversation
kumaraditya303
commented
Dec 17, 2024
•
edited by bedevere-app
bot
Loading
edited by bedevere-app
bot
- Issue: Data race in PyUnicode_AsUTF8AndSize under free-threading #128013
87bdaea
to
0f692ce
Compare
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 think there's still a bug where PyUnicode_UTF8()
is checked outside the lock, but the condition may change once the lock is acquired (because some other thread filled in the utf8
field).
I think we should refactor out the check into something like unicode_ensure_utf8
that does the double-checked locking:
static int
unicode_ensure_utf8(PyObject *unicode)
{
int err = 0;
if (PyUnicode_UTF8(unicode) == NULL) {
Py_BEGIN_CRITICAL_SECTION(unicode);
if (PyUnicode_UTF8(unicode) == NULL) {
err = unicode_fill_utf8(unicode);
}
Py_END_CRITICAL_SECTION();
}
return err;
}
unicode_fill_utf8
should assert that the critical section is held.
I wrote PR gh-128061 "Convert unicodeobject.c macros to functions" to prepare the code for this change. |
I have updated the PR to use the new static inline functions and it now uses acquire/release semantics for utf8 member. I have tested the reproducer from issue and now there aren't any data races AFAICS. |
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.
Overall looks good, I think there's just one issue in _PyUnicode_CheckConsistency.
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.
LGTM
|
The failure looks unrelated:
|
GH-128417 is a backport of this pull request to the 3.13 branch. |